Flask 中的重定向和 url_for 通过 Gunicorn 和 Nginx 在产品服务器上添加域两次
Redirect and url_for in Flask adds domain twice on prod server via Gunicorn and Nginx
我在产品服务器上的 Flask 中重定向时遇到问题。注意:这不会在本地发生,只有当 运行 在带有 Nginx 和 Gunicorn 的生产服务器上时才会发生。添加 https 和域时出现问题。我不确定问题出在 flask 还是我的 nginx 设置上。
所有重定向都会出现我的问题,但这里有一个示例:return redirect(url_for('.login'))
。这里的预期输出是 "domain.com/login",但它重定向到 "domain.com%2Cdomain.com/login"。当我打印 `url_for('.login') 时,它显示“/login”。
在寻找解决方案时,我发现它可能与我的 nginx 设置有关,所以它们是:
server {
server_name domain.com www.domain.com;
#Fix for socket.io, dont think this is related
location /socket.io {
proxy_pass http://127.0.0.1:8088/socket.io;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8088;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by
Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed
by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name domain.com www.domain.com;
return 404; # managed by Certbot
}
请注意,我用 "domain.com" 替换了我们的域名。
如果我能提供更多信息,请告诉我。
编辑:已解决。我不得不在我的 nginx 配置文件中删除 include proxy_params;
行。我不知道为什么,但它奏效了。现在行为符合预期。我试图将此 post 标记为已解决,但它说我需要等待几天。
好的,我明白了。我必须删除我的 nginx 配置文件中的 include proxy_params;
行。不知道为什么,但之后它按预期工作。
我也遇到了同样的问题。我删除了 include proxy_params;
但它没有解决问题。如果有人能帮助我,我将不胜感激。
这是我的 nginx 配置:
server {
# listen on port 80 (http)
listen 80;
server_name domain.com;
location /.well-known {
root /to/dorectory/where/the/files/are;
}
location / {
# redirect any requests to the same URL but on https
return 301 https://$host$request_uri;
}
}
server {
# listen on port 443 (https)
listen 443 ssl;
server_name domain.com;
# location of the SSL certificate
ssl_certificate /to/fullchain.pem;
ssl_certificate_key /to/privkey.pem;
# write access and error logs to /var/log
access_log /to/mysite_access.log;
error_log /to/mysite_error.log;
location / {
# forward application requests to the gunicorn server
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static {
# handle static files directly, without forwarding to the application
alias /to/static/directory;
}
}
我在产品服务器上的 Flask 中重定向时遇到问题。注意:这不会在本地发生,只有当 运行 在带有 Nginx 和 Gunicorn 的生产服务器上时才会发生。添加 https 和域时出现问题。我不确定问题出在 flask 还是我的 nginx 设置上。
所有重定向都会出现我的问题,但这里有一个示例:return redirect(url_for('.login'))
。这里的预期输出是 "domain.com/login",但它重定向到 "domain.com%2Cdomain.com/login"。当我打印 `url_for('.login') 时,它显示“/login”。
在寻找解决方案时,我发现它可能与我的 nginx 设置有关,所以它们是:
server {
server_name domain.com www.domain.com;
#Fix for socket.io, dont think this is related
location /socket.io {
proxy_pass http://127.0.0.1:8088/socket.io;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8088;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by
Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed
by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name domain.com www.domain.com;
return 404; # managed by Certbot
}
请注意,我用 "domain.com" 替换了我们的域名。
如果我能提供更多信息,请告诉我。
编辑:已解决。我不得不在我的 nginx 配置文件中删除 include proxy_params;
行。我不知道为什么,但它奏效了。现在行为符合预期。我试图将此 post 标记为已解决,但它说我需要等待几天。
好的,我明白了。我必须删除我的 nginx 配置文件中的 include proxy_params;
行。不知道为什么,但之后它按预期工作。
我也遇到了同样的问题。我删除了 include proxy_params;
但它没有解决问题。如果有人能帮助我,我将不胜感激。
这是我的 nginx 配置:
server {
# listen on port 80 (http)
listen 80;
server_name domain.com;
location /.well-known {
root /to/dorectory/where/the/files/are;
}
location / {
# redirect any requests to the same URL but on https
return 301 https://$host$request_uri;
}
}
server {
# listen on port 443 (https)
listen 443 ssl;
server_name domain.com;
# location of the SSL certificate
ssl_certificate /to/fullchain.pem;
ssl_certificate_key /to/privkey.pem;
# write access and error logs to /var/log
access_log /to/mysite_access.log;
error_log /to/mysite_error.log;
location / {
# forward application requests to the gunicorn server
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static {
# handle static files directly, without forwarding to the application
alias /to/static/directory;
}
}