From b1a484f4d023007095f0a6487fffc1b99a1077e0 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Tue, 22 Oct 2024 21:01:04 -0600 Subject: [PATCH] logging fixes --- install/nginx-log-format.sh | 35 +++++++++++++++++++++++++++++++++++ site-config.conf | 10 +++------- 2 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 install/nginx-log-format.sh diff --git a/install/nginx-log-format.sh b/install/nginx-log-format.sh new file mode 100644 index 0000000..90a2936 --- /dev/null +++ b/install/nginx-log-format.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Define the log format +LOG_FORMAT='log_format domain_combined '\''$host $remote_addr - $remote_user [$time_local] '\ +''\"$request\" $status $body_bytes_sent '\ +''\"$http_referer\" \"$http_user_agent\"'\'';' + +# Check if we're root +if [ "$EUID" -ne 0 ]; then + echo "Please run as root" + exit 1 +fi + +# Backup the original config +cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak + +# Check if format already exists +if grep -q "log_format domain_combined" /etc/nginx/nginx.conf; then + echo "domain_combined format already exists" + exit 0 +fi + +# Add the format after the first 'http {' line +sed -i "/^http {/a \ ${LOG_FORMAT}" /etc/nginx/nginx.conf + +# Test the nginx config +nginx -t + +if [ $? -eq 0 ]; then + echo "Successfully added domain_combined log format" +else + echo "Error in nginx config, reverting..." + mv /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf + exit 1 +fi diff --git a/site-config.conf b/site-config.conf index f324d51..dbe9ce2 100644 --- a/site-config.conf +++ b/site-config.conf @@ -27,13 +27,9 @@ server { # Subdomain handling set $subdomain ''; set $full_root "/var/www/$DOMAIN/_main/www"; - set $access_log_path "/var/www/$DOMAIN/logs/main.access.log"; - set $error_log_path "/var/www/$DOMAIN/logs/main.error.log"; if ($host ~* ^([^.]+)\.$DOMAIN$) { set $subdomain $1; set $full_root "/var/www/$DOMAIN/subdomains/$subdomain/www"; - set $access_log_path "/var/www/$DOMAIN/logs/$subdomain.access.log"; - set $error_log_path "/var/www/$DOMAIN/logs/$subdomain.error.log"; } root $full_root; @@ -81,8 +77,8 @@ server { } # Logging - access_log /var/log/nginx/access.log; + access_log /var/log/nginx/access.log domain_combined; error_log /var/log/nginx/error.log; - access_log $access_log_path; - error_log $error_log_path; + access_log "/var/www/$DOMAIN/logs/access.log" domain_combined; + error_log "/var/www/$DOMAIN/logs/error.log"; }