Nginx
Nginx 真實 IP 日誌記錄不起作用?
我有以下 nginx 配置:
worker_processes auto; daemon off; error_log /dev/stdout warn; include /etc/nginx/modules/*.conf; events { } http { real_ip_header X-Forwarded-For; real_ip_recursive on; set_real_ip_from 192.168.2.1; server_tokens off; default_type application/octet-stream; log_format json_combined escape=json '{' '"remote_addr":"$remote_addr",' '"request":"$request",' '"real_ip": "$realip_remote_addr",' '"http_x_real_ip": "$http_x_real_ip"' '}'; access_log /dev/stdout json_combined; server { location / { root /content/; } } include /etc/nginx/conf.d/*.conf; }
我的期望是該
realip_remote_addr
變數應該在X-Forwarded-For
標題中具有最後一個不受信任的地址。但是,當我提出這樣的請求時:$ curl -v --header "X-Forwarded-For:10.1.1.1,192.168.2.1" localhost
日誌顯示
127.0.0.0.1
和remote_addr
變數realip_remote_addr
。這是我的 nginx 版本:
nginx version: nginx/1.16.0 built by gcc 9.1.1 20190503 (Red Hat 9.1.1-1) (GCC) built with OpenSSL 1.1.1b FIPS 26 Feb 2019 (running with OpenSSL 1.1.1c FIPS 28 May 2019) TLS SNI support enabled configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
任何想法我做錯了什麼?
該設置
set_real_ip_from 192.168.2.1
意味著 nginx 將僅信任X-Forwarded-For
從該 IP 地址發送的標頭。在您的測試中,標頭來自 127.0.0.1,因此 nginx 會忽略該標頭。