? 如果妳的Web服務器前端有代理服務器或CDN時日誌中的$remote_addr可能就不是客戶端的真實IP了。比較常用的解決方法有以下三幾種,本文將主要介紹如何使用Nginx自帶realip模塊來解決這壹問題:
1,用CDN自定義IP頭來獲取
2,通過HTTP_X_FORWARDED_FOR獲取IP地址
3,使用Nginx自帶模塊realip獲取用戶IP地址
ngx_realip模塊究竟有什麽實際用途呢?為什麽我們需要去改寫請求的來源地址呢?答案是:當Nginx處理的請求經過了某個HTTP代理服務器的轉發時,這個模塊就變得特別有用。
當原始用戶的請求經過代理(squid,proxy)轉發之後,nginx接收到的請求的來源地址也就變成了該代理服務器的IP,於是乎nginx 就無法獲取用戶請求的真實IP地址了。
所以,壹般我們會在Nginx之前的代理服務器中把請求的原始來源地址編碼進某個特殊的HTTP請求頭中,然後再在Nginx中把這個請求頭中編碼的地址恢復出來。這樣Nginx中的後續處理階段(包括Nginx背後的各種後端應用)就會認為這些請求直接來自那些原始的地址,代理服務器就仿佛不存在壹樣。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; #真實服務器上壹級代理的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裏面出現的IP。如果出現了未出現這些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_headerX-Forwarded-For;
real_ip_recursive?on;
}
}
如果服務器獲取的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中出現,僅僅118.242.26.94沒有出現,那麽這個IP就被認為是用戶的IP地址,並且賦值到remote_addr變量。
在real_ip_recursive off或者不設置的情況下,192.168.180.4出現在了set_real_ip_from中會被排除掉,其它的IP地址便認為是用戶的ip地址。