【问题标题】:Nginx redirects www. to wrong VhostNginx 重定向 www.错误的虚拟主机
【发布时间】:2015-01-11 12:22:53
【问题描述】:

嗯,我有个问题……

浏览器将我的 3 个虚拟站点 www.site1.com www.site2.com www.site3.com 重定向到 www.site1.com,只有当我使用 http://site3.com / http://site2.com 时它才能正常工作并正确重定向。

更简单地说,我的问题是 http:// 正确重定向到我的虚拟实体, www 没有。

www.site2.com -> www.site1.com     (redirects to wrong vhost)
http://site2.com -> www.site2.com  works!



我的设置是 Nginx + Gunicorn + Django,但我坚信这是我的 Nginx 配置中的问题。



Nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;



        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

    text/javascript;



        ##
        # Virtual Host Configs
        ##


        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}



我的虚拟主机:

MYSITE (/etc/nginx/sites-enabled/site)

upstream mysite_app_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  server unix:/webapps/mysite/run/gunicorn.sock fail_timeout=0;
}

server {

    listen   80;
    server_name mysite.com;

    client_max_body_size 4G;

    access_log /webapps/mysite/logs/nginx-access.log;
    error_log /webapps/mysite/logs/nginx-error.log;

    location /static/ {
        alias   /webapps/mysite/static/;
    }

    location /media/ {
        alias   /webapps/mysite/mysite/media/;
    }

    location / {

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://mysite_app_server;
            break;
        }

        include includes/botblock;

    }

    # Error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /webapps/mysite/static/;
    }
}



MYSITE2 (/etc/nginx/sites-enabled/site2)

upstream mysite2_app_server {
      # fail_timeout=0 means we always retry an upstream even if it failed
      # to return a good HTTP response (in case the Unicorn master nukes a
      # single worker for timing out).

      server unix:/webapps/mysite2/run/gunicorn.sock fail_timeout=0;
    }

    server {

        listen   80;
        server_name mysite2.com;

        client_max_body_size 4G;

        access_log /webapps/mysite2/logs/nginx-access.log;
        error_log /webapps/mysite2/logs/nginx-error.log;

        location /static/ {
            alias   /webapps/mysite2/static/;
        }

        location /media/ {
            alias   /webapps/mysite2/mysite2/media/;
        }

        location / {


            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            if (!-f $request_filename) {
                proxy_pass http://mysite2_app_server;
                break;
            }

            include includes/botblock;

        }

        # Error pages
        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /webapps/mysite2/static/;
        }
    }



MYSITE3 (/etc/nginx/sites-enabled/site3)

upstream mysite3_app_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  server unix:/webapps/mysite3/run/gunicorn.sock fail_timeout=0;
}



server {

    listen   80;
    server_name mysite3.com;

    client_max_body_size 4G;

    access_log /webapps/mysite3/logs/nginx-access.log;
    error_log /webapps/mysite3/logs/nginx-error.log;

    location /static/ {
        alias   /webapps/mysite3/static/;
    }

    location /media/ {
        alias   /webapps/mysite3//media/;
    }


    location / {


        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://mysite3_app_server;
            break;
        }

        include includes/botblock;

    }

    # Error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /webapps/mysite3/static/;
    }
}

【问题讨论】:

    标签: django nginx gunicorn


    【解决方案1】:

    我不会清理和验证您的配置文件,但它们包含几个问题,您应该 DRY 解决它们。

    关于您的实际问题。

    1. 您尚未配置任何重定向,因此 nginx 很乐意将这些子域重定向到您的默认服务器。
    2. 您尚未配置默认服务器,而 nginx 只是使用第一个定义的服务器作为默认服务器(在您的情况下,site 因为site2 和site3 紧随其后;简单排序)。李>

    实际的解决方案是为每个服务器配置您希望发生的重定向。这个 sn-p 是在 from another answer of mine 上针对类似问题拍摄的。

    server {
        #listen 80 is default
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }
    
    server {
        #listen 80 is default
        server_name example.com;
        ## here goes the rest of your conf...
    }
    

    【讨论】:

    • 如果它解决了您的问题,您应该将答案标记为正确。
    • @Toothfairy,请标记答案,这样人们就不会浪费时间尝试回答已经回答的问题
    • 试图找到一个函数,我在哪里可以找到它?
    • 答案左侧有向上和向下投票按钮,在其下方您会找到一个复选标记(仅当您是提出问题的人时),只需单击它即可标记回答为解决您的问题的正确答案。您可以随时咨询 Stackoverflow 的帮助中心以获取更多信息:stackoverflow.com/help/someone-answers
    【解决方案2】:

    你需要

    server {
        listen       80;
        server_name  mysite1.com;
        return       301 http://www.mysite1.com$request_uri;
    }
    
    server {
        listen      80;
        server_name www.mysite1.com;
        # other stuff... 
    
    }
    

    这会重定向non-www to www 和www to www。您需要为这 3 个域中的每一个域进行此配置..

    【讨论】:

    • 我试图实现这一点,但现在它导致没有页面加载并且所有站点(SITE、SITE1、SITE2)都被重定向到 SITE 并在浏览器中返回以下错误:此网页有一个重定向循环 ReloadHide详细信息 ww w .site .com/ 的网页导致了过多的重定向。清除此站点的 cookie 或允许第三方 cookie 可能会解决问题。如果不是,则可能是服务器配置问题,而不是您的计算机问题。了解有关此问题的更多信息。错误代码:ERR_TOO_MANY_REDIRECTS 在我尝试之前清除了缓存
    • @Toothfairy 从你的配置中移除 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; 的东西。
    • 我从所有三个 Vhost 中删除了它,重新加载并重新启动了服务器,不幸的是它们仍然重定向到第一个 Vhost。移除并返回 301 站点时。 com $request_uri;补充说我仍然被重定向到第一个 Vhost,但来自正确站点的域仍然在 url 字段中(但仍然重定向不正确)。
    • 谢谢,你的回答也很有帮助,可惜直到现在我才明白需要同时添加上面的两个服务器{}。
    猜你喜欢
    • 2015-06-09
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2014-07-28
    • 2014-07-08
    • 1970-01-01
    • 2017-09-13
    • 2016-02-28
    相关资源
    最近更新 更多