環境:php
一、使用阿里雲的slb進行配置nginx,nginx沒法獲取用戶的真實ip解決方案html
參考阿里雲:nginx
https://help.aliyun.com/knowledge_detail/40535.html?spm=5176.2000002.0.0.57554e22IybjHDshell
Nginx配置方案app
- 確認 http_realip_module 模塊已安裝。Nginx做爲負載均衡獲取真實IP是使用http_realip_module模塊。
說明 經過一鍵安裝包安裝的Nginx默認不安裝此模塊,能夠使用
# nginx -V | grep http_realip_module
查看此模塊有無安裝。
wget http://soft.phpwind.me/top/nginx-1.0.12.tar.gz tar zxvf nginx-1.0.12.tar.gz cd nginx-1.0.12 ./configure --user=www --group=www --prefix=/alidata/server/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_realip_module make make install kill -USR2 `cat /alidata/server/nginx/logs/nginx.pid` kill -QUIT `cat /alidata/server/nginx/logs/ nginx.pid.oldbin`- 修改Nginx對應server的配置。 在
location / {}
中添加如下內容。set_real_ip_from ip_range1; set_real_ip_from ip_range2; ... set_real_ip_from ip_rangex; real_ip_header X-Forwarded-For;
說明 這裏的ip_range1,2,...
指的是高防IP的回源IP地址,須要添加多條。若是高防IP後還有WAF、CDN,則須要寫WAF、CDN的回源IP地址,即須要寫離源站最近的一層七層代理的回源IP段。 - 修改日誌記錄格式 log_format。log_format通常在nginx.conf中的HTTP配置中:
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" ';
- 重啓Nginx使配置生效
nginx -s reload
。
- 修改Nginx對應server的配置。 在
實際配置文件:負載均衡
nginx具體配置:阿里雲
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" ';url