I host a handful of my own sites, including this blog, on a Synology NAS at home. Web Station and the built-in reverse proxy cover most needs, but sooner or later you want something the GUI can't do: a bare-domain redirect, a custom header, a special location block, or a site that only exists to serve one file. That's when you reach for your own nginx config.
This guide shows the way I do it on DSM 7: a small config file of my own in nginx's include folder, using a Let's Encrypt certificate that DSM manages and renews. It also covers the gotchas that have bitten me, including the 502 errors you can get after a Web Station restart.
If all you need is HTTPS in front of an app, start with my Synology reverse proxy with Let's Encrypt guide instead. It's GUI-only and the safer option. This guide is for when you've outgrown it.
Prerequisites
- A Synology NAS on DSM 7 (I'm on 7.4) with Web Station installed.
- SSH enabled (Control Panel > Terminal & SNMP) and an admin account. My Synology SSH hardening guide covers doing that safely with keys.
- A domain with a DNS A record pointing at your public IP, here called
example.com. - Router port forwards for TCP 80 and 443 to the NAS. Port 80 matters even if you only want HTTPS: Synology's own help says port 80 must be open for Let's Encrypt domain validation and renewal.
- Comfort with a terminal. You'll be editing files as root, and a typo can take down every site on the NAS until you fix it.
A fair warning: Synology doesn't officially document hand-written nginx files. Everything here is reversible, but keep a copy of your config in your home folder and re-check it after DSM updates.
How nginx is wired on DSM 7
DSM generates its main config at /etc/nginx/nginx.conf. Don't edit that file, because DSM rewrites it. Instead, look at what it includes:
grep -n "include conf.d" /etc/nginx/nginx.conf
You'll see lines such as include conf.d/http.*.conf; inside the http block. On DSM 7, /etc/nginx/conf.d is a symlink to /usr/local/etc/nginx/conf.d. So any file you create there named http.something.conf is loaded as part of the http block, which means you can put complete server { } blocks in it.
List the folder and you'll see plenty of other files:
ls -la /usr/local/etc/nginx/conf.d/
Most are symlinks with long random IDs that Web Station and DSM packages create and manage. Leave them alone. Your own file should be a normal file with a clear name, such as http.mysite.conf.
Step 1: Get a Let's Encrypt certificate in DSM
Let DSM handle issuing and renewing the certificate. It does this well.
- Go to Control Panel > Security > Certificate and click Add.
- Choose Add a new certificate, then Get a certificate from Let's Encrypt.
- Enter your domain (
example.com), your email address, and any extra names such aswww.example.comunder Subject Alternative Name. - Click Done and wait for validation.
Synology's certificate help page explains the options, and notes that DSM renews Let's Encrypt certificates automatically before they expire, as long as port 80 is reachable.
Step 2: Find the certificate files
DSM keeps each certificate in its own folder with a random ID under /usr/syno/etc/certificate/_archive/. That folder is root-only, so this loop prints the names and expiry date for each one:
sudo sh -c 'for d in /usr/syno/etc/certificate/_archive/*/; do [ -f "$d/cert.pem" ] || continue echo "== $d" openssl x509 -in "$d/cert.pem" -noout -enddate openssl x509 -in "$d/cert.pem" -noout -text | grep -A1 "Subject Alternative Name" | tail -1 done'
Find the folder whose names match your domain. Inside it you'll find fullchain.pem (your certificate plus the chain) and privkey.pem. In the examples below I'll call that folder <CERT_ID>. Never post the contents of privkey.pem anywhere, and don't copy it into your web root.
Step 3: Write your server blocks
Create the file:
sudo vi /usr/local/etc/nginx/conf.d/http.mysite.conf
Here's a complete example. It redirects HTTP to HTTPS but keeps the Let's Encrypt validation path working on port 80, then serves a static site over HTTPS:
# HTTP: keep ACME validation working, send everything else to HTTPS
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location ^~ /.well-known/acme-challenge/ {
root /var/lib/letsencrypt;
default_type text/plain;
}
location / {
return 301 https://example.com$request_uri;
}
}
# HTTPS
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
ssl_certificate /usr/syno/etc/certificate/_archive/<CERT_ID>/fullchain.pem;
ssl_certificate_key /usr/syno/etc/certificate/_archive/<CERT_ID>/privkey.pem;
root /volume1/web/mysite;
index index.html;
add_header X-Content-Type-Options nosniff always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
location / {
try_files $uri $uri/ =404;
}
# Never serve hidden files such as .git or .env
location ~ /\. {
deny all;
}
}
A few notes:
- The
/var/lib/letsencryptpath is where DSM's own default server looks for challenge files. Confirm it on your NAS withgrep -n -A2 "acme-challenge" /etc/nginx/nginx.confand use whatever path it shows. - Don't add
default_serverto yourlistenlines. DSM already owns the default server on 80 and 443. - To proxy to an app (a container on port 8080, say) instead of serving files, replace the
location /block withproxy_pass http://127.0.0.1:8080;plus the usualproxy_set_header Host $host;andproxy_set_header X-Forwarded-Proto $scheme;lines. - Make sure the web server can read your files. On DSM, content under
/volume1/webis normally readable by thehttpgroup.
The nginx docs on configuring HTTPS servers explain each directive if you want to go further.
Step 4: Test, then reload
Always test before you reload. If the config is invalid, nginx refuses to load it, and on a NAS that can mean every site and the DSM web interface go down together.
sudo nginx -t
You want to see syntax is ok and test is successful. Then reload without dropping connections:
sudo nginx -s reload
The -t and -s switches are documented on the nginx command-line page. sudo systemctl reload nginx also works on DSM 7.
Step 5: Check it from the outside
From another machine (or your phone on mobile data), check the redirect and the certificate:
curl -sI http://example.com/ | head -3 curl -sI https://example.com/ | head -3 echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -subject -enddate
The first should show a 301 pointing at https://, the second a 200, and the third your domain with an expiry date in the future. To test from the NAS itself before DNS has updated, use curl --resolve example.com:443:127.0.0.1 -skI https://example.com/.
The Apache port 915 gotcha
If any of your Web Station sites use Apache HTTP Server 2.4 as their back-end, nginx doesn't serve them directly. It proxies them to Apache listening locally. On my NAS that's 127.0.0.1:915, which you can confirm with:
grep -rho "proxy_pass http://127.0.0.1:[0-9]*" /usr/local/etc/nginx/conf.d-available/ | sort | uniq -c
The gotcha: after a Web Station restart, a package update or a DSM update, nginx can come back up while Apache is still down. Your nginx-only sites (including the custom ones from this guide) work fine, while the Apache-backed ones return 502 Bad Gateway. It looks like you broke something with your new config, when really Apache is down. Check it like this:
sudo netstat -tlnp | grep ':915' curl -sI http://127.0.0.1:915/ | head -1 sudo systemctl status pkg-apache24
If nothing is listening on 915, restart Apache from Package Center > Apache HTTP Server 2.4 > Stop/Run, or:
sudo systemctl restart pkg-apache24
Your port number may differ, so trust what the grep shows over my 915.
Troubleshooting
nginx -tfails with "conflicting server name ... ignored". That's a warning, not an error, but it means another server block (often a Web Station portal or a reverse proxy rule) already claims that hostname. Remove the duplicate, or only one of them will ever answer.cannot load certificate. The path or<CERT_ID>is wrong, or you pointed atcert.peminstead offullchain.pem. Re-run the loop from Step 2.- Browser says the certificate is for a different name. Your request is reaching a different server block, usually DSM's default. Check that
server_namematches exactly, and that you're testing the right hostname. - Let's Encrypt renewal fails. Port 80 isn't reaching the NAS, or your port 80 block redirects the challenge path somewhere it can't be served. Keep the
acme-challengelocation as shown. - 403 Forbidden. File permissions. The
httpuser can't read your web folder. - Your config vanished after an update. Restore it from your home folder copy, run
nginx -t, and reload. This is why you keep a copy. - After a certificate renewal the old expiry still shows. nginx reads certificates when it loads. Check the date with the
openssl s_clientcommand above after the first renewal. If it's stale, runsudo nginx -s reload. A weekly DSM Task Scheduler job runningnginx -t && nginx -s reloadas root is a cheap safety net.
Wrapping up
The pattern is simple once you've seen it: let DSM own the certificate, keep your config in your own clearly named http.*.conf file, and never reload without nginx -t. It's how I run redirects and one-off hosts here without fighting Web Station. Just keep that backup copy handy, and re-check things after every DSM update.
Comments (0)
No comments yet. Be the first to leave one!
Leave a Comment