webserver-config/install/nginx-log-format.sh

29 lines
702 B
Bash
Raw Normal View History

2024-10-23 03:01:04 +00:00
#!/bin/bash
2024-10-23 21:33:06 +00:00
# Define the log format
2024-10-23 21:35:40 +00:00
LOG_FORMAT="log_format domain_combined '\$host \$remote_addr - \$remote_user [\$time_local] \"\$request\" \$status \$body_bytes_sent \"\$http_referer\" \"\$http_user_agent\"';"
2024-10-23 03:01:04 +00:00
# Check if we're root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
2024-10-23 21:33:06 +00:00
# Define the config file path
CONFIG_FILE="/etc/nginx/conf.d/log-format.conf"
2024-10-23 03:01:04 +00:00
2024-10-23 21:33:06 +00:00
# Create/overwrite the config file
echo "$LOG_FORMAT" > "$CONFIG_FILE"
2024-10-23 03:01:04 +00:00
# Test the nginx config
nginx -t
if [ $? -eq 0 ]; then
2024-10-23 21:33:06 +00:00
echo "Successfully updated domain_combined log format"
systemctl restart nginx
2024-10-23 03:01:04 +00:00
else
2024-10-23 21:33:06 +00:00
echo "Error in nginx config, removing new config..."
rm "$CONFIG_FILE"
2024-10-23 03:01:04 +00:00
exit 1
fi