Nginx重定向www。错误的Vhost

时间:2015-01-11 12:22:54

标签: django nginx gunicorn

嗯我有问题......

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



我的Vhosts:

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/;
    }
}

2 个答案:

答案 0 :(得分:1)

我不打算清理和验证您的配置文件,但它们包含几个问题,您应该DRY出来。

关于你的实际问题。

  1. 您尚未配置任何重定向,因此nginx很乐意将这些子域重定向到您的默认服务器。
  2. 您尚未配置默认服务器,而nginx只是使用第一个定义的服务器作为默认服务器(在您的情况下为site,因为site2site3来自该服务器之后;简单的排序)。
  3. 实际的解决方案是配置您希望为每台服务器进行的重定向。这个代码段是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...
    }
    

答案 1 :(得分:0)

你需要

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个域中的每个域配置此配置..

相关问题