Skip to content

Instantly share code, notes, and snippets.

@Damotron500
Forked from hjbotha/free_ports.sh
Last active July 6, 2026 14:57
Show Gist options
  • Select an option

  • Save Damotron500/18d7e114df22631a428c9f44eb8b3b3d to your computer and use it in GitHub Desktop.

Select an option

Save Damotron500/18d7e114df22631a428c9f44eb8b3b3d to your computer and use it in GitHub Desktop.
Free ports 80 and 443 on Synology NAS
#!/bin/bash
# WARNING: Use at your own risk. Test carefully before applying in production.
# THIS SCRIPT WAS UPDATED FROM THE ORIGINAL USING CHATGPT!
# Updated for DSM 7.2.2 compatibility.
# STILL working on DSM 7.2.3-86009 Update 3 (6th July 26)
# This script will attempt to free up ports 80 and 443 used by the built-in nginx.
HTTP_PORT=81
HTTPS_PORT=444
BACKUP_FILES=true
BACKUP_DIR=/volume1/apps/free_ports/backup
DELETE_OLD_BACKUPS=false
KEEP_BACKUP_DAYS=30
NGINX_DIR="/usr/syno/share/nginx"
DATE=$(date +%Y-%m-%d-%H-%M-%S)
CURRENT_BACKUP_DIR="$BACKUP_DIR/$DATE"
if [ "$BACKUP_FILES" = "true" ]; then
mkdir -p "$CURRENT_BACKUP_DIR"
cp "$NGINX_DIR"/*.mustache "$CURRENT_BACKUP_DIR"
fi
if [ "$DELETE_OLD_BACKUPS" = "true" ]; then
find "$BACKUP_DIR/" -type d -mtime +$KEEP_BACKUP_DAYS -exec rm -r {} \;
fi
# Replace IPv4 listen ports 80 and 443
sed -i "s/^\([ \t]*listen[ \t]*\[\?:\?]*\)80\([^0-9]\)/\1$HTTP_PORT\2/" "$NGINX_DIR"/*.mustache
sed -i "s/^\([ \t]*listen[ \t]*\[\?:\?]*\)443\([^0-9]\)/\1$HTTPS_PORT\2/" "$NGINX_DIR"/*.mustache
# Replace IPv6 listen ports [::]:80 and [::]:443
sed -i "s/^\([ \t]*listen[ \t]*\[::\]:\)80\([^0-9]\)/\1$HTTP_PORT\2/" "$NGINX_DIR"/*.mustache
sed -i "s/^\([ \t]*listen[ \t]*\[::\]:\)443\([^0-9]\)/\1$HTTPS_PORT\2/" "$NGINX_DIR"/*.mustache
if command -v synoservicecfg >/dev/null; then
synoservicecfg --restart nginx
elif command -v synosystemctl >/dev/null; then
synosystemctl restart nginx
elif systemctl status nginx >/dev/null 2>&1; then
systemctl restart nginx
else
echo "Could not find nginx service restart command."
fi
echo "Port replacements complete. Here are the diffs:"
diff -u "$CURRENT_BACKUP_DIR" "$NGINX_DIR" | tee "$CURRENT_BACKUP_DIR/changes.log"
@Damotron500

Copy link
Copy Markdown
Author

I've recently updated my DS920+ to DSM 7.3.2-86009 Update 3. This script still works for me as of July 6th 2026. I'm using traefik over ports 80/443

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment