简体   繁体   English

nginx:如何获取实际客户端 ip?

[英]nginx: how to get the actual client ip?

I have a simple express app.我有一个简单的快递应用程序。 which is Dockerized .这是Dockerized This is Repository .这是存储库

I used nginx as a reverse proxy there.我在那里使用nginx作为反向代理。 when I visit http://45.33.97.232:3000 , it gives me the actual IP .当我访问http://45.33.97.232:3000时,它给了我实际的IP But, when I visit http://45.33.97.232/ , It gives me the same server IP .但是,当我访问http://45.33.97.232/时,它给了我相同的服务器IP But I need actual Client IP here.但我在这里需要实际Client IP And I am using Server IP in nginx config file.我在nginx配置文件中使用服务器 IP but I have a restriction, I can't write Server IP in nginx config file.但我有一个限制,我不能在nginx配置文件中写入服务器 IP

This is my nginx config file,这是我的nginx配置文件,

server {
    listen 80 default_server;
    server_name 45.33.97.232;
location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://45.33.97.232:3000; #port where you are serving your express app.
  }
}

In nginx if you want to pass through the IP address of the remote user to your backend web server you have to set X-Forwarded-For header to this remote ip, Like this: In nginx if you want to pass through the IP address of the remote user to your backend web server you have to set X-Forwarded-For header to this remote ip, Like this:

proxy_set_header X-Forwarded-For $remote_addr;

and if you do not want to use server IP you can use the domain in your Nginx config file.如果您不想使用服务器 IP,您可以使用 Nginx 配置文件中的域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM