diff --git a/install/nginx-log-format.sh b/install/nginx-log-format.sh index 28fed4d..7678ad8 100644 --- a/install/nginx-log-format.sh +++ b/install/nginx-log-format.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Define the log format (all on one line) +# 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 @@ -9,28 +9,20 @@ if [ "$EUID" -ne 0 ]; then exit 1 fi -# Backup the original config -if ! cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak; then - echo "Failed to create backup" - exit 1 -fi +# Define the config file path +CONFIG_FILE="/etc/nginx/conf.d/log-format.conf" -# 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 +# Create/overwrite the config file +echo "$LOG_FORMAT" > "$CONFIG_FILE" # Test the nginx config nginx -t if [ $? -eq 0 ]; then - echo "Successfully added domain_combined log format" + echo "Successfully updated domain_combined log format" + systemctl restart nginx else - echo "Error in nginx config, reverting..." - mv /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf + echo "Error in nginx config, removing new config..." + rm "$CONFIG_FILE" exit 1 fi