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://*.com/questions/23347387/x-forwarded-proto-and-flask


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


上一篇:《企业大数据系统构建实战:技术、架构、实施与应用》一第1章 企业大数据战略定位1.1 宏观


下一篇:7种你应该知道的JavaScript常见的错误