一、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 '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$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 $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_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服務,日志內容如下: