nginx进行获取阿里云slb真实ip配置操作

环境:

 

1、使用阿里云的slb进行配置nginx,nginx无法获取用户的真实ip解决方案

 

参考阿里云:

https://help.aliyun.com/knowledge_detail/40535.html?spm=5176.2000002.0.0.57554e22IybjHD


 

 

 

Nginx配置方案

  1. 确认 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`


      1. 修改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段。
      2. 修改日志记录格式 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" ';
        
        添加x-forwarded-for字段,替换原本的remote-address。
      3. 重启Nginx使配置生效 nginx -s reload

实际配置文件:

 

 

nginx具体配置:

 

 

log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" ';

 

 

 

 

 

posted @ 2019-06-24 10:10  zhouyuqiang  阅读(1657)  评论(0编辑  收藏  举报