阿里云SLB后Nginx、Tomcat获取真实IP

一、SLB后Nginx如何获取真实IP

前提:nginx作为slb获取真实ip是使用 http_realip_module,默认一键安装包安装的nginx没有安装这个模块需要重新重新编译nginx并加装。

  查看是否安装此模块:./nginx -V

  如没有安装此模块,需重新编译nginx增加 --with-http_realip_module

1、阿里云SLB需要开启获取真实IP(默认已开启)

2、修改Nginx配置文件nginx.conf

添加:

http {

  log_format  main  'remoteaddrremote_user [timelocal]"request" '
              'statusbody_bytes_sent "httpreferer""http_user_agent" "$http_x_forwarded_for"';

  access_log  /home/logs/nginx_access.log  main;

  server {
    listen 80;
    server_name domain_name;
    location / {
      proxy_set_header Host httphost;      proxyredirectoff;      proxysetheaderXRealIPremote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://IPADDR:PORT;
    }
  }

}

3、日志内容如下:

二、SLB后Tomcat如何获取真实IP

1、修改Tomcat配置文件

   

2、重启Tomcat服务,日志内容如下:

 
posted @   GaoYanbing  阅读(407)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示