14
votes

Total NGINX beginner here.

My logs currently look like:

92.21.236.47 - - [08/Jan/2017:00:48:10 +0000] "GET / HTTP/1.1" 200 148 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"

When I add the following line in the default /etc/nginx/nginx.conf

log_format main '$remote_addr - $remote_user xxx[$time_local]xxx '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';

access_log /var/log/nginx/access.log; 
error_log /var/log/nginx/error.log;

(the access_log and error_log lines already exist in the default config, I have put these here solely for context).

I then restart NGINX with:

systemctl restart nginx

I now expect my logs to change and in particular to show the xxx literal values used .. xxx[$time_local]xxx .. but my change makes no difference.

If I change log_format main to log_format combined then the server won't restart.

2

2 Answers

29
votes

changing ..

access_log /var/log/nginx/access.log;

to ..

access_log /var/log/nginx/access.log main;

fixed it.

where main is the name of the log_format as defined at ..

log_format main '$remote_addr - $remote_user xxx[$time_local]xxx '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';
13
votes

Its late to reply but it might be useful for those who are still hanging in this prolem.

Write the log_format directives outsider the server {}

The whole code would be like this

log_format main '$remote_addr - $remote_user xxx[$time_local]xxx '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';
server {
      listen 80;
      access_log /var/log/nginx/<your_domain>_access.log main; 
}

Here main represents what fields nginx should include while writing the logs.

For more detail information, Please look in the official documentation Sampling Requests with NGINX Conditional Logging