Redirect 通过Gunicorn和Nginx在prod服务器上两次为in Flask添加域的重定向和url_
我在prod服务器上的Flask中重定向时遇到问题。注意:只有在使用Nginx和Gunicorn的prod服务器上运行时,才会在本地发生这种情况。当添加https和域时,问题开始出现。我不确定问题是出在flask还是我的nginx设置上 所有重定向都会出现我的问题,但下面是一个示例:Redirect 通过Gunicorn和Nginx在prod服务器上两次为in Flask添加域的重定向和url_,redirect,nginx,flask,gunicorn,url-for,Redirect,Nginx,Flask,Gunicorn,Url For,我在prod服务器上的Flask中重定向时遇到问题。注意:只有在使用Nginx和Gunicorn的prod服务器上运行时,才会在本地发生这种情况。当添加https和域时,问题开始出现。我不确定问题是出在flask还是我的nginx设置上 所有重定向都会出现我的问题,但下面是一个示例:返回重定向(url\u表示('.login'))。此处的预期输出为“domain.com/login”,但它会重定向到“domain.com%2Cdomain.com/login”。当我打印('.login')的“u
返回重定向(url\u表示('.login'))
。此处的预期输出为“domain.com/login”,但它会重定向到“domain.com%2Cdomain.com/login”。当我打印('.login')的“url”时,它显示“/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”
如果我能提供更多信息,请告诉我
编辑:解决了。我不得不删除行
include proxy_params代码>在我的nginx配置文件中。我不知道为什么,但它奏效了。现在行为如预期。我试图将此帖子标记为已解决,但它说我需要等待几天 好吧,我想起来了。我不得不删除行include proxy_params代码>在我的nginx配置文件中。我不知道为什么,但后来它果然起了作用。我也有同样的问题。我删除了include代理参数代码>但它没有解决问题。如果有人能帮我,我将不胜感激。
以下是我的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;
}
}
我删除了此处解释的
的
$proxy\u add\u x\u forwarded\u,问题解决了