Nginx varnish nginx php-fpm: real ip for php’s _SERVER[‘REMOTE_ADDR’]

nginxphp-fpmvarnish

This is specific question.

A Nginx server (call it N1) listens on :80 and forwards to varnish with proxy_pass
Varnish listens on 127.0.0.1:6081 and forwards to Nginx (N2) on 8080.
N2 talks to the php-fpm socket.

N1<>V<>N2<>P

N1:

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

    proxy_pass http://varnish/;
    proxy_redirect off;
}

currently

$_SERVER['REMOTE_ADDR'] == '127.0.0.1'

desired

$_SERVER['REMOTE_ADDR'] == 'The real remote addr'

Best Answer

This is a specific answer. ;)

You could add a x-forwarded-for in N1, let that pass through varnish and N2 to fastcgi:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Then in fastcgi params:

fastcgi_param REMOTE_ADDR $http_x_forwarded_for;