nginx无限302findredirect循环

我正在使用nginx将多个域映射到谷歌应用程序引擎。 在这个特定的情况下,我将多个域映射到同一个租户 。 在我的代码中有一个configuration知道哪个域是主域,当请求进来我检查主机是否是主域,如果不是我redirect到主域。

例如,假设www.aaa.com是默认设置的域

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

偶尔随机发生的事情是JavaScripturl进入nginx的redirect循环。 请注意,请求不会触及appengine。 nginx正在创build这个302 Found循环。 这在过去两周里发生了两次。

请求path类似于www.aaa.com/22/foobar.js ,其中path的第一部分是JavaScript文件的版本号。

具有相同uri格式的css文件www.aaa.com/22/foobar.css不会有这个redirect循环。

如果我部署新版本,文件将再次正确服务。 所以部署一个版本23和访问www.aaa.com/23/foobar.js将再次工作。 另外如果我使用像www.aaa.com/22/foobar.js?345重新启动nginx请求caching或甚至重新启动nginx运行的机器没有帮助。 build立一个具有相同的确切configuration的新机器没有问题的文件服务。

那么可能会导致这个redirect循环? 一旦发生,我怎么能摆脱它? 有没有在Nginx的caching(我知道Nginx默认情况下不caching)我不知道? 该机器可能caching这些redirect?

这里是我的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; } } } 

所以我通过从nginx而不是从后端redirect来解决这个问题。
虽然现在我已经解决了这个问题,但是我仍然没有弄清楚为什么这个redirect循环首先发生了。

每一个提示都会有所帮助。