Recently, I encountered a problem when using nginx reverse proxy service, that is, I want to specify the source IP address to realize the forwarding of requests, but I have not found a better solution. After some exploration and practice, I summarized some experiences and tips, and I hope to share them with you.
In order to achieve the purpose of specifying the source IP address, we first need to understand what nginx reverse proxy and source IP address are. nginx reverse proxy refers to receiving requests on the Internet through a proxy server, and then forwarding these requests to a server on the internal network and re-transmitting the content returned from the server to the client of the Internet request. The source IP address is the real IP address of the client making the request.
Configuring the nginx reverse proxy to specify an IP address requires the following considerations:
How to get the real IP address of the client
Configuring the nginx reverse proxy
Specify source IP address forwarding
Get the client's real IP address
In nginx reverse proxy, the presence of proxy server will cause the IP address of the original request to be hidden, so we need to get the real IP address of the client through some special header fields. In the configuration of nginx, we can pass the real IP address of the client to the back-end server by setting some http request header fields. The specific configuration is as follows:
"`nginx
server {
listen 80.
server_name example.com.
location / {
proxy_pass http://backend_server.
proxy_set_header X-Real-IP $remote_addr.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
“`
In the configuration above, we set two http header fields, X-Real-IP and X-Forwarded-For, via the proxy_set_header directive to pass the client's real IP address and the proxy server's IP address, respectively.
Configuring the nginx reverse proxy
Configuring the nginx reverse proxy is very simple, just add a proxy_pass directive to the nginx configuration file. Here is a simple example:
"`nginx
server {
listen 80.
server_name example.com.
location / {
proxy_pass http://backend_server.
}
}
“`
In the above configuration, we forwarded the client's request for example.com to the backend_server via reverse proxy.
Specify source IP address forwarding
To implement forwarding with a specified source IP address, we can use the nginx module ngx_http_realip_module to modify the source IP address of the request. The specific configuration is as follows:
"`nginx
set_real_ip_from 192.168.1.0/24.
real_ip_header X-Real-IP.
real_ip_recursive on;
“`
In the configuration above, we specify the network segment where the source IP address is allowed to be modified via the set_real_ip_from directive, and then specify the HTTP header field of the source IP address to be modified via the real_ip_header directive. Finally, the real_ip_recursive directive enables recursive lookup of requests from the proxy server to ensure that the client's real IP address is correctly obtained.
summarize
With the above configuration and settings, we can realize the forwarding of the specified source IP address in nginx reverse proxy. First we get the real IP address of the client by setting the http header field, then we configure the reverse proxy and use the ngx_http_realip_module module to realize the forwarding of the specified source IP address. I hope the above experience and tips will be helpful to you.