ngingx proxy express get real ip

To get the real IP address of a client making a request through an NGINX proxy to an Express server, you can follow the steps below:

  1. Configure NGINX to Forward Real IP: In the NGINX configuration, set the proxy_set_header directive to forward the real IP address of the client. Use the $remote_addr variable to capture the original client IP.

  2. Set Express to Trust NGINX Proxy: In the Express server setup, use the app.set('trust proxy', true) method to make it trust the headers set by the NGINX proxy server.

  3. Access Real IP in Express: Once the Express server trusts the NGINX proxy, it can access the real IP address of the client from the X-Forwarded-For or X-Real-IP headers, which were set by NGINX.

By following these steps, the Express server can accurately retrieve the real IP address of the client even when requests are made through an NGINX proxy.