Nginx 真实 IP 日志记录不起作用?

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.1remote_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'

有什么想法我做错了吗?

答案1

该设置set_real_ip_from 192.168.2.1意味着 nginx 将只信任X-Forwarded-For从该 IP 地址发送的标头。在您的测试中,标头来自 127.0.0.1,因此 nginx 会忽略该标头。

相关内容