我正在使用 nginx 将多个域映射到 Google App Engine。在这个特定情况下,我将多个域映射到同一租户。我的代码中有一个配置,它知道哪个域是主域,当请求进来时,我会检查主机是否是主域,如果不是,我会重定向到主域。
例如,假设www.aaa.com
域名设置为默认域名
aaa.com -> www.aaa.com
www.bbb.com -> www.aaa.com
ccc.com -> www.aaa.com
...
时不时会随机发生的情况是,javascript url 会在 nginx 上进入重定向循环。请注意,请求从未到达 appengine。nginx 正在创建此302 Found
循环。这种情况在过去两周内发生过两次。
请求路径看起来像www.aaa.com/22/foobar.js
路径的第一部分是 javascript 文件的版本号。
具有相同 uri 格式的 css 文件www.aaa.com/22/foobar.css
不会出现此重定向循环。
如果我部署一个新版本,该文件将再次正确提供服务。因此,部署一个版本23
并再次访问www.aaa.com/23/foobar.js
将可以正常工作。此外,如果我在请求上使用缓存破坏器(例如www.aaa.com/22/foobar.js?345
重新启动 nginx 或甚至重新启动运行 nginx 的机器)也无济于事。设置具有完全相同配置的新机器可以毫无问题地提供该文件。
那么什么会导致此重定向循环?一旦发生,我该如何摆脱它?nginx 上是否有缓存(我知道 nginx 默认不缓存)我不知道?机器是否可能缓存这些重定向?
这是我的 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;
}
}
}
答案1
所以我通过从 nginx 而不是从后端重定向来解决了这个问题。
虽然现在问题已经解决了,但我仍然不明白为什么会出现这个重定向循环。
每个提示都会有帮助。