Astro SSR on your own server means adding the @astrojs/node adapter in standalone mode, building with npm run build, and starting node ./dist/server/entry.mjs with the right HOST and PORT behind Nginx. Read secrets at runtime with process.env or astro:env, keep the process running, and prerender every page that doesn’t need a request.
Most Astro SSR guides end at “pick a serverless adapter”. That works until you need a login page, an API route or per-request data on a server you control, next to your other sites, with no per-invocation bill. If you’re still deciding whether Astro fits at all, our beginner’s guide to Astro JS covers islands and rendering modes first.
This guide is about the server side only: what the Node adapter actually builds, which mode to pick, why the HOST and PORT values cause most first-day 502s, how runtime environment variables differ from build-time ones, and how Astro 7’s route caching works on a single server. For the static-vs-SSR decision and moving off Vercel, Netlify or Cloudflare, read how to deploy an Astro site from GitHub to your own server.
TL;DR
Short on time? Here is the whole guide in a few lines:
- SSR needs an adapter.
npx astro add nodeinstalls@astrojs/nodeand wires it intoastro.config.mjs. - Pick standalone mode unless an Express or Fastify app must stay in charge. Standalone starts a server and serves
dist/client/for you. - Start it with
node ./dist/server/entry.mjs. It listens on theHOSTandPORTyou pass in. Behind Nginx, that is127.0.0.1and one fixed port. import.meta.envis baked in at build time. Runtime secrets come fromprocess.envorgetSecret()fromastro:env/server.- Prerender everything that doesn’t need a request. One
prerenderline per page keeps the Node process small. - On xCloud, the server side is set up for you. Git deploy detects
@astrojs/node, picks SSR (Node process), passes host and port, loads the site’s.envand keeps the process up with PM2.
What xCloud Sets for an Astro SSR Process
Here is the finding this guide is built on. Most “Astro SSR 502” threads come down to one mismatch: the app listens on one address, the web server knocks on another. xCloud removes that guesswork, and this table shows exactly what it sets, taken from xCloud’s deploy scripts and Git framework detection:
| Setting | What xCloud does for an @astrojs/node site |
Where you see it |
|---|---|---|
| Detection | @astrojs/node in package.json → framework Astro, app type Node.js, serving mode SSR (Node process) |
Review screen when you add the repo |
| Build command | npm run build (only when your package.json has a build script, otherwise a warning asks you to set one) |
Build & serving |
| Start command | node ./dist/server/entry.mjs |
Build & serving, later SSR Configuration |
| HOST | 127.0.0.1, so the site is only reachable through your domain, not by server IP and port |
Set on the process, nothing to type |
| PORT | The site’s Port (a free port on the server, 3000 if none is set) | Build & serving, later SSR Configuration |
| Environment | The site’s .env values are given to the process; saving them restarts it |
Environment on the site |
| Process manager | PM2 runs the process, restarts it after each deploy, and keeps it in the saved process list | Monitoring → Logs → PM2 Log |
If you only remember one line: the Node adapter reads HOST and PORT at startup, so whoever starts the process decides where it listens. On a plain VPS, that’s you. On xCloud, it’s the deploy.

When You Actually Need SSR in Astro
Astro prerenders your whole site by default. Astro’s on-demand rendering guide says so in its first lines, and advises starting in the default static mode “until you are sure that most or all of your pages will be rendered on demand”.
You need the Node adapter when at least one route must run on each request:
| You need… | Example | SSR required? |
|---|---|---|
| Pages that change per visitor | Account page, dashboard, cart | ✅ Yes |
| API endpoints | src/pages/api/contact.ts that writes to a database |
✅ Yes |
| Astro Actions or sessions | Form handling, logged-in state | ✅ Yes |
Server islands (server:defer) |
A personalised block on a static page | ✅ Yes |
| Content that changes on publish | Blog, docs, landing pages | ❌ No: rebuild on push instead |
| Search, comments, analytics | Third-party widgets in the browser | ❌ No |
Rule of thumb: if you can’t name a route that must be fresh on every request, stay static. A static Astro site needs no Node process at all, and xCloud serves it from dist/ with Nginx or OpenLiteSpeed.
Install @astrojs/node in Standalone Mode
Run the installer from your project folder:
npx astro add node
It installs @astrojs/node and updates your config. Check that astro.config.mjs ends up like this:
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
output: 'server', // optional: every page on demand by default
adapter: node({ mode: 'standalone' }),
});
Leave output out if most of your site is static. You then opt single pages into SSR (see the hybrid section below).
Standalone vs middleware mode
The @astrojs/node guide describes two modes. The difference is who owns the HTTP server.
| Standalone | Middleware | |
|---|---|---|
| What the build gives you | A server that starts when you run the entry file | A handler you mount in your own server |
| Start command | node ./dist/server/entry.mjs |
node server.mjs (your file) |
Serves dist/client/ files |
✅ Yes, with long cache headers on hashed _astro/ assets |
❌ No, you add a static file route |
| Best for | A plain Astro site with some SSR routes | An existing Express or Fastify app that adds Astro pages |
| Works with xCloud’s detected start command | ✅ Yes | Change the start command to your own server file |
Middleware mode looks like this in an Express app (adapted from the adapter docs):
import express from 'express';
import { handler as ssrHandler } from './dist/server/entry.mjs';
const app = express();
app.use('/', express.static('dist/client/'));
app.use(ssrHandler);
app.listen(process.env.PORT, process.env.HOST);
Notice the last line: in middleware mode your server binds the port, so read PORT and HOST from the environment instead of hardcoding 8080. That single habit prevents most 502s after a move.
HOST, PORT and the 502 Every Serverless Guide Skips
A serverless platform hides the port from you. Your own server doesn’t. The standalone server listens wherever HOST and PORT tell it to:
HOST=127.0.0.1 PORT=3000 node ./dist/server/entry.mjs
Nginx then forwards your domain to http://127.0.0.1:3000. If nothing answers there, visitors get 502 Bad Gateway, even though the build passed and the deploy log looks green.
Which HOST to use
| Where the process runs | HOST | Why |
|---|---|---|
| Behind Nginx on the same server | 127.0.0.1 |
Only reachable through your domain, not by server IP and port |
| Inside a Docker container | 0.0.0.0 |
The container’s port mapping must reach it from outside the container |
| On your laptop | leave it empty | Astro’s default is fine for local testing |
Here’s the part that trips people up: localhost is not the same as 127.0.0.1 on current Node.js versions. It can resolve to the IPv6 address ::1, so the app works when you curl localhost over SSH, while the web server finds nothing at 127.0.0.1. Our Node.js deploy guide lists this as a common 502 cause on xCloud.
The 502 checklist
- Read the process log first. A missing environment variable that crashes the app at startup looks exactly like a port problem from the outside. On xCloud: Monitoring → Logs → PM2 Log.
- Check the port matches. The port in SSR Configuration must be the port the process listens on. Don’t hardcode one in
astro.config.mjsorserver.mjs. - Check the host. Don’t override
HOSTwithlocalhostin the start command or the.envfile. - Check the start command.
astro previewis for local testing. Production runsnode ./dist/server/entry.mjs. - Check the build wrote
dist/server/entry.mjs. If the adapter isn’t inastro.config.mjs, the build is static and there is nothing to start.

Runtime Env Vars and Secrets
Astro has two kinds of environment values, and mixing them up gives you an empty API key in production.
| You read it with | When the value is fixed | Use it for |
|---|---|---|
import.meta.env.MY_VAR |
At build time: Vite replaces it in the code | Values that never change between builds; PUBLIC_ values the browser may see |
process.env.MY_VAR |
At runtime, on each start of the process | Secrets and per-server values in SSR routes |
getSecret('MY_VAR') from astro:env/server |
At runtime | Typed, validated server secrets declared in your config |
The Astro environment variables guide is explicit that import.meta.env is statically replaced at build time, while on-demand pages can read variables at runtime and “with most adapters you can access environment variables with process.env”.
For a typed, checked secret, declare it in your config:
import { defineConfig, envField } from 'astro/config';
export default defineConfig({
env: {
schema: {
API_SECRET: envField.string({ context: 'server', access: 'secret' }),
},
},
});
Then read it in a page or endpoint with import { API_SECRET } from 'astro:env/server'. Secret server variables stay out of the client bundle.
Two catches worth knowing. First, the Node adapter docs say “neither Astro nor the adapter loads environment variables for you”, so on a plain VPS you set them in the start command, a systemd unit or your process manager. Second, astro:env doesn’t work inside astro.config.mjs itself; use process.env there.
On xCloud, add the values under Advance → Environment file (.env) when you create the site, or later under Environment. The SSR process gets them at startup, and saving them restarts it. 👉 Environment variables and Git deployments in xCloud
Keep the Node Process Up
A standalone Astro server is one Node process. If it crashes or the server reboots, your SSR pages are gone until something starts it again. On a DIY server you need three things:
- A process manager (PM2 or a systemd unit) that restarts the process on a crash and at boot.
- A restart after each deploy, or the old build keeps serving.
- Logs you can read when the process dies at 3 a.m.
On xCloud this is part of the site. The process runs under PM2, and a push to your branch (with Auto-deploy on push (webhook) on) pulls the code, rebuilds and restarts it. To change the port or the start command later, open SSR Configuration on the site; saving restarts the process.

Two upgrades once traffic grows:
- 👉 All CPU cores: run your own PM2 ecosystem file in cluster mode. See PM2 cluster mode for Node.js apps in xCloud.
- 👉 No dropped requests while deploying: turn on Release mode. See zero-downtime deploys for Node.js apps.
Route Caching on Your Own Server (Astro 7)
Astro 7.0 added route caching, and it ships a built-in in-memory provider that works with the Node adapter. The route caching guide shows this config:
import { defineConfig, memoryCache } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
adapter: node({ mode: 'standalone' }),
cache: { provider: memoryCache() },
});
Then cache a server-rendered page for two minutes, and serve the stale copy for one more minute while it refreshes:
---
Astro.cache.set({ maxAge: 120, swr: 60, tags: ['home'] });
---
You can clear entries by tag or path with cache.invalidate(), or set rules per route with routeRules in the config.
Know the limits before you count on it. Caching doesn’t happen in astro dev, so test with a build. And an in-memory cache lives inside one Node process: it is empty after every restart, and with PM2 cluster mode each process keeps its own copy. For a page that is the same for everyone, prerendering it is still simpler than caching it.
Hybrid: Prerender Some Pages
Most real Astro sites are mostly static with a few live routes. You have two ways to say that:
| Your config | Pages are… | Mark the exceptions with |
|---|---|---|
Default (output not set) |
Static | export const prerender = false on each SSR page or endpoint |
output: 'server' |
Rendered on request | export const prerender = true on each static page |
A typical setup keeps the default and opts in a few routes:
---
// src/pages/account.astro
export const prerender = false;
const user = Astro.locals.user;
---
Prerendered pages are written to dist/client/ at build time, and the standalone server sends them as files. Only the routes marked for on-demand rendering run code per request, so the Node process stays small.
Deploy Astro SSR on xCloud From Git
Once @astrojs/node is in your package.json and pushed, the deploy is a few clicks. 👉 Every screen with screenshots: How to deploy an Astro site from Git on xCloud.
- Go to Sites → Add New Site, click the Astro card and pick a server with the Nginx or OpenLiteSpeed stack.
- Paste your repository URL (or pick a private one from your connected Git account). xCloud shows Native app with an Astro label and App Type: Node.js.
- Under Build & serving, check Serving mode: SSR (Node process), the start command
node ./dist/server/entry.mjsand the Port. - Open Advance and add your runtime values in Environment file (.env).
- Click Deploy. When the deployment shows Deployed and LIVE, open the site and test one SSR route and one prerendered page.
- Turn on Auto-deploy on push (webhook), so each push to the branch rebuilds the site and restarts the process.

If a deployment fails, open it under Deployments and read Build steps. 👉 Troubleshooting Git deployments
What xCloud Does (and Doesn’t Do) for Astro SSR
xcloud.host itself is built with Astro, so this is a stack we use every day. Here is the honest split:
| ✅ xCloud does | ❌ xCloud doesn’t |
|---|---|
Detects @astrojs/node and fills in SSR settings |
Keep serverless adapters: @astrojs/vercel, @astrojs/netlify and @astrojs/cloudflare are removed at deploy and the site builds static |
| Runs the process with PM2 on the site’s host and port | Run edge functions or a global CDN cache for your SSR routes |
Gives the site’s .env to the process and restarts it on change |
Change your code: a hardcoded port or localhost host is yours to fix |
| Rebuilds and restarts on every push | Share an in-memory route cache between PM2 processes or servers |
The trade-off: a serverless host scales SSR routes to zero and back for you. A server of your own gives you a fixed monthly cost, your other sites and databases next door, and no cold starts, but one server is one region. For most content sites with a few live routes, that trade is worth it.
Run Astro SSR on a server you control
Start with Astro hosting on xCloud, or see Node.js Hosting for every Node app you run. Git deploys are covered on the One Click Git Deployment page, and plans are on pricing. Picking a content backend for the static half? See the best CMS for Astro.
To undo SSR, remove the adapter from astro.config.mjs and package.json and push. Add the repository again and xCloud detects a Static build served from dist/.
Ship Your First Astro SSR Route This Week
The Node adapter is not the hard part of Astro SSR. The server around it is: which address the process listens on, which values it reads at runtime, and what restarts it after a crash or a deploy.
This week, add @astrojs/node in standalone mode, move one page or endpoint to prerender = false, read its secret with process.env or astro:env, and deploy it from Git. Test one SSR route and one static page before you point your domain at it.
If you have found this blog helpful, feel free to subscribe to our blogs for valuable tutorials, guides, knowledge, and tips on web hosting and server management. You can also join our Facebook community to share insights and engage in discussions.
Frequently Asked Questions
Does Astro SSR need Vercel, Netlify or Cloudflare?
No. Astro SSR needs an adapter, and @astrojs/node turns your build into a plain Node.js server. You can run that server on any VPS behind Nginx or OpenLiteSpeed, with no serverless platform involved.
Should I use standalone or middleware mode?
Use standalone unless you already have an Express or Fastify app that must stay in charge. Standalone starts its own server and serves your static files. Middleware gives you a handler to mount in your own server, and you serve the files yourself.
Why does my Astro SSR site return 502 Bad Gateway?
The web server can’t reach the Node process on the address and port it expects. Usually the process crashed at startup, listens on a different port, or listens only on localhost resolved to IPv6. Read the process log first, then check HOST and PORT.
Why is my environment variable empty at runtime?
You probably read it with import.meta.env, which Astro replaces at build time. Read runtime values with process.env or with getSecret from astro:env/server, and make sure the variable is set for the running process, not only for the build.
Can I mix static and server-rendered pages in one Astro site?
Yes. Keep the default static output and add export const prerender = false to the pages that must render on request. With output: ‘server’, do the reverse and mark static pages with export const prerender = true.
Does xCloud keep my Astro SSR process running?
Yes. xCloud runs it with PM2, gives it the site’s host and port, and restarts it after each deploy and after you change the site’s environment. Its output is under Monitoring → Logs → PM2 Log.
