0

Hmm I have a problem...

Browser redirects my 3 virtual sites www.site1.com www.site2.com www.site3.com to www.site1.com, only when I use http://site3.com / http://site2.com it works and redirects correctly.

More simply put my problem is that http:// redirects to my virtual entities correctly, www does NOT.

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



My setup is Nginx + Gunicorn + Django, however I strongly believe this is a problem within my Nginx configuration.



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



My 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/;
    }
}
Toothfairy
  • 383
  • 1
  • 6
  • 24

2 Answers2

1

I’m not going to sanitize and validate your configuration files, but they contain several problems and you should DRY them out.

Regarding your actual question.

  1. You haven’t configured any redirects and nginx is therefore happily redirecting those subdomains to your default server.
  2. You haven’t configured a default server and nginx is simply using the very first defined server as default server (in your case site since site2 and site3 come after that one; simple sort).

The actual solution is to configure the redirects you want to happen for each of your servers. This snippet is taken from another answer of mine on a similar question.

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...
}
Community
  • 1
  • 1
Fleshgrinder
  • 15,703
  • 4
  • 47
  • 56
  • 1
    @Toothfairy, please mark the answer so people don't waste time trying to answer an already answered question – Mohammad AbuShady Jan 11 '15 at 14:51
  • 1
    Tried to find a function for that, where do I find it? – Toothfairy Jan 14 '15 at 16:55
  • To the left of an answer you have the up and down vote buttons, below that you'll find a checkmark (only if you were the person who asked the question), just click it to mark an answer as the correct one that solved your problem. You may always consult the Help Center here at Stackoverflow for more information: https://stackoverflow.com/help/someone-answers – Fleshgrinder Jan 14 '15 at 17:10
0

you need

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... 

}

this redirects non-www to www and www to www. you need this config for each of those 3 domains..

doniyor
  • 36,596
  • 57
  • 175
  • 260
  • I tried to implement this but now it results in that no page loads and all sites (SITE, SITE1, SITE2) gets redirected to SITE and return following error in browser: This webpage has a redirect loop ReloadHide details The webpage at http://ww w .site .com/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. Learn more about this problem. Error code: ERR_TOO_MANY_REDIRECTS Cleared cache before I tried – Toothfairy Jan 11 '15 at 12:51
  • @Toothfairy remove ``proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off;`` stuff from your config. – doniyor Jan 11 '15 at 12:51
  • I removed it from all three Vhosts, reloaded and restarted server, unfortunately they all still redirect to first Vhost. When removed and with return 301 site. com $request_uri; added I still get redirected to first Vhost but domain from correct site remains in url field (but still redirects incorrectly). – Toothfairy Jan 11 '15 at 12:59
  • Thank you, your answer was also helpful, unfortunately I did not understand that I needed to add both server {} above until now. – Toothfairy Jan 11 '15 at 13:11