【问题标题】:Proxy redirect errors with Nginx, Docker, Gunicorn, and FlaskNginx、Docker、Gunicorn 和 Flask 的代理重定向错误
【发布时间】: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'))

地址不解析为

https://mywebsite.com/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'))

返回网址:

https://mywebsite.com/home

但是,它返回的是 URL:

“gunicornservice/home”

如何调用redirect(url_for()) 返回我网站的正确页面,例如https://mywebsite.com/home

【问题讨论】:

    标签: docker nginx flask https gunicorn


    【解决方案1】:

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

    在我的 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_host 为我解决了重定向问题。

    【讨论】:

    • 伟大的电话,试图调试这个几个小时。这为我解决了这个问题。
    【解决方案2】:

    也许,设置SERVER_NAME 会有所帮助。

    【讨论】:

      【解决方案3】:

      我在另一篇关于 SO 的帖子中找到了解决方案。在我的代码中插入更多内容后,我收到了 400 错误,指出“普通 HTTP 请求已发送到 HTTPS 端口”,这导致我看到了这个 SO 帖子:Dealing with nginx 400 "The plain HTTP request was sent to HTTPS port" error

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

      【讨论】:

        猜你喜欢
        • 2017-05-11
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        • 2019-03-11
        • 1970-01-01
        • 1970-01-01
        • 2021-01-29
        相关资源
        最近更新 更多