Python 3.x 有没有办法使用Python后端服务器检索客户端IP地址

Python 3.x 有没有办法使用Python后端服务器检索客户端IP地址,python-3.x,gunicorn,fastapi,starlette,uvicorn,Python 3.x,Gunicorn,Fastapi,Starlette,Uvicorn,有问题的设置如下所示: 我的web应用程序使用fastapi实现,并使用gunicorn和uvicorn工作者类部署,位于IP地址为172.31.x.x的同一主机上的nginx代理之后(以及其他远程设备,如VPN集中器等) nginx的配置如下: location / { real_ip_header X-Forwarded-For; real_ip_recursive on; set_real_ip_from 172.31.x.x/32; # well-known v

有问题的设置如下所示:

我的web应用程序使用
fastapi
实现,并使用
gunicorn
uvicorn
工作者类部署,位于IP地址为
172.31.x.x
的同一主机上的
nginx
代理之后(以及其他远程设备,如VPN集中器等)

nginx
的配置如下:

location / {
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    set_real_ip_from 172.31.x.x/32;  # well-known vpn concentrator

    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
    proxy_pass http://172.31.x.x:5045;
OPTIONS="--bind 127.0.0.1:5045 --bind 172.31.x.x:5045 --forwarded-allow-ips=127.0.0.1,172.31.x.x --workers 1 --worker-class uvicorn.workers.Uv
icornWorker --log-config config/logging.conf"
gunicorn
的配置如下:

location / {
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
    set_real_ip_from 172.31.x.x/32;  # well-known vpn concentrator

    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
    proxy_pass http://172.31.x.x:5045;
OPTIONS="--bind 127.0.0.1:5045 --bind 172.31.x.x:5045 --forwarded-allow-ips=127.0.0.1,172.31.x.x --workers 1 --worker-class uvicorn.workers.Uv
icornWorker --log-config config/logging.conf"
fastapi
中,使用
starlette.requests.Request
对象(名为
Request
),
Request.client.host
打印承载web应用的服务器的接口IP地址(即
172.31.x

request.headers[“x-real-ip”]、request.headers[“x-forwarded-for”]
都会在我的代理之前打印设备的ip地址,我的代理是我公司众所周知的防火墙设备

我想问的是:

  • 是否可以打印整个
    X-Forwarded-For
    HTTP头以查看中间代理服务
  • 如何检索最终用户的真实客户端IP地址(基本上覆盖了众所周知的白名单IP地址)