By default nginx's log_format does not contain hostname. Since I serve multiple domains with a single nginx, I want to add hostname to nginx's access logs.
I guess nginx's default log_format
is combined
which is something
like this:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
Our new log_format
is named combined_with_host
, defined as:
log_format combined_with_host '$remote_addr - $remote_user [$time_local] '
'"$request_method $scheme://$host$request_uri $server_protocol" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
Go to your nginx.conf
, and add the lines above into http { ... }
block.
Now we want to set format of access_log
s to combined_with_host
:
access_log /var/log/nginx/access.log combined_with_host;
Save your nginx.conf
and reload the nginx.
Switch to root user, and add following cron job by crontab -e
:
0 0 * * 1 /bin/bash -c "/some/path/to/report.sh" >> /some/path/to/report.log
server {
# ...
location /nginx-weekly-reports {
alias /some/path/to/reports;
auth_basic "Restricted Content";
auth_basic_user_file /some/path/to/.htpasswd;
}
}