log format fix

This commit is contained in:
Joby 2024-10-23 15:33:06 -06:00
parent 3adab944db
commit c756a8ac81

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/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\"';" 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 # Check if we're root
@ -9,28 +9,20 @@ if [ "$EUID" -ne 0 ]; then
exit 1 exit 1
fi fi
# Backup the original config # Define the config file path
if ! cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak; then CONFIG_FILE="/etc/nginx/conf.d/log-format.conf"
echo "Failed to create backup"
exit 1
fi
# Check if format already exists # Create/overwrite the config file
if grep -q "log_format domain_combined" /etc/nginx/nginx.conf; then echo "$LOG_FORMAT" > "$CONFIG_FILE"
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 # Test the nginx config
nginx -t nginx -t
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "Successfully added domain_combined log format" echo "Successfully updated domain_combined log format"
systemctl restart nginx
else else
echo "Error in nginx config, reverting..." echo "Error in nginx config, removing new config..."
mv /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf rm "$CONFIG_FILE"
exit 1 exit 1
fi fi