#!/bin/bash # nginx-safe-reload.sh # Safely reload nginx by testing configuration first echo "Testing nginx configuration..." # Test nginx configuration and capture output test_output=$(nginx -t 2>&1) test_result=$? if [ $test_result -eq 0 ]; then echo "✓ Configuration test passed" echo "Reloading nginx..." # Reload nginx service if service nginx reload; then echo "✓ nginx reloaded successfully" exit 0 else echo "✗ Failed to reload nginx service" exit 1 fi else echo "✗ Configuration test failed - nginx NOT reloaded" echo echo "Error details:" echo "$test_output" echo # Try to extract and highlight the problematic file error_file=$(echo "$test_output" | grep -o 'in /[^:]*' | head -1 | sed 's/in //') if [ -n "$error_file" ]; then echo "🔍 Check this file: $error_file" fi echo "Please fix configuration errors before reloading" exit 1 fi