nginx访问subdomain.domain.com作为domain.com

时间:2014-09-20 11:07:16

标签: ruby-on-rails nginx dns http-redirect

我是nginx系统的新用户,之前我通过this answer的帮助设法通过subdomain.domain.com:3000(没有端口号)访问subdomain.domain.com

我发现难以实现以下目标:

我希望通过subdomain.domain.com访问在domain.com中运行的Rails服务器。即
 当有人在浏览器中点击网址domain.com时,它应该充当subdomain.domain.com,但网址不应该更改为浏览器。

任何人都可以帮助我如何实现它?

我在/etc/nginx/sites-enabled/default中注释掉了默认设置并创建了我的 /etc/nginx/sites-enabled/myblog中的自己设置为:

upstream my-app-cluster
{
    server blog.budhram.com:3000;
}
server
{
    listen       80;
    server_name budhram.com;
    # above not working but if used blog.budhram.com then working but not expected

    # rails app public folder path
    root /home/ubuntu/myblog/public;

    # rails app log file path
    access_log  /home/ubuntu/myblog/log/development.log;

    location / {
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (-f $request_filename/index.html) {
            rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename.html) {
            rewrite (.*) $1.html break;
        }
        if (!-f $request_filename) {
           proxy_pass http://my-app-cluster;
           break;
        }
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以为代理设置所需的主机,不应在浏览器中更改网址,但这可以更改代理网站上基于主机名的链接:

proxy_set_header Host subdomain.domain.com;