nginx获取后端真实IP,添加后端服务器响应时间并记录日志
1.日志定义
log_format nginx '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$proxy_add_x_forwarded_for"'
'"$http_x_forwarded_for" "$upstream_response_time"'
'"$request_time" "$upstream_status" "$remote_addr" "$upstream_addr"';
access_log /var/log/nginx/access-nginx.log nginx;
$upstream_addr 是关键
$request_time #处理完请求所花时间,以秒为单位
$upstream_response_time #建立连接 到 接收完数据并关闭连接
2. proxy定义
location / {
proxy_pass http://server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
}
3. 日志展示
10.0.0.1 - - [13/Aug/2020:14:10:32 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36" "10.0.0.1""-" "0.002""0.002" "304" "10.0.0.1" "10.0.0.65:80"
10.0.0.1 - - [13/Aug/2020:14:10:32 +0800] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36" "10.0.0.1""-" "0.001""0.001" "200" "10.0.0.1" "10.0.0.66:80"