来自Nginx反向代理服务器而不是客户端的REMOTE_ADDR IP

时间:2018-08-29 10:52:03

标签: http-headers nginx-reverse-proxy

我的问题与这个问题完全相反:REMOTE_ADDR IP from user instead off Nginx reverse proxy server

让我解释一下。我在Nginx反向代理后面有一个Web服务器(Apache)。当我通过代理连接到Web服务器时,它会添加一些标头(应有),但是REMOTE_ADDR标头包含客户端的IP而不是代理的IP,即使我没有在nginx配置中覆盖它。

这是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/;
}

这是var_dump($ _ SERVER);的输出; 代替

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

我会期望的

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

完整输出:

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) "

我还感到奇怪的是,仅当我手动将X-Real-IP标头设置为

时,它才出现在var_dump中。
proxy_set_header X-Real-IP "test";

尝试手动设置REMOTE_ADDR也不起作用 有提示吗?

仅此而已:我正在使用openmediavault nginx服务器,并且apache在docker映像nextcloud:latest的实例中运行

0 个答案:

没有答案