🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Astro Nginx Config for Static Sites and SSR

An Astro Nginx config that serves dist/ with clean URLs, a custom 404, long caching for /_astro/, gzip and HTTPS redirects, plus a reverse proxy block for SSR.

#Astro#Nginx#Deployment

To serve an Astro site with Nginx, copy the contents of dist/ to a web root and use a server block with try_files $uri $uri/ $uri.html =404; and error_page 404 /404.html;. That serves Astro’s directory-style URLs, your custom 404 page and every asset. For on-demand rendering, Nginx sits in front of the Node.js server as a reverse proxy instead.

Key Takeaways

  • astro build writes about/index.html for /about/, which try_files ... $uri/ serves with Nginx’s index directive.
  • Point error_page 404 at Astro’s 404.html to show your own not-found page with a 404 status.
  • Cache /_astro/ for a year as immutable; revalidate HTML.
  • For SSR, proxy to the @astrojs/node standalone server and pass the original host headers.

Build and upload

# Terminal
npm run build
rsync -avz --delete dist/ deploy@server:/var/www/example.com/

--delete removes files that no longer exist in the build, such as old hashed assets. The trailing slash on dist/ copies its contents rather than the folder itself.

Static server block

# /etc/nginx/sites-available/example.com
server {
  listen 80;
  listen [::]:80;
  server_name example.com www.example.com;
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl;
  listen [::]:443 ssl;
  http2 on;
  server_name www.example.com;

  ssl_certificate     /etc/letsencrypt/live/www.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

  root /var/www/example.com;
  index index.html;

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

  location /_astro/ {
    add_header Cache-Control "public, max-age=31536000, immutable";
    try_files $uri =404;
  }

  location / {
    add_header Cache-Control "public, max-age=0, must-revalidate";
    try_files $uri $uri/ $uri.html =404;
  }

  error_page 404 /404.html;
}

$uri.html covers projects that set build.format: 'file', where /about is written as about.html. The http2 on; directive needs Nginx 1.25.1 or newer; on older versions put http2 on the listen line instead. Check the config and reload:

# Terminal
sudo nginx -t && sudo systemctl reload nginx

Trailing slashes

With the default directory format, Nginx redirects /about to /about/ because about is a directory. That matches Astro’s default output. If you set trailingSlash: 'never' in astro.config.mjs, use build.format: 'file' as well, so files and URLs agree. Our trailing slash tip explains the combinations.

Security headers

Add headers in the server block. Note that an add_header inside a location replaces those inherited from the server level, so repeat them or use an include file:

# /etc/nginx/snippets/security-headers.conf
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header X-Frame-Options "DENY" always;

Then include snippets/security-headers.conf; inside each location. Our security headers tip explains each one.

Reverse proxy for SSR

With @astrojs/node in standalone mode running on port 4321:

# inside the HTTPS server block
location /_astro/ {
  root /var/www/example.com/client;
  add_header Cache-Control "public, max-age=31536000, immutable";
}

location / {
  proxy_pass http://127.0.0.1:4321;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
}

Serving /_astro/ from dist/client/ directly takes static asset traffic off Node.js. Astro reads forwarded host headers only from domains listed in security.allowedDomains, so add your domain there if you rely on Astro.url behind the proxy. Run the Node process under systemd or a container; our Docker tip has an image.

The static deploy docs cover managed hosts if you would rather not run a server. Every theme in our Astro theme catalog builds to plain files that this config serves as-is.

FAQ

How do I serve an Astro build with Nginx?

Copy dist/ to your web root, set root to it, and use try_files $uri $uri/ $uri.html =404; with error_page 404 /404.html;. Reload Nginx after nginx -t passes.

Why does Nginx show its own 404 page instead of Astro’s?

error_page 404 /404.html; is missing, or 404.html is not in the web root. Astro writes it to dist/404.html when you have src/pages/404.astro.

Does an Astro site need a rewrite to index.html like a single-page app?

No. Astro builds a real HTML file for every route, so do not rewrite everything to /index.html. That would hide 404s and break direct links.

Can Nginx serve an Astro SSR site?

Yes, as a reverse proxy in front of the Node.js server from @astrojs/node. Proxy page requests to the Node port and serve /_astro/ assets directly from dist/client/.