Log inSign up

Static website hosting with Nginx

Install Nginx, create a deploy user that owns /var/www/example.net, and push your built site with rsync into a new release folder that a current symlink points to. Add long cache headers for fingerprinted assets, short ones for HTML, and a Certbot certificate, and a small VPS serves the site quickly with instant rollbacks.

Topic: Websites, CMS and shops · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why host a static site on a VPS?

A static site is a folder of HTML, CSS, JavaScript and images, so Nginx can serve it straight from disk with almost no CPU or memory. Output from generators such as Hugo, Jekyll, Eleventy, Astro or a plain hand-written page all deploys the same way. Owning the server means you choose the cache rules, redirects and headers, and you can add a small API or a second site later on the same machine.

The smallest plans are enough; even A1-1 serves a busy static site. Check the monthly transfer of your plan with usage monitoring if you host large downloads or video. Before starting, point example.net and www.example.net at the server as shown in DNS records.

Which command sets up Nginx?

Install the distribution package and confirm it answers on port 80.

# Ubuntu and Debian
sudo apt update
sudo apt install -y nginx rsync
# Rocky Linux and AlmaLinux
sudo dnf install -y nginx rsync
sudo systemctl enable --now nginx

nginx -v
ss -tlnp | grep ':80'
curl -I http://127.0.0.1

The curl call should return HTTP/1.1 200 OK with the default welcome page. More on the server itself is in the Nginx guide.

How do you set up a deploy user and folder layout?

Use a normal account that owns the site files, so deploys never need root. The layout keeps every upload in its own release folder:

sudo adduser --disabled-password --gecos "" deploy
sudo mkdir -p /var/www/example.net/releases
sudo chown -R deploy:deploy /var/www/example.net
sudo chmod 755 /var/www/example.net

On Rocky and Alma create the user with sudo useradd -m deploy. Then copy your public key to /home/deploy/.ssh/authorized_keys (mode 600, folder 700) so your laptop or CI job can log in without a password; SSH keys and hardening covers the details. Nginx only needs read access, which the default permissions already give.

On Rocky and Alma with SELinux enforcing, label the folder so Nginx may read it:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.net(/.*)?"
sudo restorecon -Rv /var/www/example.net

How do you write the Nginx server block?

Point the root at the current symlink and let Nginx resolve clean URLs to HTML files.

server {
    listen 80;
    listen [::]:80;
    server_name example.net www.example.net;
    root /var/www/example.net/current;
    index index.html;

    location / {
        try_files $uri $uri.html $uri/ =404;
    }
    error_page 404 /404.html;

    location ~* \.(?:css|js|woff2|svg|png|jpg|jpeg|webp|avif|ico)$ {
        expires 1y;
        add_header Cache-Control "public, max-age=31536000, immutable";
        access_log off;
    }
    location ~* \.html$ {
        add_header Cache-Control "no-cache";
    }
    location ~ /\. { deny all; }

    gzip on;
    gzip_types text/css application/javascript application/json image/svg+xml;
}

On Ubuntu and Debian save it as /etc/nginx/sites-available/example.net and run sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/. On the RHEL family use /etc/nginx/conf.d/example.net.conf. Test and reload with sudo nginx -t && sudo systemctl reload nginx.

The one year lifetime is safe only for files whose names change when their content changes, which most site generators do with a hash in the file name. HTML stays no-cache so visitors always get the newest page, which then references the new asset names. If your generator does not fingerprint assets, use something like max-age=3600 instead. Compression options are discussed further in gzip and Brotli.

How do you deploy with rsync?

Build locally, upload into a new timestamped release folder, then switch the symlink in one step. Visitors never see a half-uploaded site.

#!/usr/bin/env bash
set -euo pipefail
HOST=deploy@198.51.100.24
BASE=/var/www/example.net
REL=$(date +%Y%m%d%H%M%S)

rsync -az --delete --link-dest="$BASE/current/" ./public/ "$HOST:$BASE/releases/$REL/"
ssh "$HOST" "ln -sfn $BASE/releases/$REL $BASE/current.tmp && mv -T $BASE/current.tmp $BASE/current"
ssh "$HOST" "cd $BASE/releases && ls -1t | tail -n +6 | xargs -r rm -rf"
echo "Deployed $REL"

Save it as deploy.sh in your project and replace ./public/ with your build output folder (_site, dist and so on). The --link-dest option hard-links unchanged files from the previous release, so each upload only transfers what changed and takes almost no extra disk. The mv -T rename is atomic, and the last line keeps the five newest releases. The rsync guide explains the flags in depth. On the very first deploy, current does not exist yet; rsync warns about --link-dest and simply copies everything.

How do you roll back a bad deploy?

Point current at the previous release folder; there is nothing to rebuild.

ls -1t /var/www/example.net/releases
ln -sfn /var/www/example.net/releases/20261201093000 /var/www/example.net/current.tmp
mv -T /var/www/example.net/current.tmp /var/www/example.net/current

Nginx follows the symlink on the next request, so no reload is needed. Because HTML is sent with no-cache, browsers pick up the older version straight away.

How do you add HTTPS and redirects?

Allow web traffic through the firewall and issue a certificate for both names, as described in the Certbot and Let's Encrypt guide.

# Ubuntu and Debian
sudo ufw allow 'Nginx Full'
# Rocky and Alma
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload

sudo certbot --nginx -d example.net -d www.example.net --redirect
curl -I https://example.net

The --redirect flag sends all plain HTTP to HTTPS. To keep a single canonical host, add a server block that answers for www.example.net with return 301 https://example.net$request_uri;; the patterns are in HTTPS and www redirects. Once everything works, consider adding add_header Strict-Transport-Security "max-age=31536000" always; to the HTTPS server block.

How do you check and troubleshoot the site?

Verify the headers each file type returns; they tell you whether caching behaves as planned.

curl -sI https://example.net/ | grep -i cache-control
curl -sI https://example.net/assets/app.3f9a1c.css | grep -iE 'cache-control|expires'
curl -sI -H 'Accept-Encoding: gzip' https://example.net/ | grep -i content-encoding
sudo tail -n 50 /var/log/nginx/error.log

A 403 Forbidden usually means the release folder is not readable (check with namei -l /var/www/example.net/current/index.html) or, on Rocky and Alma, an SELinux label is missing. A 404 on clean URLs often points to the build writing page/index.html while the link omits the trailing slash; the try_files line above covers both forms. Since a static site holds no database, the release folder plus your source repository is the full backup; the optional daily backups add-on covers the server configuration too.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1one or a few static sites1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
S1-2more disk for large media files1 vCPU, 2 GB RAM, 55 GB SSD$6.00/mo$72.00 for 12 months

Questions about Static site with Nginx and rsync

Can I host several static sites on one VPS?

Yes. Give each site its own folder and server block with its own server_name, and add each domain to Certbot.

Do I need PHP or a database?

No. Nginx serves the files directly. Add PHP only if a form handler or similar script needs it.

How do I deploy from a CI pipeline?

Store a dedicated private key as a CI secret, add its public key to the deploy user, and run the same deploy.sh after the build step.

Why do visitors still see the old CSS?

The asset file name did not change while a one year cache was set. Use fingerprinted names or a shorter max-age for unhashed files.

Is a 1 GB plan enough?

For a static site, yes. Nginx serving files uses very little memory; transfer volume is the figure to watch.