如何使用 FastAPI 日志记录捕获 X-Forwarded-For?

问题描述 投票:0回答:2

我正在像这样的函数中使用 guvicorn 运行 FastApi:

if __name__ == "__main__":

    uvicorn.run(
        app="app.main:app",
        host="HOSTIP",
        port=8000,
        reload=True,
        # log_config=None,
        log_config=log_config,
        log_level="info"
    )

这就是我的 log_config 的样子:

log_config = {
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "default": {
            "()": "uvicorn.logging.DefaultFormatter",
            "fmt": "%(asctime)s::%(levelname)s::%(name)s::%(filename)s::%(funcName)s - %(message)s",
            "use_colors": None,
        },
        "access": {
            "()": "uvicorn.logging.AccessFormatter",
            "fmt": '%(asctime)s::%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s',
        },
    },
    "handlers":
    {
        "default":
        {
            "formatter": "default",
            # "class": 'logging.NullHandler',
            "class": 'logging.FileHandler',
            "filename": CONFIG[SECTION]["default"]
        },
        "error":
        {
            "formatter": "default",
            # "class": 'logging.NullHandler',
            "class": 'logging.FileHandler',
            "filename": CONFIG[SECTION]["error"]
        },
        "access":
        {
            "formatter": "access",
            # "class": 'logging.NullHandler',
            "class": 'logging.FileHandler',
            "filename": CONFIG[SECTION]["access"]
        },
    },
    "loggers":
    {
        "uvicorn": {"handlers": ["default"], "level": "INFO", "propagate": False},
        "uvicorn.error": {"handlers": ["error"], "level": "ERROR", "propagate": False},
        "uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False},
    }
}

我在 2 台服务器上有 2 个 fastapi 实例,在 haproxy 后面运行。我能够在 haproxy 中添加此选项,将客户端 IP 转发到我的 API:

option forwardfor

我能够通过其中一台 API 服务器上的 TCPDUMP 确认我实际上收到了一些 x-fwd 标头:

[user@server ~]# tcpdump -i INTERFACE host SERVERIP -AAA | grep -i IP OF MY LAPTOP
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on INTERFACE, link-type EN10MB (Ethernet), capture size 262144 bytes
*X-Forwarded-For: IP OF MY LAPTOP*

但是在我的日志中,我只看到请求命中的vip的IP,即使HAproxy正在转发客户端的IP,我也无法记录它。

是否有一个自定义变量可以用于 log_config 访问部分?

谢谢。

python-3.x haproxy fastapi uvicorn
2个回答
10
投票

好吧,我明白了。

我必须在 start.py 中包含两件事:

if __name__ == "__main__":

    uvicorn.run(
        app="app.main:app",
        host="HOSTIP",
        port=8000,
        reload=True,
        proxy_headers=True, # THIS LINE
        forwarded_allow_ips='*', # THIS LINE
        log_config=log_config,
        log_level="info"
    )

7
投票

您可以使用

proxy_headers=True
填充远程地址信息:

来自文档:

--proxy-headers / --no-proxy-headers
                                  Enable/Disable X-Forwarded-Proto,
                                  X-Forwarded-For, X-Forwarded-Port to
                                  populate remote address info.

https://www.uvicorn.org/deployment/#running-from-the-command-line

© www.soinside.com 2019 - 2024. All rights reserved.