【问题标题】:Gunicorn doesn't log real ip from nginxGunicorn 不会从 nginx 记录真实 ip
【发布时间】:2014-11-02 11:01:52
【问题描述】:

我通过 gunicorn、supervisor 和 nginx 作为反向代理运行 django 应用程序,并且努力让我的 gunicorn 访问日志显示实际 ip 而不是 127.0.0.1:

目前的日志条目如下所示:

127.0.0.1 - - [09/Sep/2014:15:46:52] "GET /admin/ HTTP/1.0" ...

supervisord.conf

[program:gunicorn]
command=/opt/middleware/bin/gunicorn --chdir /opt/middleware -c /opt/middleware/gunicorn_conf.py middleware.wsgi:application 
stdout_logfile=/var/log/middleware/gunicorn.log

gunicorn_conf.py

#!python
from os import environ
from gevent import monkey
import multiprocessing
monkey.patch_all()

bind = "0.0.0.0:9000"
x_forwarded_for_header = "X-Real-IP"
policy_server = False
worker_class = "socketio.sgunicorn.GeventSocketIOWorker"   
accesslog = '-'

我的 nginx 模块配置

server {
    listen 80;
    root /opt/middleware;
    index index.html index.htm;
    client_max_body_size 200M;
    server_name _;

    location / {
        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_redirect off;
        real_ip_header X-Real-IP;

      }
    }

我在 location {} 块中尝试了各种组合,但看不出有什么不同。任何提示表示赞赏。

【问题讨论】:

    标签: django nginx gunicorn supervisord


    【解决方案1】:

    问题是你需要配置gunicorn's logging,因为它(默认情况下)不会显示任何自定义标题。

    从文档中我们发现默认的访问日志格式是由access_log_format控制的,设置如下:

    "%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
    

    地点:

    • h 是远程地址
    • l-(未使用)
    • u-(未使用,保留)
    • t 是时间戳
    • r 是状态行
    • s 是请求的状态
    • b 是响应长度
    • f 是推荐人
    • a 是用户代理

    您还可以使用以下默认不使用的额外变量对其进行自定义:

    • T - 请求时间(以秒为单位)
    • D - 请求时间(以微秒为单位)
    • p - 进程 ID
    • {Header}i - 请求标头(自定义)
    • {Response}o - 响应头(自定义)

    对于gunicorn,所有请求都来自 nginx,因此它将显示为远程 IP。要让它记录任何自定义标头(您从nginx 发送的内容),您需要调整此参数并添加适当的变量,在您的情况下,您可以将其设置为以下内容:

    %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" "%({X-Real-IP}i)s"
    

    【讨论】:

    • 谢谢,我以为它会替换 %(h) 的值。效果很好,但我不明白为什么它总是在我放置 {X-Real-IP} 的地方插入换行符:-/
    • 这些都不适合我。注意:上面的字符串需要用单引号括起来,否则 bash 不会在参数列表中接受它。最后我使用了uWsgi。我从 gunicorn 开始,因为我认为它很简单,但是当您设置 supervisord 并且必须处理它而不是轻松地将信息传递给 django 时,uwsgi 是一个更好的选择。
    • 如果你想用它作为一个命令标志你应该使用--access-logformat
    【解决方案2】:

    请注意,包含- 的标头应在此处通过将- 替换为_ 来引用,因此X-Forwarded-For 变为%({X_Forwarded_For}i)s

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 2016-12-05
      • 2019-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      相关资源
      最近更新 更多