DevOpsExample
Deploying a static Next.js site with GitHub Actions and nginx
Static export, rsync upload, Let's Encrypt certificate and nginx configuration, on every push.
A Next.js site exported as static files only needs a web server. With GitHub Actions, every push to the main branch can build it, upload it to the server and reload nginx, with no manual step.
Overview
| Step | Tool | Result |
|---|---|---|
| Build | next build | out/ folder of HTML, CSS and JS files |
| Upload | rsync over SSH | Files copied to the server |
| Secure | Certbot | Let's Encrypt HTTPS certificate |
| Serve | nginx | Site online, with caching and compression |
Exporting the site
In the Next.js config, static export produces an out/ folder ready to serve:
const config: NextConfig = {
output: "export",
trailingSlash: true, // one page = one folder with index.html
images: { unoptimized: true },
};
The GitHub Actions workflow
The workflow runs on every push that touches the site. The server's SSH key is stored in the repository secrets, never in the code.
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- run: pnpm install --frozen-lockfile && pnpm build
- run: rsync -az --delete out/ deploy@server:/var/www/site/
The HTTPS certificate
Certbot obtains a Let's Encrypt certificate for the domain. In standalone mode, it stops nginx for a few seconds to answer the challenge itself.
The nginx configuration
nginx serves the files, returns a real 404 page and caches the assets whose names change with each version:
location / {
try_files $uri $uri/ =404;
}
location /_next/static/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
error_page 404 /404.html;
Checking the deployment
Once the workflow is done, check that the site responds, that the http address redirects to https and that an unknown page returns a 404 error.