1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| location / { proxy_pass [$Domain]; #必须 index index.html index.htm index.jsp index.shtml; proxy_redirect off; proxy_set_header Host $host; proxy_set_header Lky $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-My-Header "My Value"; #在日志中使用$http_x_my_header就可以获取到值,可以写死也可以用内置变量比如set自定义 经测试 proxy_set_header 第一个字母必须大写,只能用-不能用_ log_format 日志格式必须是$http开头-需要换成_而且必须全部小写 }
log_format custom '$remote_addr [$time_local] "$request" ' 'Lky:$http_lky' ; access_log /var/log/nginx/access.log custom;
#如果是 proxy_pass http://127.0.0.1:83 第一跳记录上游日志包含真实ip,第二条是客户端访问的不包含Lky,一个请求有两个日志输出 #127.0.0.1 - - [23/May/2025:16:31:41 +0800] "GET / HTTP/1.0" 200 4833 "-" "curl/7.60.0" "10.0.1.100" 10.0.1.100Lky:"10.0.1.100" #这里的日志可以通过proxy_set_header自定义,比如获取客户端真实IP
#10.0.1.100 - - [23/May/2025:16:31:41 +0800] "GET / HTTP/1.1" 200 4833 "-" "curl/7.60.0" "-" 10.0.1.100:83Lky:"-" #这个则是客户端直接请求的日志,因为$remote_addr为空
#测试响应头 add_header Lky $remote_addr always; GET / HTTP/1.1 Host: example.com User-Agent: Mozilla/5.0 Lky: 192.168.1.100
|