Created
October 25, 2024 10:05
-
-
Save victornavorskie/bf92031d8d14367cda7ddc1abcfed144 to your computer and use it in GitHub Desktop.
update_nginx_ports.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
#!/bin/bash | |
# Configuration files | |
CONFIG_FILE1="/etc/nginx/ugreen_redirect.conf" | |
CONFIG_FILE2="/etc/nginx/ugreen_ssl_redirect.conf" | |
# Search and replace patterns for ugreen_redirect.conf | |
SEARCH_LISTEN1="listen 80;" | |
REPLACE_LISTEN1="listen 8081;" | |
SEARCH_LISTEN_IPV61="listen \[::\]:80;" | |
REPLACE_LISTEN_IPV61="listen \[::\]:8081;" | |
# Search and replace patterns for ugreen_ssl_redirect.conf | |
SEARCH_LISTEN2="listen 443 ssl;" | |
REPLACE_LISTEN2="listen 4433 ssl;" | |
SEARCH_LISTEN_IPV62="listen \[::\]:443 ssl;" | |
REPLACE_LISTEN_IPV62="listen \[::\]:4433 ssl;" | |
# Directory and command for restarting Docker Compose | |
DOCKER_COMPOSE_DIR="/infrastructure/HomeLab/traefik" | |
DOCKER_COMPOSE_CMD="sudo docker compose restart" | |
# Function to update listen directives | |
update_listen_directives() { | |
local config_file=$1 | |
local search_listen=$2 | |
local replace_listen=$3 | |
local search_listen_ipv6=$4 | |
local replace_listen_ipv6=$5 | |
local changed=false | |
if grep -q "$search_listen" "$config_file"; then | |
echo "Updating IPv4 config in $config_file..." | |
sed -i "s/$search_listen/$replace_listen/" "$config_file" | |
changed=true | |
fi | |
if grep -q "$search_listen_ipv6" "$config_file"; then | |
echo "Updating IPv6 config in $config_file..." | |
sed -i "s/$search_listen_ipv6/$replace_listen_ipv6/" "$config_file" | |
changed=true | |
fi | |
if [ "$changed" = true ]; then | |
echo "Changes detected in $config_file." | |
echo "Reloading nginx..." | |
sudo systemctl reload nginx | |
echo "Waiting for nginx to reload..." | |
sleep 3 | |
echo "Restarting traefik..." | |
(cd "$DOCKER_COMPOSE_DIR" && $DOCKER_COMPOSE_CMD) | |
fi | |
} | |
# Initial update for both configuration files | |
update_listen_directives "$CONFIG_FILE1" "$SEARCH_LISTEN1" "$REPLACE_LISTEN1" "$SEARCH_LISTEN_IPV61" "$REPLACE_LISTEN_IPV61" | |
update_listen_directives "$CONFIG_FILE2" "$SEARCH_LISTEN2" "$REPLACE_LISTEN2" "$SEARCH_LISTEN_IPV62" "$REPLACE_LISTEN_IPV62" | |
# Monitor both configuration files for changes | |
while inotifywait -e close_write "$CONFIG_FILE1" "$CONFIG_FILE2"; do | |
echo "Detected changes in configuration files, updating listen directives..." | |
update_listen_directives "$CONFIG_FILE1" "$SEARCH_LISTEN1" "$REPLACE_LISTEN1" "$SEARCH_LISTEN_IPV61" "$REPLACE_LISTEN_IPV61" | |
update_listen_directives "$CONFIG_FILE2" "$SEARCH_LISTEN2" "$REPLACE_LISTEN2" "$SEARCH_LISTEN_IPV62" "$REPLACE_LISTEN_IPV62" | |
done | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment