0
0
Nginxdevops~5 mins

Access log configuration in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Access logs record every request made to your web server. Configuring access logs helps you see who visits your site and what they do, which is useful for troubleshooting and understanding traffic.
When you want to track visitor activity on your website to improve user experience.
When you need to debug issues by checking what requests your server received.
When you want to monitor traffic patterns for security or performance reasons.
When you want to analyze which pages are most popular on your site.
When you need to keep records for compliance or auditing purposes.
Config File - nginx.conf
nginx.conf
http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

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

    server {
        listen 80;
        server_name example.com;

        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
}

The log_format directive defines a custom format named main for access logs, specifying what details to record for each request.

The access_log directive sets the file path where logs are saved and applies the main format.

The server block configures a basic web server listening on port 80 for example.com.

Commands
Check the Nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the new access log configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
View the last 5 lines of the access log to verify that requests are being logged correctly.
Terminal
tail -n 5 /var/log/nginx/access.log
Expected OutputExpected
192.168.1.10 - - [27/Apr/2024:14:22:10 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" "-" 192.168.1.11 - - [27/Apr/2024:14:22:15 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)" "-"
Key Concept

If you remember nothing else from this pattern, remember: access logs record every request your server handles, and configuring them helps you monitor and troubleshoot your website.

Common Mistakes
Forgetting to reload Nginx after changing the access log configuration.
The new logging settings won't take effect until Nginx reloads the configuration.
Always run 'sudo nginx -t' to test, then 'sudo systemctl reload nginx' to apply changes.
Setting the access log file path to a directory without write permission.
Nginx will fail to write logs, causing errors and no logs being recorded.
Ensure the log file path is writable by the Nginx user, typically /var/log/nginx/access.log.
Summary
Define a log format using the 'log_format' directive to specify what details to record.
Set the 'access_log' directive with the file path and format to enable logging.
Test the configuration with 'nginx -t' and reload Nginx to apply changes.
Check the access log file to verify that requests are being logged.