Restore Real IP in Nginx Reverse Proxy

Compile Nginx with real ip module. The origin server will then be able to get the real ip address from the proxy server.

Origin Server:

Add the following into conf file. Put it inside server{} but outside location{}.

    real_ip_header    X-Forwarded-For;
    real_ip_recursive on;

Proxy Server:

location / {
      proxy_pass         https://xxx;
      proxy_set_header   Host              $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;
      proxy_redirect     off;

You can further secure the connection by only allowing proxy server to access origin server.



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.