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

通过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

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页