Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full record url in nginx log

Tags:

nginx

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?

like image 513
user1859451 Avatar asked Jan 15 '14 11:01

user1859451


People also ask

How do I view Nginx logs?

By default, the access log is located at /var/log/nginx/access. log , and the information is written to the log in the predefined combined format. You can override the default settings and change the format of logged messages by editing the NGINX configuration file ( /etc/nginx/nginx. conf by default).

What is the format of Nginx logs?

The default log format in nginx is called "combined".

How do I change the log format in Nginx?

The syntax for configuring a log format is: log_format format_name 'set_of_variables_to_define_format'; and the syntax for configuring access log is: access_log /path/to/log_file format_name; #simplest form OR access_log /path/to/log_file [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

How do I enable Nginx logs?

For configuring the error_log, you have to add the path of the log file and set the log level. If you do not set the second parameter, then the error_log will take “error” as its default log level: error_log /var/log/nginx/error.


2 Answers

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.

like image 115
Tan Hong Tat Avatar answered Sep 30 '22 19:09

Tan Hong Tat


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.

like image 21
Tim Bray Avatar answered Sep 30 '22 19:09

Tim Bray