您当前的位置: 首页 >  http

彭世瑜

暂无认证

  • 0浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Nginx转发到Flask应用scheme获取不到https

彭世瑜 发布时间:2020-10-12 15:32:38 ,浏览量:0

通过Nginx配置转发到Flask应用

server {
    listen 80;

    server_name _;

    access_log  /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    location / {
        proxy_pass         http://127.0.0.1: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-Proto    $scheme;
    }
}

配置中明明已经有$scheme 字段配置了,Flask中只能获取到http,而不是https

还需要在Flask应用中加入以下代码才能生效

from werkzeug.middleware.proxy_fix import ProxyFix

app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)

参考 https://stackoverflow.com/questions/23347387/x-forwarded-proto-and-flask

https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/#proxy-setups

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0578s