Skip to content

Instantly share code, notes, and snippets.

@hjbotha
Last active November 24, 2025 13:59
Show Gist options
  • Select an option

  • Save hjbotha/f64ef2e0cd1e8ba5ec526dcd6e937dd7 to your computer and use it in GitHub Desktop.

Select an option

Save hjbotha/f64ef2e0cd1e8ba5ec526dcd6e937dd7 to your computer and use it in GitHub Desktop.
Free ports 80 and 443 on Synology NAS
#! /bin/bash
# NEWLY ADDED BACKUP FUNCTIONALITY IS NOT FULLY TESTED YET, USE WITH CARE, ESPECIALLY DELETION
# Developed for DSM 6 - 7.0.1. Not tested on other versions.
# Steps to install
# Save this script in one of your shares
# Edit it according to your requirements
# Backup /usr/syno/share/nginx/ as follows:
# # cd /usr/syno/share/
# # tar cvf ~/nginx.tar nginx
# Run this script as root
# Reboot and ensure everything is still working
# If not, restore the backup and post a comment on this script's gist page
# If it did, schedule it to run as root at boot
# through Control Panel -> Task Scheduler
HTTP_PORT=81
HTTPS_PORT=444
BACKUP_FILES=true # change to false to disable backups
BACKUP_DIR=/volume1/apps/free_ports/backup
DELETE_OLD_BACKUPS=false # change to true to automatically delete old backups.
KEEP_BACKUP_DAYS=30
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 /usr/syno/share/nginx/*.mustache "$CURRENT_BACKUP_DIR"
fi
if [ "$DELETE_OLD_BACKUPS" == "true" ]; then
find "$BACKUP_DIR/" -type d -mtime +$KEEP_BACKUP_DAYS -exec rm -r {} \;
fi
sed -i "s/^\([ \t]\+listen[ \t]\+[]:[]*\)80\([^0-9]\)/\1$HTTP_PORT\2/" /usr/syno/share/nginx/*.mustache
sed -i "s/^\([ \t]\+listen[ \t]\+[]:[]*\)443\([^0-9]\)/\1$HTTPS_PORT\2/" /usr/syno/share/nginx/*.mustache
if which synoservicecfg; then
synoservicecfg --restart nginx
else
synosystemctl restart nginx
fi
echo "Made these changes:"
diff /usr/syno/share/nginx/ $CURRENT_BACKUP_DIR 2>&1 | tee $CURRENT_BACKUP_DIR/changes.log
@krenova
Copy link

krenova commented Apr 2, 2025

A little bit bit of finetuning and works like a charme :-)

@ctrlaltdelete007 what were the issues that required you to do some finetuning?

Edit: I was able to get it working, I just needed to reboot. I have my Synology set to run this script at reboot as well, which I believe is what fixed it. The script took the port away from Synology WebGUI at reboot.

@ZaxLofful , i ran the script at reboot as well but running sudo netstat -tulnp | grep ':443 ' shows that my ports are still tied to synology's nginx :( any thoughts?

I'm running DS723+ on DSM 7.2.2-72806 Update 1

@Damotron500
Copy link

I asked chatgpt to look at the script, explain in detail why it would no longer work on DSM 7.2+ and suggest some changes to fix. This was the result: https://gist.github.com/Damotron500/18d7e114df22631a428c9f44eb8b3b3d
It works for me. (DSM 7.2.2). Linking it here in case anyone wants to review. Its AI generated plagiarised from the original, so take with a pinch of salt.

@solidgonk
Copy link

solidgonk commented Oct 9, 2025

Can confirm the current gist works on DSM 7.3

@mjstark
Copy link

mjstark commented Nov 13, 2025

I'm running DSM 7.2.2 UPDATE 4 ...and the script still shows ports 443 open for IPv4/IPv6. Anyway suggestions on how to fix this....im no coder, but have a basic understanding.

image

@mjstark
Copy link

mjstark commented Nov 15, 2025

ctrlaltdelete007

Your screenshot shows port 443 and 80 open still, I wouldn't say the script worked :)

@mjstark
Copy link

mjstark commented Nov 16, 2025

Does not work with DSM 7.2.2 UPDATE 4 ....,must be another file that cotains port 443/80

@CamelT0E
Copy link

Script from @hjbotha works fine with DSM 7.3, 7.3.1 and 7.3.1 Update 1.

@mjstark
Copy link

mjstark commented Nov 20, 2025

Incremental versioning shouldn't matter.
7.x.
I'm certain the paths for nginx are the same for all Incremental versions. Ports 80/443 have been used for DSM 7.0 ...and above by nginx.

It surprises me that the script works for other Incremental versions of v7,from what I'm seeing ...but not others. The script should either work for v7 or not. That being said...the script does work for freeing up port 80 ....just not port 443. There has to be another file that contains port 443 that the script is missing. Any suggestions would be appreciated.

Thanks

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