Nginx、Docker、Gunicorn和Flask的代理重定向错误

Nginx、Docker、Gunicorn和Flask的代理重定向错误,docker,nginx,flask,https,gunicorn,Docker,Nginx,Flask,Https,Gunicorn,我正在使用Flask、MariaDB、Nginx、Gunicorn和Docker来托管我的网页。我使用4个独立的docker容器(Flask+let's encrypt、Nginx、Gunicorn和MariaDB),网站功能齐全,通过https连接所有请求,但通过以下方式重定向时: 返回重定向(url\u用于('main.home')) 地址不解析为 而是解析为docker-compose.yml文件中指定的gunicorn服务的名称: Gunicorn服务/家庭 注意,我的Flask应用程

我正在使用Flask、MariaDB、Nginx、Gunicorn和Docker来托管我的网页。我使用4个独立的docker容器(Flask+let's encrypt、Nginx、Gunicorn和MariaDB),网站功能齐全,通过https连接所有请求,但通过以下方式重定向时:

返回重定向(url\u用于('main.home'))

地址不解析为

而是解析为docker-compose.yml文件中指定的gunicorn服务的名称:

Gunicorn服务/家庭

注意,我的Flask应用程序是一个名为“main”的Blueprint()。url_for()调用中指定的位置是为“main”指定的路由函数的名称:

@main.route("/home")
def home():
    return render_template('home.html')
我已经尝试过为nginx编辑配置文件,因为我相信这是解决方案所在,因为这可能是一个转发问题。我在SO上发现了一些稍微相似的帖子,其中一篇引用了url\u for()中使用_external=True作为第二个参数,但这只是将地址解析为 mywebsite.com/home,它给出了相同的错误

“无法访问此网站

找不到GunicorService的服务器IP地址

DNS\u探测\u完成\u 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,请执行以下操作:

“”

但它会返回URL:

“Gunicorn服务/主页”


如何调用重定向(url_for())返回我站点的正确页面,例如?

也许,设置会有帮助。

也许,设置会有帮助。

我在另一篇文章中找到了解决方案。在我的代码中插入了更多内容后,我得到了一个400错误,说明“普通HTTP请求被发送到HTTPS端口”,这导致我发了这篇文章:


在这篇文章中,解释了如何插入一个配置规范,将http请求正确地转发到https中。

我在另一篇文章中找到了解决方案。在我的代码中插入了更多内容后,我得到了一个400错误,说明“普通HTTP请求被发送到HTTPS端口”,这导致我发了这篇文章:


在这篇文章中,解释了如何插入一个配置规范,将http请求正确地转发到https中。

我遇到了与这篇文章中描述的类似的问题,但我不得不依赖不同的解决方案

在docker compose文件中,我将nginx端口设置如下

 nginx:
   ports:
   - 1337:80
调用重定向命令时

redirect(url_for('main.home'))
它转发给http://localhost/requested_uri

不http://localhost:1337/requested_uri 正如我所怀疑的那样

解决方案是更改nginx.config文件中的主机定义

location / {
        proxy_set_header   Host $host;
更改为:

location / {
        proxy_set_header   Host $http_host;
不同之处在于$host返回$http_host小写,且不带端口号


使用$http\u host为我解决了重定向问题。

我遇到了与本文所述类似的问题,但我不得不依赖不同的解决方案

在docker compose文件中,我将nginx端口设置如下

 nginx:
   ports:
   - 1337:80
调用重定向命令时

redirect(url_for('main.home'))
它转发给http://localhost/requested_uri

不http://localhost:1337/requested_uri 正如我所怀疑的那样

解决方案是更改nginx.config文件中的主机定义

location / {
        proxy_set_header   Host $host;
更改为:

location / {
        proxy_set_header   Host $http_host;
不同之处在于$host返回$http_host小写,且不带端口号

使用$http_主机为我解决了重定向问题