The Astro Node adapter (@astrojs/node) builds your on-demand pages into a Node.js server. In standalone mode, npm run build produces dist/server/entry.mjs, and node ./dist/server/entry.mjs starts a server that renders pages and serves your static assets, listening on the HOST and PORT environment variables. In middleware mode it exports a handler you mount inside Express, Fastify or another Node.js server.
Key Takeaways
- Install with
npx astro add node, then choosemode: 'standalone'ormode: 'middleware'.- Run the built server with
node ./dist/server/entry.mjs; setHOST=0.0.0.0in containers.- Behind a reverse proxy, set
security.allowedDomainsso forwarded host and client IP headers are trusted.- Use the same Node.js version to build and run: Node.js 22.23.4 or newer for current Astro releases.
Install and configure
# Terminal
npx astro add node
// astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
site: 'https://example.com',
output: 'server',
adapter: node({ mode: 'standalone' }),
});
output: 'server' renders every page on demand; leave it out to keep pages static by default and opt individual routes in with export const prerender = false. Prerendered pages are still built to HTML files and served by the same Node server. Our hybrid rendering tip explains the mix.
Build and run
# Terminal
npm run build
HOST=0.0.0.0 PORT=8080 node ./dist/server/entry.mjs
The standalone server reads PORT and HOST from the environment when they are set. HOST=0.0.0.0 is needed inside Docker and on most servers so the process accepts connections from outside localhost. The server serves files from dist/client/ and renders everything else.
You need dist/ and the production node_modules on the server. Install with npm ci --omit=dev there, or build inside the deployment image.
Middleware mode
To add Astro to an existing Node app, build in middleware mode and mount the exported handler:
// astro.config.mjs (adapter line)
adapter: node({ mode: 'middleware' }),
// server.mjs
import express from 'express';
import { handler as astro } from './dist/server/entry.mjs';
const app = express();
app.use(express.static('dist/client/'));
app.use(astro);
app.listen(8080);
In middleware mode Astro does not serve static files itself, so add a static file handler for dist/client/ before it. This suits apps that already have their own routes, authentication or websockets.
Behind nginx or a load balancer
Most production setups put the Node server behind a reverse proxy that terminates HTTPS:
# /etc/nginx/sites-available/example.com
server {
listen 443 ssl;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Astro only trusts X-Forwarded-Host, and the X-Forwarded-For value behind Astro.clientAddress, when the host matches security.allowedDomains. Without it, clientAddress is the proxy’s address. Add your domain:
// astro.config.mjs (inside defineConfig)
security: {
allowedDomains: [{ hostname: 'example.com', protocol: 'https' }],
},
More on this in getting the visitor’s IP in Astro.
Keep it running
Use a process manager so the server restarts on crashes and reboots. A minimal systemd unit:
# /etc/systemd/system/astro-site.service
[Unit]
Description=Astro site
After=network.target
[Service]
WorkingDirectory=/srv/astro-site
Environment=HOST=127.0.0.1 PORT=8080 NODE_ENV=production
ExecStart=/usr/bin/node ./dist/server/entry.mjs
Restart=always
User=www-data
[Install]
WantedBy=multi-user.target
Or package it in Docker, starting from an official node:22 image, copying the project, running npm ci && npm run build, and setting CMD ["node", "./dist/server/entry.mjs"]. Check the Node version tip so the image meets Astro’s minimum.
Other options
bodySizeLimitcaps request bodies (default 1 GB); lower it for public forms.experimentalDisableStreaming: trueturns off HTML streaming if a proxy or host buffers badly.
For a comparison with serverless adapters, see Astro adapters explained. Our Astro themes build as static sites, and adding this adapter is how you move one onto your own server.
FAQ
How do I run Astro SSR on my own server?
Install @astrojs/node with mode: 'standalone', run npm run build, then start node ./dist/server/entry.mjs with HOST and PORT set. Put nginx or another proxy in front for HTTPS.
What is the difference between standalone and middleware mode?
Standalone builds a complete server that serves pages and static files. Middleware mode exports a handler to mount in your own Express or Fastify server, which then serves static files and any other routes.
Why is Astro.clientAddress the proxy’s IP behind nginx?
Astro ignores forwarded headers unless the host matches security.allowedDomains. Add your domain there and make sure the proxy sends X-Forwarded-For and X-Forwarded-Host.
Which port does the Astro Node server use?
Whatever you set in the PORT environment variable. Set it explicitly in production rather than relying on a default.