用nginx做反向代理,后端服务器获取访客的真实IP的配置方法

参考:https://blog.csdn.net/diyiday/article/details/80827437

公司目前使用的业务是前端使用nginx做为反向代理,后端使用nginx作为web服务器,由于前期没有配置,导致后端服务器记录的访问日志的ip全部是来着前端反向代理服务器的。
如果需要后端服务器记录访客真是ip。需要进行如下配置:

一、配置反向代理端的nginx服务器

可以配置在server段,也可配在location段

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

二、在后端服务器配置如下

在http端自定义日志格式:

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

然后在定义日志路径的时候也需要加上test;

例如: access_log /tmp/1.log test;

猜你喜欢

转载自www.cnblogs.com/new-journey/p/13187571.html