Nginx real case - get real client
In this experiment as server1 nginx server; Server2 is server1 agent; client Server3
step1 modify nginx configuration file to configure a reverse proxy in server2:
vim /usr/local/nginx/conf/nginx.conf
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20 upstream westos {
21 server 172.25.254.1:80;
22 }
118 server {
119 listen 80;
120 server_name www.westos.org;
121 location / {
122 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
123 proxy_pass http://westos;
124 }
125 }
nginx -t #语法检测
nginx -s reload #在不暂停服务的情况下重新加载
step2 empty log on server1:
cd /usr/local/nginx/logs
>access.log #清空日志
step3 added resolved in server3 and tested:
vim /etc/hosts
step4 view the log on server1:
step5 server1 modify configuration files, access to real client ip:
vim /usr/local/nginx/conf/nginx.conf
39 server {
40 listen 80;
41 server_name localhost;
42 set_real_ip_from 172.25.254.2; #从代理处获取真实ip
43 real_ip_header X-Forwarded-For; #从http请求头中拿到数据
44 real_ip_recursive on;
step6 server2 modify configuration files, access to real client ip:
vim /usr/local/nginx/conf/nginx.conf
118 server {
119 listen 80;
120 server_name www.westos.org;
121 location / {
122 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #从真实的请求头中拿到数据,真实后端才能保存下来
123 proxy_pass http://westos;
step7 reload nginx server1 and server2 are:
nginx -t #语法检测
nginx -s reload #在不暂停服务的情况下重新加载
step8 access server1 in the client:
step9 View customer log end server1 ip:
After setting up a profile, you can see the real client ip (server3 of ip) a