Full record url in nginx log
NginxNginx Problem Overview
We use following nginx site configure file in our production env.
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';
server {
root /srv/www/web;
server_name *.test.com;
access_log /var/log/nginx/xxx.test.com.access.log main;
Both "http://a.test.com/ping" and "http://b.test.com/ping" http request will be record in file xxx.test.com.access.log.
But there is a problem, nginx don't store "domain name" in xxx.test.com.access.log.
"http://a.test.com/ping" and "http://b.test.com/ping" share the same request "Get /ping".
How can I record "a.test.com" or "b.test.com" in nginx log?
Nginx Solutions
Solution 1 - Nginx
Try adding the $host
variable in log_format:
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$host" "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';
http://wiki.nginx.org/HttpCoreModule#.24host:
> $host > > This variable is equal to line Host in the header of request or name > of the server processing the request if the Host header is not > available. > > This variable may have a different value from $http_host in such > cases: 1) when the Host input header is absent or has an empty value, > $host equals to the value of server_name directive; 2) when the value > of Host contains port number, $host doesn't include that port number. > $host's value is always lowercase since 0.8.17.
Solution 2 - Nginx
If you want to log the full requested url, then my method is
log_format main '$http_x_forwarded_for - $remote_user [$time_local] '
'"$request_method $scheme://$host$request_uri $server_protocol" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';
So splitting the $request into its constituent parts and popping $host in the middle. Also lets you see if the request is http or https.