nginxconfig.io icon indicating copy to clipboard operation
nginxconfig.io copied to clipboard

access.log and error.log placement

Open SorinGFS opened this issue 3 years ago • 0 comments

Feature request

Place the access_log and error_log directives inside the main server block instead of http block.

Feature description

Disable access_log at http block: File: /etc/nginx/nginx.conf

# ...
http {
    # ...
    access_log off;
    # ...
}
# ...

Enable per site access_log and error_log at main server block: File: /etc/nginx/sites-available/example.com.conf

server {
    listen                  443 ssl http2;
    listen                  [::]:443 ssl http2;
    server_name             example.com;
    set                     $base /var/www/example.com;
    root                    $base/public;
    # new lines here
    error_log               /var/log/nginx/${server_name}_error.log warn;
    access_log              /var/log/nginx/${server_name}_access.log buffer=512k flush=1m;;
    # ...
}
# ...

Note: the ${server_name} variable must be replaced with the actual server_name set by the user for current website since Nginx does not accept variables in error_log or access_log directives path.

How the feature is useful

Every site has from 3 to 5 server blocks for 301 redirects, this means that access_log will be filled with useless info. Disabling access_log at http level and enabling only for the main server block will ensure cleaner logs. Also, enabling per site access_log will allow a faster i/o (smaller files) and faster spotting of entries (for example using bash tail will retreive only the records belonging to a certain site). The last two arguments buffer=512k flush=1m will massively reduce the i/o frequency by keeping the records in memory up to 512Kb or dumping them to file after 1 minute (in case of not reaching the limit). Enabling per site access_log can also be made optional (for backward compatibility, or for user preference) by enabling the ${server_name}_ part from syntax only when a checkbox is checked. IMHO it should be enabled by default, but any case is better than before.

SorinGFS avatar Apr 29 '22 00:04 SorinGFS