← Back to all snippets
BASH

Automate Nginx Configuration Reload on Changes

Automatically validate and reload your Nginx configuration files when changes are detected, ensuring your web server always runs with the latest settings.

#!/bin/bash

# --- Configuration Start ---
NGINX_CONF_DIR="/etc/nginx"
# Main Nginx config file or directory to monitor for changes
# For single file: NGINX_CONFIG_TARGET="$NGINX_CONF_DIR/nginx.conf"
# For a directory with multiple configs (e.g., sites-enabled): NGINX_CONFIG_TARGET="$NGINX_CONF_DIR/sites-enabled"
NGINX_CONFIG_TARGET="$NGINX_CONF_DIR"

# Path to the Nginx executable (usually /usr/sbin/nginx or /usr/local/nginx/sbin/nginx)
NGINX_BIN="/usr/sbin/nginx"

# Interval (in seconds) to check for changes if not using inotifywait
CHECK_INTERVAL=5
# --- Configuration End ---

LAST_MD5=""

# Function to calculate MD5 sum of target files/directory
calculate_md5() {
  if [ -d "$NGINX_CONFIG_TARGET" ]; then
    find "$NGINX_CONFIG_TARGET" -type f \( -name "*.conf" -o -name "*.inc" \) -print0 | sort -z | xargs -0 cat | md5sum | awk '{print $1}'
  elif [ -f "$NGINX_CONFIG_TARGET" ]; then
    md5sum "$NGINX_CONFIG_TARGET" | awk '{print $1}'
  else
    echo "Error: NGINX_CONFIG_TARGET '$NGINX_CONFIG_TARGET' is neither a file nor a directory." >&2
    exit 1
  fi
}

echo "Monitoring Nginx configuration files in '$NGINX_CONFIG_TARGET' for changes..."

while true; do
  CURRENT_MD5=$(calculate_md5)

  if [ -z "$LAST_MD5" ]; then
    LAST_MD5="$CURRENT_MD5"
    echo "Initial MD5 hash established."
  fi

  if [ "$CURRENT_MD5" != "$LAST_MD5" ]; then
    echo "
--- Nginx configuration change detected! ---"
    echo "Current MD5: $CURRENT_MD5"
    echo "Previous MD5: $LAST_MD5"

    echo "Testing Nginx configuration syntax..."
    "$NGINX_BIN" -t
    if [ $? -eq 0 ]; then
      echo "Nginx configuration syntax is OK. Reloading Nginx..."
      "$NGINX_BIN" -s reload
      if [ $? -eq 0 ]; then
        echo "Nginx reloaded successfully!"
        LAST_MD5="$CURRENT_MD5"
      else
        echo "Error: Failed to reload Nginx. Configuration not applied." >&2
      fi
    else
      echo "Error: Nginx configuration syntax failed. Changes not applied." >&2
      # Revert LAST_MD5 to prevent continuous re-attempts on bad config
      # LAST_MD5="$CURRENT_MD5" # Uncomment to keep trying until fixed
    fi
  else
    echo -n "."
  fi

  sleep "$CHECK_INTERVAL"
done
How it works: This script continuously monitors Nginx configuration files for changes. It calculates an MD5 hash of the relevant configuration files (or an entire directory) and compares it to a previously stored hash. If a change is detected, it first runs `nginx -t` to test the configuration syntax. If the syntax is valid, it then gracefully reloads Nginx using `nginx -s reload`, applying the new configuration without service interruption. This automation is invaluable for development environments or situations where frequent Nginx configuration updates are needed, preventing downtime from manual errors.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs