Configure Nginx to format logs and set up a server block.
Open the Nginx configuration file:
sudonano/etc/nginx/nginx.confÂ
Add the following log format into your http group in nginx:
http { log_format combined_log '$remote_addr - $remote_user [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent"';# ... (rest of the configuration)}
Warning: This logs the users IP address directly. It's not recommended to do it in this fashion, if possible anonymize the address as seen below.
(optional) Instead anonymize IP addresses in logs:
http {map $remote_addr $anonymized_addr {~(?P<ip>\d+\.\d+)\.\d+\. $ip.0.0;~(?P<ip>[^:]+:[^:]+): $ip::;# IP addresses to not anonymize (such as your server)127.0.0.1 $remote_addr; ::1 $remote_addr;#w.x.y.z $remote_addr;#a::c:d::e:f $remote_addr;default0.0.0.0; } log_format combined_log '$anonymized_addr - $remote_user [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent"'; access_log /var/log/nginx/access.log combined_log;# ... (rest of the configuration)}
Configure a server block:
server { listen 80; server_name yourdomain.com; # Replace with your domain root /var/www/html; index report.html;location / { try_files $uri $uri/ =404; }}
Testing and Reloading Nginx Configuration
Test the new configuration:
sudonginx-t
Reload Nginx to apply changes:
sudosystemctlreloadnginx
Configuring Log Rotation
Log rotation in Nginx is a process for managing log files to prevent them from becoming excessively large and consuming too much disk space. As Nginx continuously logs web requests, these files can grow rapidly. Without rotation, they can lead to performance issues and make log analysis more difficult. The default setting is for log rotation is daily, which means that the logs that goaccess can use for its reporting are also only daily. To increase that timeframe, do the following:
Edit log rotation configuration:
sudonano/etc/logrotate.d/nginx
Add the configuration, please change the monthly to daily or weekly if you need daily or weekly rotation of the logs.
/var/log/nginx/*.log{monthlymissingokrotate1compressdelaycompressnotifemptycreate0640www-dataadmsharedscriptsprerotateif [ -d /etc/logrotate.d/httpd-prerotate ]; then \run-parts/etc/logrotate.d/httpd-prerotate; \fi \endscriptpostrotateinvoke-rc.dnginxrotate>/dev/null2>&1endscript}