Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am using nginx to map multiple domains to google app engine. in this specific case i map multiple domains to the same tenant. there is a configuration in my code that knows which domain is the main domain and when a request comes in i check if the host is the main domain and if it isn't i redirect to the main domain.

for example lets say www.aaa.com is the domain set as default

aaa.com     -> www.aaa.com
www.bbb.com -> www.aaa.com
ccc.com     -> www.aaa.com
...

what happens randomly from time to time is that a javascript url goes into a redirect loop on nginx. note that the request never hits appengine. nginx is creating this 302 Found loop. this happened 2 times in the past 2 weeks.

the request path looks like www.aaa.com/22/foobar.js where the first part of the path is the version number of the javascript file.

a css file with the same uri format www.aaa.com/22/foobar.css won't have this redirect loop.

if i deploy a new version the file will server correctly again. so deploying a version 23 and accessing www.aaa.com/23/foobar.js would work again. also if i use a cachebuster on the request like www.aaa.com/22/foobar.js?345 restarting nginx or even restarting the machine where nginx is running does not help. setting up a new machine with the same exact configuration serves the file with no problem.

so what could cause this redirect loop? how could i get rid of it once it happens? is there a cache on nginx (i know nginx is not caching by default) i dont know about? does the machine maybe cache those redirects?

here my nginx.conf

user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    server_names_hash_max_size 1024;
    server_names_hash_bucket_size 128;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;
    sendfile on;

    keepalive_timeout 65;
    # enable gzip compression
    gzip on;
    gzip_min_length 1100;
    gzip_buffers 4 32k;
    gzip_comp_level 5;
    gzip_types text/plain application/x-javascript text/xml text/css;
    gzip_vary on;


    server {
        server_name www.aaa.com;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://tenantname.myappid.appspot.com/$request_uri;
            proxy_set_header Host tenantname.myappid.appspot.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header NexHost $scheme://$host;
            proxy_redirect http://tenantname.myappid.appspot.com/ /;
            proxy_intercept_errors on;
        }
    }


    server {
        server_name aaa.com;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://tenantname.myappid.appspot.com/$request_uri;
            proxy_set_header Host tenantname.myappid.appspot.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header NexHost $scheme://$host;
            proxy_redirect http://tenantname.myappid.appspot.com/ /;
            proxy_intercept_errors on;
        }
    }

    server {
        server_name www.bbb.com;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://tenantname.myappid.appspot.com/$request_uri;
            proxy_set_header Host tenantname.myappid.appspot.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header NexHost $scheme://$host;
            proxy_redirect http://tenantname.myappid.appspot.com/ /;
            proxy_intercept_errors on;
        }
    }



    server {
        server_name bbb.com;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://tenantname.myappid.appspot.com/$request_uri;
            proxy_set_header Host tenantname.myappid.appspot.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header NexHost $scheme://$host;
            proxy_redirect http://tenantname.myappid.appspot.com/ /;
            proxy_intercept_errors on;
        }
    }



    server {
        server_name www.ccc.com;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://tenantname.myappid.appspot.com/$request_uri;
            proxy_set_header Host tenantname.myappid.appspot.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header NexHost $scheme://$host;
            proxy_redirect http://tenantname.myappid.appspot.com/ /;
            proxy_intercept_errors on;
        }
    }


    server {
        server_name ccc.com;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://tenantname.myappid.appspot.com/$request_uri;
            proxy_set_header Host tenantname.myappid.appspot.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header NexHost $scheme://$host;
            proxy_redirect http://tenantname.myappid.appspot.com/ /;
            proxy_intercept_errors on;
        }
    }


    server {
        server_name ~^(?<sub>.+)\.maindomain\.com$;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://$sub.myappid.appspot.com/$request_uri;
            proxy_set_header Host $sub.myappid.appspot.com;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header NexHost $scheme://$host;
            proxy_redirect http://$sub.myappid.appspot.com/ /;
            proxy_intercept_errors on;
        }
    }

    server {
        listen 80 default_server;
        server_name _;
        location / {
            root /etc/nginx/html;
        }
    }
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

so i solved this problem by redirecting from nginx rather than from the backend.
while the issue is solved by now i still didn't figure out why this redirect loop was happening in the first place.

every hint would be helpful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.