Nginx 重定向 www.错误的虚拟主机

Nginx redirects www. to wrong Vhost

嗯我有个问题...

浏览器将我的 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/*;
}



我的虚拟主机:

我的网站(/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/;
    }
}



我的站点 2 (/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/;
        }
    }



我的网站 3 (/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/;
    }
}

你需要

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 wwwwww to www。这 3 个域中的每一个都需要此配置..

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

关于您的实际问题。

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

实际的解决方案是为每个服务器配置您希望发生的重定向。此片段摘自 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...
}