nginx 在请求IP头中获取127.0.0.1

htzpubme  于 5个月前  发布在  Nginx
关注(0)|答案(1)|浏览(84)

我有一个NestJS服务器,部署在一个EC2示例上,使用nginx。我想获取客户端的IP地址,但我在每个请求中都得到“127.0.0.1“作为IP,无论它来自哪里。
我已经尝试直接从请求头中获取IP,并从nestjs中使用@IP装饰器。但仍然获得相同的IP。
我也试过在nginx的配置中设置header。

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://127.0.0.1:5001;
                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;
        }

字符串

9wbgstp7

9wbgstp71#

main.tsbootstrap方法中,将此行添加到app.listen()之前

app.getHttpAdapter().getInstance().set("trust proxy", true);

字符串
这将告诉express信任代理IP,并允许您正确使用req.ip@Ip()装饰器

相关问题