【发布时间】:2019-09-24 16:06:49
【问题描述】:
我正在使用 Flask、MariaDB、Nginx、Gunicorn 和 Docker 来托管我的网页。我正在使用 4 个独立的 docker 容器(Flask + let's encrypt、Nginx、Gunicorn 和 MariaDB),并且网站功能齐全,通过 https 连接所有请求,但重定向时通过:
return redirect(url_for('main.home'))
地址不解析为
而是解析为我的 gunicorn 服务的名称,在我的 docker-compose.yml 文件中指定:
gunicornservice/home
注意,我的 Flask 应用是一个名为“main”的 Blueprint()。 url_for() 调用中指定的位置是为 'main' 指定的路由函数的名称:
@main.route("/home")
def home():
return render_template('home.html')
我已经尝试编辑 nginx 的配置文件,因为我相信这是解决方案所在,因为这可能是一个转发问题。我在 SO 上发现了一些类似的帖子,其中一个使用 _external=True 作为 url_for() 中的第二个参数引用,但这仅将地址解析为 mywebsite.com/home,它给出了同样的错误,
“无法访问此站点
gunicornservice 的服务器 IP 地址找不到。
DNS_PROBE_FINISHED_NXDOMAIN"
我的 Nginx 配置文件:
events { }
http {
upstream upstream-web {
server jonathanolson.us;
}
server {
listen 80;
server_name gunicornservice;
location / {
root /NHL-Project/flasksite/static;
proxy_pass http://gunicornservice:8000;
proxy_redirect 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_set_header X-Forwarded-Host $server_name;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/jonathanolson.us/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/jonathanolson.us/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 = jonathanolson.us) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name jonathanolson.us;
return 404; # managed by Certbot
}
}
我期待调用的结果
return redirect(url_for('main.home'))
返回网址:
但是,它返回的是 URL:
“gunicornservice/home”
如何调用redirect(url_for()) 返回我网站的正确页面,例如https://mywebsite.com/home?
【问题讨论】:
标签: docker nginx flask https gunicorn