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{}.

    set_real_ip_from  13.124.23.35;
    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.

References:

  1. http://www.ttlsa.com/nginx/nginx-get-user-real-ip/
  2. http://www.nginx.cn/4646.html

Leave a Comment

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