创建只能访问某些页面的子域别名

时间:2015-01-30 20:04:26

标签: angularjs nginx routing subdomain

背景

我们正在为想要一个“白标”解决方案的人运行一个项目(他们希望他们的网站的子域显示我们网站的某些页面,样式看起来像他们的)。我们倾向于这条路线的主要原因是他们的用户觉得他们没有离开原始网站(或他们是相关的)并且客户端坚持。

计划

将子域指向我们的服务器,但只允许它访问与此项目相关的页面(否则我们的常规网站页面会让人感到困惑)。我们的前端堆栈只是一个小型Nginx服务器,里面有一个单页面应用程序。我们没有从该站点的区域到其他区域的任何导航,但如果路径没有被阻止,人们仍然可以到达那里。我们想要访问的页面是:

  • example.example.com/profiles/*
    • 个人资料是正在开发的网站的新区域

//其中example.example.com只是other.com的别名

问题

  1. 确定请求者是使用子域还是主域的最可靠方法是什么?
  2. 然后我们可以向/ profiles / *页面外的请求提供404吗?
  3. 使用服务器配置(Nginx)或路由拦截器(AngularJS)更好吗?

1 个答案:

答案 0 :(得分:2)

假设您的客户端拥有example.com域并且您拥有example.org域,则在客户端服务器上添加此配置应该可以解决问题

server {
    listen 80;
    server_name sub.example.com;

    location /profiles/ {
        # proxy pass all requests to example.org/relevant/profiles/folder
        proxy_pass http://example.org/relevant/profiles/folder/;
        proxy_redirect default;
    }
    location / {
        # Return 404 for all other requests to sub.example.com
        return 404;
    }
}