Nginx real case - get real client

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	#在不暂停服务的情况下重新加载

Here Insert Picture Description
Here Insert Picture Description

step2 empty log on server1:

cd /usr/local/nginx/logs
>access.log 	#清空日志

step3 added resolved in server3 and tested:

vim /etc/hosts

Here Insert Picture Description

step4 view the log on server1:
Here Insert Picture Description

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;

Here Insert Picture Description
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;

Here Insert Picture Description

step7 reload nginx server1 and server2 are:

nginx -t			#语法检测
nginx -s reload		#在不暂停服务的情况下重新加载

step8 access server1 in the client:
Here Insert Picture Description
step9 View customer log end server1 ip:

After setting up a profile, you can see the real client ip (server3 of ip) a
Here Insert Picture Description

Published 175 original articles · won praise 11 · views 6049

Guess you like

Origin blog.csdn.net/weixin_45775963/article/details/104540682