(一)簡要說明 ?
? 如果你的Web服務(wù)器前端有代理服務(wù)器或CDN時日志中的$remote_addr可能就不是客戶端的真實IP了。比較常用的解決方法有以下三幾種,本文將主要介紹如何使用Nginx自帶realip模塊來解決這一問題:
1,用CDN自定義IP頭來獲取
2,通過HTTP_X_FORWARDED_FOR獲取IP地址
3,使用Nginx自帶模塊realip獲取用戶IP地址
? ? ngx_realip模塊究竟有什么實際用途呢?為什么我們需要去改寫請求的來源地址呢?答案是:當(dāng)Nginx處理的請求經(jīng)過了某個HTTP代理服務(wù)器的轉(zhuǎn)發(fā)時,這個模塊就變得特別有用。
? ? 當(dāng)原始用戶的請求經(jīng)過代理(squid,proxy)轉(zhuǎn)發(fā)之后,nginx接收到的請求的來源地址也就變成了該代理服務(wù)器的IP,于是乎nginx 就無法獲取用戶請求的真實IP地址了。
? ?所以,一般我們會在Nginx之前的代理服務(wù)器中把請求的原始來源地址編碼進某個特殊的HTTP請求頭中,然后再在Nginx中把這個請求頭中編碼的地址恢復(fù)出來。這樣Nginx中的后續(xù)處理階段(包括Nginx背后的各種后端應(yīng)用)就會認為這些請求直接來自那些原始的地址,代理服務(wù)器就仿佛不存在一樣。ngx_realip模塊正是用來處理這個需求的。
(二)安裝realip模塊
[root@k8s-admin ~]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips? 26 Jan 2017
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 -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
(三)配置語法
set_real_ip_from 192.168.1.0/24; #真實服務(wù)器上一級代理的IP地址或者IP段,可以寫多行。 set_real_ip_from 192.168.2.1;?
real_ip_header ? X-Forwarded-For; ?#從哪個header頭檢索出所要的IP地址。
real_ip_recursive on; ? ? ?#遞歸的去除所配置中的可信IP。排除set_real_ip_from里面出現(xiàn)的IP。如果出現(xiàn)了未出現(xiàn)這些IP段的IP,那么這個IP將被認為是用戶的IP。
一下就是配置實例:
server?{
????????????????listen?80;
????????????????server_name??localhost;
????????????????index?index.html?index.htm?index.php;
????????????????#include?deny.ip;
????????????????access_log?/data/nginx.access.log;
??????????????????location?~?.*?{
????????????????????proxy_pass?http://192.168.180.20;
????????????????????proxy_set_header?X-Real-IP?$remote_addr;
????????????????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
???????????????????#proxy_set_header?X-Forward-For?$remote_addr;
????????????????????proxy_set_header?Host?$host;
????????????????????set_real_ip_from??192.168.180.0/24;
????????????????????set_real_ip_from?192.168.181.0/24;
????????????????????real_ip_header????X-Forwarded-For;
????????????????????real_ip_recursive?on;
????????????????????????}
????????}
如果服務(wù)器獲取的IP地址如下:
192.168.180.4
192.168.181.30
118.242.26.94
在real_ip_recursive on的情況下,192.168.180.4和192.168.181.30這兩個IP地址都在set_real_ip_from中出現(xiàn),僅僅118.242.26.94沒有出現(xiàn),那么這個IP就被認為是用戶的IP地址,并且賦值到remote_addr變量。
在real_ip_recursive off或者不設(shè)置的情況下,192.168.180.4出現(xiàn)在了set_real_ip_from中會被排除掉,其它的IP地址便認為是用戶的ip地址。