This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
The server obtains the real client IP, which is often required in daily projects. If we use the reverse proxy service, it is likely that the real client IP cannot be obtained in some special cases. At this point we need to add some configuration to the Nginx reverse proxy service to provide the real client IP.
Before configuration, we need to talk about the main types of built-in variables in Nginx, and what values are placed and what effects they have.
Nginx built-in variables
$arg_name: the name of the parameter in the request parameter, if? $arg_name =123; $arg_name =123;
$args: The value of the parameter in the request parameter.
$content_LENGTH: The content_length of the request header.
$content_type: The content_type of the request header.
$cookie_name: indicates the cookie name.
$hostname: indicates the hostname.
$remote_addr: client address, remember this parameter, we will use it later.
$remote_port: indicates the client port.
$remote_user: specifies the user name for the HTTP basic authentication service.
$request: indicates the request address of the client.
$server_addr: indicates the address of the server.
$server_name: indicates the server name, such as the domain name.
$server_port: indicates the port of the server.
Obtain the real IP address of the client
We usually get the client IP from the Java backend using request.getremoteAddr (), but if we use Nginx or another reverse proxy server.
This method will get an inaccurate value or not, so we need to do some “manipulation” on the reverse proxy server.
To do that, this is where our nginx built-in variables come in. Take a look.
$remote_addr: client address;
This parameter represents the address from which you can access the Nginx reverse proxy service from the client. Passing this parameter to the server is ok.
The configuration is as follows:
server{ ....... I'm not going to write that. I'm just going to write the main points. location / { proxy_set_header X-Real-IP $remote_addr; proxy_pass http:www.test.com; }}Copy the code
With this configuration, we can use request.getheader (‘ x-real-ip ‘) to fetch it in the background;