Recently, nginx was installed on the server where the application website was deployed, as a bridge between the client and the application server. For web applications, the client of this HTTP request is Nginx instead of the real client browser. If no special processing is done, the web application will treat Nginx as the requesting client, and the obtained client information is Some information about Nginx.
Now you need to configure Ngnix:
Add the following configuration to each location of the proxy:
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_set_header X-Forwarded-Proto $scheme;
1.
Host
Contains the real domain name and port number of the client;
2.
X-Forwarded-Proto
Indicates the real protocol of the client (http or https);
3.
X-Real-IP
Indicates the real IP of the client;
4. X-Forwarded-For
This Header X-Real-IP
is similar, but it will contain the IP of the real client and each proxy server in the middle of the multi-layer proxy.
E.g:
#Mainly used to set a group of proxy servers that can be used in proxy_pass and fastcgi_pass directives. The default load balancing method is polling
upstream tomcat_client {server 127.0.0.1:8080;
} #Enable
gzip compression, after opening, the webpage will be automatically compressed
#gzip on; #Specify
the name and parameters of the
server server {
listen 80;
server_name http://;
location / {
proxy_pass http://tomcat_client;
proxy_redirect off; #Set
proxy
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme ;
}
}
It is found that changing to the above configuration is still unable to obtain the real ip of the user. Tomcat needs to be configured to solve.
request.getHeader("X-Forwarded-For")
orrequest.getHeader("X-Real-IP")或request.getRemoteAddr()获取用户ip。
As an application server, Tomcat can be configured with Tomcat's server.xml file and added at the end of the Host element:
<Valve className="org.apache.catalina.valves.RemoteIpValve" />