2

My problem is the exact opposite of this question: REMOTE_ADDR IP from user instead off Nginx reverse proxy server

Let me explain. I have a webserver (apache) behind an nginx reverse proxy. When I connect to the webserver via the proxy, it adds some headers (as it should), but the REMOTE_ADDR header contains the client's IP instead of the proxy's, even though I don't override it in my nginx configuration.

Here is that part of the nginx conffile:

location / {
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
    add_header Referrer-Policy "same-origin";

    proxy_cookie_path / "/; secure; HttpOnly";

    proxy_set_header Host $http_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;

    # Even uncommenting the next line has NO EFFECT!
    #proxy_set_header REMOTE_ADDR "172.17.0.1";

    client_max_body_size 2g;

    proxy_pass http://172.17.0.2:80/;
}

And this is the output of var_dump($_SERVER); Instead of

["REMOTE_ADDR"]=> string(14) "1.2.3.4"

I would have expected

["REMOTE_ADDR"]=> string(14) "172.17.0.1"

Full output:

Apache/2.4.25 (Debian) Server at my.dedyn.io Port 80
" ["SERVER_SOFTWARE"]=> string(22) "Apache/2.4.25 (Debian)"
["SERVER_NAME"]=> string(16) "my.dedyn.io"
["SERVER_ADDR"]=> string(10) "172.17.0.2"
["SERVER_PORT"]=> string(2) "80"
["REMOTE_ADDR"]=> string(14) "1.2.3.4" ### CLIENT IP ###
["DOCUMENT_ROOT"]=> string(13) "/var/www/html"
["REQUEST_SCHEME"]=> string(4) "http"
["CONTEXT_PREFIX"]=> string(0) ""
["CONTEXT_DOCUMENT_ROOT"]=> string(13) "/var/www/html"
["SERVER_ADMIN"]=> string(19) "webmaster@localhost"
["SCRIPT_FILENAME"]=> string(23) "/var/www/html/index.php"
["REMOTE_PORT"]=> string(5) "49894"
["REDIRECT_URL"]=> string(23) "/settings/admin/logging"
["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=> string(8) "HTTP/1.0"
["REQUEST_METHOD"]=> string(3) "GET"
["QUERY_STRING"]=> string(0) ""
["REQUEST_URI"]=> string(23) "/settings/admin/logging"
["SCRIPT_NAME"]=> string(10) "/index.php"
["PHP_SELF"]=> string(10) "/index.php"
["REQUEST_TIME_FLOAT"]=> float(1535537778.731)
["REQUEST_TIME"]=> int(1535537778)
["argv"]=> array(0) { }
["argc"]=> int(0) } array(47) {
["REDIRECT_HTTP_AUTHORIZATION"]=> string(0) ""
["REDIRECT_PATH_INFO"]=> string(0) ""
["REDIRECT_htaccessWorking"]=> string(4) "true" 
["REDIRECT_front_controller_active"]=> string(4) "true" 
["REDIRECT_STATUS"]=> string(3) "200"
["HTTP_AUTHORIZATION"]=> string(0) ""
["PATH_INFO"]=> string(0) ""
["htaccessWorking"]=> string(4) "true"
["front_controller_active"]=> string(4) "true"
["HTTP_HOST"]=> string(16) "my.dedyn.io"
["HTTP_X_FORWARDED_FOR"]=> string(14) "1.2.3.4" ### CLIENT IP ###
["HTTP_X_FORWARDED_PROTO"]=> string(5) "https"
["HTTP_CONNECTION"]=> string(5) "close"
["HTTP_USER_AGENT"]=> string(76) "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"
["HTTP_ACCEPT"]=> string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-GB,en;q=0.5"
["HTTP_ACCEPT_ENCODING"]=> string(17) "gzip, deflate, br"
["HTTP_COOKIE"]=> string(334) "__Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true; nc_sameSiteCookielax=true; nc_sameSiteCookiestrict=true; '''long string'''"
["HTTP_DNT"]=> string(1) "1"
["HTTP_UPGRADE_INSECURE_REQUESTS"]=> string(1) "1"
["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0"
["PATH"]=> string(60) "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
["SERVER_SIGNATURE"]=> string(77) "

What I also find curious is that the X-Real-IP header does not appear in the var_dump, only if I set it manually to, say

proxy_set_header X-Real-IP "test";

Trying to set the REMOTE_ADDR manually also does not work Any hints?

Just to be complete: I'm using the openmediavault nginx server and the apache runs in an instance of the docker image nextcloud:latest

Daniel
  • 112
  • 1
  • 8
  • The whole point of this `proxy_set_header X-Real-IP $remote_addr;` is to pass the client address upstream. What happens if you change it to `proxy_set_header X-Real-IP $server_addr;` or delete it altogether? – miknik Aug 29 '18 at 18:33
  • @miknik ok, now it gets weird: when I delete it a new header appears: `["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0"` and REMOTE_ADDR changes: `["REMOTE_ADDR"]=> string(10) "172.17.0.1"` which is the value I expected in the first place! When I set it to `$server_addr;` the same happens, in addition the header `["HTTP_X_REAL_IP"]=> string(12) "192.168.0.49"` appears (which is the LAN IP of the proxy server). I don't understand though, why does the X-Real-IP header influence the REMOTE_ADDR header? – Daniel Aug 30 '18 at 06:41
  • Because its the header used to pass the client ip address through the reverse proxy. REMOTE_ADDR isn't a header, it's one of the variables in the $_SERVER array within php – miknik Aug 30 '18 at 08:13

0 Answers0