Skip to content

CodeCraft Hub

Getting the Actual Client IP Address in Nginx Reverse Proxy

DevOps, Server1 min read

...

When using Nginx as a reverse proxy server, obtaining the actual client's IP address can be a bit complex. By default, Nginx records the remote address of requests forwarded through a proxy pass. However, to acquire the real client's IP address, several configurations are necessary.

One approach is to use the X-Real-IP header to transmit the client's actual IP address. Below is an example implementation in the Nginx configuration file:

In this setup, proxy_set_header X-Real-IP $remote_addr; instructs Nginx to add the client's actual IP address to the X-Real-IP header.

Subsequently, on the server side, you can extract the actual client IP using this header. For instance, in a Node.js server:

This way, you can retrieve the actual client IP address from requests forwarded through Nginx's reverse proxy server.

Bonus Tip: Nginx Reload vs Restart

When restarting Nginx, it's crucial to understand the difference between reload and restart. The reload command re-reads the configuration file without stopping the web server, maintaining its current state even if there are issues. Conversely, restart involves stopping and then restarting Nginx, which may lead to downtime if problems arise during the restart process.

Remember to validate the configuration syntax with nginx -t before reloading to ensure a smooth transition.

Now, let's continue exploring and optimizing your Nginx setup for a seamless web-serving experience!

© 2024 by CodeCraft Hub. All rights reserved.
Powered by Gatsby