Skip to content

Nginx Log Format

Nginx allows you to configure the log format quite easily.

The standard log is basically useful but can be extended with extra useful info.

Log Locations

Unless configured not to, each request is logged

The locations default to /var/log/nginx/access.log and /var/log/nginx/error.log, however logging to these files is highly inadvised.

Per Server Block Log Files

Instead of allowing all logs to go into the main files described above, it is much better to log requests to each server block to their own set of files

For example:

server {
    #server block configs..

    access_log /var/log/nginx/www.domain.com.access.log main;
    error_log /var/log/nginx/www.domain.com.error.log info;

Log Format

The access log format is defined in /etc/nginx/nginx.conf

Suggested Access Log Format

Here is a suggested log format:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_tokens on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request "'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      '{P:$server_port} '
                      '{SSL:$scheme|$ssl_protocol|$ssl_cipher} '
                      '{RT:$request_time} '
                      '{CT:$connection}';
    access_log  /var/log/nginx/access.log  main;

Notice the use of variables and also a "grep friendly" syntax using {} characters to demarcate sections.

There is an extensive list of variables you can use, see this list