【问题标题】:Cannot get headers in flask app through nginx-gunicorn无法通过 nginx-gunicorn 在烧瓶应用程序中获取标头
【发布时间】:2020-08-21 03:26:26
【问题描述】:

我正在尝试获取访问该站点的用户的真实 IP 地址。 为此,我想为 API 服务添加自定义授权。在这两种情况下使用标头似乎是个好主意。

问题是我无法在由 nginx 和 gunicorn 网络服务器提供的烧瓶应用程序中获取自定义标头。获取“remote_addr”标头仅返回 127.0.0.1 IP 地址,尽管我是从外部网络发布的,而不是从本地主机发布的。

这是我的配置文件:

部分 nginx 配置 //1.14.2

location / {
        # forward application requests to the gunicorn server
        proxy_read_timeout 30s;
        proxy_send_timeout 30s;
        proxy_connect_timeout 30s;
        proxy_pass http://127.0.0.1:5000;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        add_header CLIENT-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

gunicorn 配置的一部分 //20.0.4

[program:wapp]
command=/home/deeslo/processor/venv/bin/gunicorn -b localhost:5000 -w 4 microservice:app
directory=/home/deeslo/processor
user=deeslo
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true

当我检查标头时,我看不到来自 Nginx 的 X-Real-IP/CLIENT-IP 以及我在烧瓶中发送的其他自定义标头生成响应标头。

我错过了什么? 还没解决。

【问题讨论】:

    标签: nginx flask gunicorn


    【解决方案1】:

    哦,那完全是我的错。

    问题是我在我的 IDE 中调试应用程序,该应用程序通过嵌入式 Web 服务器而不是 nginx 运行。

    上面的解决方案完美,我可以在flask应用程序中获取用户IP地址:

    request.header['X-Real-IP']

    【讨论】:

      猜你喜欢
      • 2017-06-06
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2018-05-27
      • 1970-01-01
      • 2020-04-16
      • 2017-12-25
      相关资源
      最近更新 更多