Nginx:反向代理将客户端IP传递给服务器

xCo*_*lus 5 nginx reverse-proxy

在现有的 nginx 反向代理上,我需要将客户端 IP 传递给服务器(从 nginx 到 NG-Engine 中服务的前端,然后从这个到后端),我尝试了很多站点,但服务器只是接收127.0.0.1。额外信息(在 Michael Hampton 问题之后添加),我们的虚拟机基础架构:

  1. 客户端浏览器(另一个虚拟机或远程桌面)
  2. (VM1) nginx 反向代理(它有效,我至少添加了 HTTPS 层)
  3. (VM1) NgEngine 服务于自制前端
  4. (VM1) 由 Java/Spring 自制后端提供的 RESTful API(在 IntelliJ idea 中运行)
  5. (VM2..x) 其他服务器向后端提供数据(不可能)

我检查了文档,在这里尝试过(包括删除 X-Real-IP 标头,正如他们在另一个问题中所建议的那样)。我还尝试添加:

set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For
Run Code Online (Sandbox Code Playgroud)

我的 nginx 有 real_ip 模块(下面 nginx -V 的输出),这是我的 nginx.conf ......我做错了什么?

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
  worker_connections 768;
  # multi_accept on;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  # server_tokens off;

  server_names_hash_bucket_size 64;
  # server_name_in_redirect off;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  ssl_prefer_server_ciphers on;

  gzip on;
  gzip_disable "msie6";

  server {
    listen 443 ssl;
    server_name          test-server;
    ssl_certificate      /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key  /etc/nginx/ssl/nginx.key;

    add_header              Strict-Transport-Security  "max-age=63072000; includeSubdomains; preload" always;
    add_header              X-Frame-Options            SAMEORIGIN;
    add_header              X-Content-Type-Options     nosniff;

    # allow nginx to start regardless of upstream endpoint state by using intermediary variable
    set                     $UPSTREAM_SERVICE          10.10.10.15:8080;

    location  / {
      proxy_pass               http://localhost:6789
      proxy_http_version       1.1;

      proxy_buffering          off;
      proxy_buffer_size        128k;
      proxy_busy_buffers_size  256k;
      proxy_buffers            4                   256k;
      proxy_set_header         Host                $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;
      proxy_set_header         Upgrade             $http_upgrade;
      proxy_set_header         Connection          $http_connection;

    }
  }
}
Run Code Online (Sandbox Code Playgroud)

nginx -V 的输出:

nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.0g  2 Nov 2017
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-mcUg8N/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-headers-more-filter --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-auth-pam --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-cache-purge --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-dav-ext --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-ndk --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-echo --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-fancyindex --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/nchan --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-lua --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/rtmp --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-uploadprogress --add-dy
namic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-upstream-fair --add-dynamic-module=/build/nginx-mcUg8N/nginx-1.14.0/debian/modules/http-subs-filter
Run Code Online (Sandbox Code Playgroud)

非常感谢和最好的问候

mfo*_*tti 6

在现有的 nginx 反向代理上,我需要将客户端 IP 传递到服务器(两者都在同一操作系统中运行),我尝试了很多站点,但服务器只接收 127.0.0.1。

作为反向代理,您的后端服务器将始终接收来自 nginx 进程的连接,根据您的*_pass指令,该连接将包含您的 nginx 的 IP 地址(在您的情况下为 127.0.0.1)。

因此,您可能无法通过检查客户端地址来获取客户端IP,您只能获取前端服务器IP地址。

在反向代理和负载均衡中,呃,业务上,我们有几种方法可以从后端的反向代理中提取客户端IP:

  1. 非标准的事实上的 X-Forwarded-ForHTTP 请求标头,包含所有传递的代理和请求的客户端 IP 地址。
  2. ForwardedHTTP 请求标头是一种替代 X-Forwarded-For 的新标准化方式,还包含所有传递的代理和请求客户端的 IP 地址。
  3. X-Real-IPHTTP 请求标头,或任何自定义 HTTP 请求标头,任意包含客户端 IP 地址。

检查您的后端应用程序是否支持这些标头中的任何一个。


我的 nginx 有 real_ip 模块

ngx_http_realip_module 不用于这种情况。如果 nginx 位于代理后面,则使用它,以便它可以验证并获取真实的客户端 IP 地址并将其存储在指定的变量中。