发布时间: 2024-2-28 文章作者: myluzh 分类名称: CLOUD 朗读文章
X-Forwarded-For: <客户端真实IP, 代理服务器1-IP, 代理服务器2-IP,...>当使用此方式获取客户端真实IP时,获取的第一个地址就是客户端真实IP。
http { # 确保设置$http_x_forwarded_for,该变量用于记录X-Forwarded-For的值 log_format main '$remote_addr- $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # ... }3、修改Nginx Server里面的日志配置
server { listen 80; server_name *.sxhlcloud.com; # 配置access输出位置,不支持error_log access_log /var/log/nginx/prd.access.log main; # ... }4、测试Nginx配置&重载配置
root@iZbp12bkuvg20e1j3y9gtvZ:/etc/nginx/conf.d# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful root@iZbp12bkuvg20e1j3y9gtvZ:/etc/nginx/conf.d# nginx -s reload5、查看日志
root@iZbp12bkuvg20e1j3y9gtvZ:/etc/nginx/conf.d# tail -f -n 1 /var/log/nginx/prd.access.log # 可以看到 每行日志最后已经有客户端IP了 100.127.135.209- - [28/Feb/2024:14:57:36 +0800] "GET /prod-api/employ/employ/reCheck/18543/666 HTTP/1.1" 200 65 "https://servicewechat.com/wx7a4767881e87ea7e/44/page-frame.html" "Mozilla/5.0 (Linux; Android 10; M2010J19SC Build/QKQ1.200830.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36 XWEB/1160065 MMWEBSDK/20230805 MMWEBID/926 MicroMessenger/8.0.42.2460(0x28002A58) WeChat/arm64 Weixin NetType/4G Language/zh_CN ABI/arm64 MiniProgramEnv/android" "223.104.159.148"
发表评论