#clientaddress when behind reverse proxy

12 messages · Page 1 of 1 (latest)

olive frigate
#

SSR mode using Node adaptor with Caddy as reverse proxy server.

radiant martenBOT
#

Hi @olive frigate. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro’s docs don’t mention Caddy specifically, but they do explain how clientAddress is determined and how adapters can override it.

By default in SSR, Astro.clientAddress comes from the adapter via RenderOptions.clientAddress. If you’re behind a reverse proxy (like Caddy), you typically need to forward the real IP in a header (e.g. X-Forwarded-For) and have the Node adapter / server entrypoint pass that value into app.render().

The adapter API shows this pattern for setting clientAddress from x-forwarded-for when calling app.render()/nodeApp.render():

const clientAddress = request.headers.get("x-forwarded-for");
const response = await app.render(request, { clientAddress });

This value then becomes available as Astro.clientAddress in pages and ctx.clientAddress in API routes and middleware.[Render options; clientAddress API]

So, to get the correct IP when using the Node adapter behind Caddy:

  1. Configure Caddy to send the real client IP in a header (commonly X-Forwarded-For).
  2. Ensure your Node server / adapter entrypoint reads that header and passes it as clientAddress in the render options as shown above.

The docs do not provide a complete, ready-made Caddy+Node adapter example, only the generic clientAddress override mechanism.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@olive frigate Kapa.ai is still learning and improving, please let me know how I did by reacting below.

olive frigate
#

@radiant marten What exactly am I editing? Or can I just use that directly inside my frontmatter?

radiant martenBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You can’t use that clientAddress override directly in your Astro component frontmatter. It belongs in the server entrypoint / adapter layer, where app.render() (or nodeApp.render()) is called.

From the docs, clientAddress is a render option passed to app.render():

const clientAddress = request.headers.get("x-forwarded-for");
const response = await app.render(request, { clientAddress });

This is part of the adapter/server code that turns an incoming HTTP request into an Astro Request and then calls app.render(). It is not something you write inside --- frontmatter --- of a page. [Render options]

Once your server/adapter passes clientAddress like this, you can then read it in your Astro page frontmatter as Astro.clientAddress, or in API routes as ctx.clientAddress, but you don’t set it there. [clientAddress API]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@olive frigate Kapa.ai is still learning and improving, please let me know how I did by reacting below.

olive frigate
#

@radiant marten I'm running in server output mode using the node adaptor, I don't think I can update the app.render() directly?

radiant martenBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You’re right that when you use the official @astrojs/node adapter, you normally don’t touch app.render() yourself – the adapter’s own server entrypoint calls it internally.

The docs only show how an adapter could pass clientAddress into app.render() / nodeApp.render() via the RenderOptions.clientAddress option:

const clientAddress = request.headers.get("x-forwarded-for");
const response = await app.render(request, { clientAddress });

This is part of a custom server entrypoint (i.e. when you are building an adapter or writing your own server.js using App / NodeApp), not something you configure from page frontmatter. [Render options; Building a server entrypoint]

For the stock @astrojs/node adapter, the knowledge sources do not document:

  • any config flag to tell it “use X-Forwarded-For as clientAddress”, or
  • any hook where you can override clientAddress without writing your own entrypoint.

So, based on the docs provided:

  • Astro.clientAddress is available in SSR and is set by the adapter. [clientAddress API]
  • The mechanism to customize it is via RenderOptions.clientAddress in the server entrypoint. [Render options]
  • There is no explicit documentation here on how to change that behavior for the built‑in Node adapter without writing your own server entrypoint / adapter.
#

Given that, with the information available, you cannot adjust clientAddress from within your Astro pages or frontmatter, and the docs don’t show a supported way to tweak it for the existing Node adapter other than implementing your own server entrypoint or custom adapter code.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: