Skip to main content

Reverse Proxy Configuration

In production, PRISM sits behind a reverse proxy (nginx, Apache, or Caddy) that handles TLS termination, static assets, and request routing.

warning

Your reverse proxy must strip the X-Prism-Bypass header from incoming requests. This header is used internally by PRISM to prevent render loops. If an external client sends it, they can bypass rendering entirely.

Ready-to-use versions of the configurations below ship in the repository at dist/reverse-proxy/nginx-prism.conf and dist/reverse-proxy/apache-prism.conf, so they can be copied rather than transcribed out of prose. Both are syntax-checked against nginx and httpd.

Failing open​

PRISM is in front of every page request, not only the ones it renders. In bot-only mode a visitor's request is proxied straight to the origin, so a visitor never needs PRISM to be healthy — but with a single upstream the edge still answers them 502 whenever PRISM is stopped, restarting or being upgraded.

Every configuration below therefore gives visitors a second route:

  • Visitors fail open. While PRISM cannot be reached, their requests go to the origin directly. Their browser runs the application as it always did.
  • Crawlers do not. A crawler handed the unrendered shell with a 200 indexes an empty page. Crawlers are routed to PRISM only, receive a 5xx while it is down, and retry — which is what a 5xx asks of them.

The fallback is taken only when PRISM cannot be connected to. A slow response is not retried against the origin, and a POST that has already been sent is never replayed.

Two things to check when adapting these:

  • The fallback sends the origin the public Host, exactly as the static-asset route already does. PRISM itself sends the origin's own host name plus X-Forwarded-Host. An origin that only answers to its internal name needs a matching virtual host for the fallback to work.
  • The crawler list is deliberately narrower than PRISM's own bot detection. It has to cover the clients for which an unrendered 200 does lasting harm, not every script with a non-browser User-Agent.

All three were exercised against stand-in servers: with PRISM stopped, a browser User-Agent is served by the origin and Googlebot receives a 5xx; when PRISM returns, both go back to it.

What the visitor path does not carry​

A visitor's request passes through PRISM to your origin as a streamed HTTP/1.1 request. That path has limits, and anything outside them has to be routed around PRISM at your edge.

Not carried or limitedBehaviourWhat to do
Upgrade requests (WebSocket)PRISM drops the Upgrade header as hop-by-hop, so the handshake fails.The shipped configurations send any request with an Upgrade header straight to the origin. Keep that rule when you adapt them.
Request bodiesRefused with 413 above server.max_request_bytes (10 MiB). 0 removes the limit.Raise the limit, or send upload URLs to the origin at the edge.
Response bodiesRefused with 502 (declared length) or aborted mid-stream above server.max_response_bytes (50 MiB). 0 removes the limit.Raise the limit, or serve large downloads and media from the origin or a CDN.
Time to response headers504 if the origin sends no response headers within server.proxy_timeout_secs (30 s). The clock runs from the last request body frame sent, so a slow upload that keeps moving is not cut off, but one that stops for that long is.Raise proxy_timeout_secs for slow endpoints.
Gaps in the responseThe response is aborted if the origin pauses between body frames for longer than server.origin_response_idle_secs (defaults to proxy_timeout_secs).Raise it for server-sent events or long polling, or route those URLs to the origin at the edge.
Gaps in the requestA client that sends no request body data for server.body_read_timeout_secs (30 s) gets 408.Raise it for very slow uploads.

Edge timeouts apply as well: nginx's proxy_read_timeout and Apache's ProxyTimeout are 30 s in the examples, and also cap how long an event stream or an upgraded connection may stay silent.

Idle connections to the origin are kept for server.origin_keepalive_secs (4 s), shorter than the 5 s that Node.js and Apache close at by default. If a connection is closed under a GET, HEAD or OPTIONS request that has no body, PRISM sends it once more (counted in prism_origin_retries_total); other requests are never repeated. If your origin closes idle connections sooner than 4 s, lower the setting.

Nginx​

:::danger Put every proxy_set_header in the same block

proxy_set_header directives are not merged across levels. A location that defines even one of its own discards all of them from the enclosing server block. Writing the bypass strip at server level and proxy_set_header Connection "" inside location / therefore silently drops the strip, and a client can forge X-Prism-Bypass.

Verified: with the directives split across levels a forged header arrives at the backend intact; with them together it arrives empty. :::

# Who must never be handed the unrendered application shell.
#
# When PRISM cannot be reached, a visitor's browser can run the application
# from the origin, so visitors fail open. A crawler cannot: it would index an
# empty shell with a 200. Crawlers therefore get nginx's own 502 and come
# back later, which is what a 5xx tells them to do.
#
# This list is deliberately narrower than PRISM's own bot detection. It only
# has to cover the clients for which an unrendered 200 does lasting harm.
map $http_user_agent $prism_upstream {
default prism_failopen;
"~*(googlebot|google-inspectiontool|storebot-google|googleother|adsbot-google|bingbot|bingpreview|yandex|baiduspider|duckduckbot|applebot|petalbot|seznambot|amazonbot)" prism_only;
"~*(gptbot|chatgpt-user|oai-searchbot|claudebot|claude-user|claude-searchbot|perplexitybot|bytespider|meta-externalagent|ccbot)" prism_only;
"~*(facebookexternalhit|twitterbot|linkedinbot|pinterestbot|slackbot|discordbot|whatsapp|telegrambot)" prism_only;
}

# Requests asking for a protocol upgrade (WebSocket) go straight to the origin.
# PRISM does not carry upgrades — it drops the Upgrade header, so the handshake
# fails — and a socket has nothing to render. The origin is the only place such
# a request can work, so this also holds while PRISM is down.
map $http_upgrade $prism_target {
"" $prism_upstream;
default prism_origin;
}

# Forward the upgrade handshake only when there is one; everything else keeps
# the empty Connection header that lets nginx reuse its upstream connections.
map $http_upgrade $prism_connection {
"" "";
default "upgrade";
}

# Upgrades: the origin itself.
upstream prism_origin {
server 127.0.0.1:3000;
keepalive 32;
}

# Crawlers: PRISM or nothing.
upstream prism_only {
server 127.0.0.1:4000;
keepalive 32;
}

# Everyone else: PRISM, and the origin itself when PRISM cannot be reached.
# `backup` is only used while the first server is failing.
upstream prism_failopen {
server 127.0.0.1:4000;
server 127.0.0.1:3000 backup;
keepalive 32;
}

server {
listen 443 ssl;
http2 on;
server_name example.com;

ssl_certificate /etc/ssl/certs/example.com.pem;
ssl_certificate_key /etc/ssl/private/example.com.key;

# Chrome rendering can take a while on a cold cache. This also closes an
# upgraded connection (WebSocket) after 30 s without traffic: raise it, or
# have the application send pings.
proxy_read_timeout 30s;
proxy_connect_timeout 5s;

location / {
proxy_pass http://$prism_target;
proxy_http_version 1.1;
proxy_set_header Connection $prism_connection;
proxy_set_header Upgrade $http_upgrade;

# Fall back only when PRISM cannot be connected to or the connection
# breaks — not on a slow response, which would send the same request
# to the origin a second time. nginx never retries a non-idempotent
# request (POST, PATCH) that it has already started sending.
proxy_next_upstream error;

# CRITICAL: strip the bypass header so a client cannot forge it.
proxy_set_header X-Prism-Bypass "";

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

# Static assets straight from the origin, skipping rendering entirely.
location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|wasm|map)$ {
proxy_pass http://127.0.0.1:3000;

# Repeated on purpose: this block has its own proxy_set_header, so it
# inherits nothing from above.
proxy_set_header X-Prism-Bypass "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}

Check the strip is working before trusting it:

curl -s -H 'X-Prism-Bypass: forged' https://example.com/ -o /dev/null -w '%{http_code}\n'
# then confirm the origin logged no X-Prism-Bypass value

Apache​

Requires proxy, proxy_http, proxy_balancer, lbmethod_byrequests, slotmem_shm, rewrite, headers and ssl. If your site uses WebSocket, also enable proxy_wstunnel; where that module is not available, httpd 2.4.47 and later can tunnel WebSocket through mod_proxy_http instead, by uncommenting the <Proxy> block in the example:

a2enmod proxy proxy_http proxy_balancer lbmethod_byrequests slotmem_shm rewrite headers ssl proxy_wstunnel # Debian/Ubuntu
<VirtualHost *:443>
ServerName example.com

SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.pem
SSLCertificateKeyFile /etc/ssl/private/example.com.key

# Never act as a forward proxy.
ProxyRequests Off

# CRITICAL: strip the bypass header so a client cannot forge it.
# `early` runs before the request is handed to the proxy; without it the
# unset can be evaluated too late to matter on some configurations.
RequestHeader unset X-Prism-Bypass early

ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"

# Chrome rendering can take a while on a cold cache.
ProxyTimeout 30

# Fail open for visitors.
#
# PRISM is in front of every page request, so without a fallback a PRISM
# that is stopped, restarting or being upgraded answers every visitor with
# 503 — over a renderer most of them never use. The second member is a
# hot standby: it receives traffic only while PRISM cannot be reached.
<Proxy "balancer://prism">
BalancerMember "http://127.0.0.1:4000" connectiontimeout=5 timeout=30 keepalive=On retry=5
BalancerMember "http://127.0.0.1:3000" status=+H
</Proxy>

# Crawlers: PRISM or nothing.
#
# A visitor's browser can run the application from the origin; a crawler
# would index an empty shell with a 200. Crawlers are therefore sent to
# PRISM directly, get a 5xx when it is down, and come back later. The list
# is deliberately narrower than PRISM's own bot detection: it only has to
# cover clients for which an unrendered 200 does lasting harm.
RewriteEngine On

# Upgrades (WebSocket): the origin itself.
#
# PRISM does not carry them — it drops the Upgrade header, so the handshake
# fails — and a socket has nothing to render. These rules come first so a
# crawler's upgrade request is not sent to PRISM either. A socket idle for
# longer than ProxyTimeout is closed: raise it, or have the application
# send pings.
#
# WebSocket needs one of two things. With mod_proxy_wstunnel loaded
# (a2enmod proxy_wstunnel) the first rule tunnels it. Where that module is
# not available — some distributions no longer ship it — httpd 2.4.47 and
# later can tunnel through mod_proxy_http instead: uncomment the <Proxy>
# block. With neither, an upgrade request reaches the origin as a plain
# request and the handshake fails there.
<IfModule mod_proxy_wstunnel.c>
RewriteCond %{HTTP:Upgrade} "^websocket$" [NC]
RewriteRule "^/(.*)$" "ws://127.0.0.1:3000/$1" [P,L]
</IfModule>
#<Proxy "http://127.0.0.1:3000/">
# ProxySet upgrade=websocket
#</Proxy>
RewriteCond %{HTTP:Upgrade} "."
RewriteRule "^/(.*)$" "http://127.0.0.1:3000/$1" [P,L]

RewriteCond %{HTTP_USER_AGENT} "(googlebot|google-inspectiontool|storebot-google|googleother|adsbot-google|bingbot|bingpreview|yandex|baiduspider|duckduckbot|applebot|petalbot|seznambot|amazonbot|gptbot|chatgpt-user|oai-searchbot|claudebot|claude-user|claude-searchbot|perplexitybot|bytespider|meta-externalagent|ccbot|facebookexternalhit|twitterbot|linkedinbot|pinterestbot|slackbot|discordbot|whatsapp|telegrambot)" [NC]
RewriteRule "^/(.*)$" "http://127.0.0.1:4000/$1" [P,L]

ProxyPass / "balancer://prism/"
ProxyPassReverse / "balancer://prism/"
ProxyPassReverse / "http://127.0.0.1:4000/"

# mod_proxy appends the client address to X-Forwarded-For itself, and
# ProxyPreserveHost passes the original Host, so neither needs setting.
</VirtualHost>

<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>

mod_proxy appends the client address to X-Forwarded-For itself, so it does not need setting; ProxyPreserveHost On passes the original Host.

Caddy​

example.com {
# Strip bypass header
request_header -X-Prism-Bypass

# Upgrades (WebSocket) go straight to the origin: PRISM does not carry them.
@upgrade header Upgrade *
handle @upgrade {
reverse_proxy 127.0.0.1:3000
}

# Crawlers: PRISM or nothing. They must never be handed the unrendered
# shell with a 200, so they get a 502 while PRISM is down and come back.
@crawler header_regexp User-Agent (?i)(googlebot|google-inspectiontool|storebot-google|googleother|adsbot-google|bingbot|bingpreview|yandex|baiduspider|duckduckbot|applebot|petalbot|seznambot|amazonbot|gptbot|chatgpt-user|oai-searchbot|claudebot|claude-user|claude-searchbot|perplexitybot|bytespider|meta-externalagent|ccbot|facebookexternalhit|twitterbot|linkedinbot|pinterestbot|slackbot|discordbot|whatsapp|telegrambot)
handle @crawler {
reverse_proxy 127.0.0.1:4000 {
header_up X-Forwarded-Proto {scheme}
}
}

# Everyone else: PRISM first, the origin while PRISM cannot be reached.
handle {
reverse_proxy 127.0.0.1:4000 127.0.0.1:3000 {
lb_policy first
lb_try_duration 2s
fail_duration 10s
header_up X-Forwarded-Proto {scheme}
transport http {
keepalive 30s
keepalive_idle_conns 32
}
}
}
}

X-Prism-Variant for CDN Cache Keying​

PRISM returns an X-Prism-Variant response header valued bot-mobile, bot-desktop, human-mobile or human-desktop. Use it as a CDN cache-key component — not as a Vary entry, which cannot work because Vary names request headers and nothing sends this one. To key the edge cheaply, inject your own normalised request header and list it in [cache] vary; see CDN Integration.

In nginx, pass it through:

Do not add X-Prism-Variant to Vary here — Vary names request headers, no client sends this one, and a cache that keys on it collapses every variant into a single object. The CDN Integration page opens with exactly this warning; the working pattern is a normalised request header your edge injects, listed in PRISM's [cache] vary.

Trusted Proxies​

When PRISM sits behind a reverse proxy, configure trusted_proxies so PRISM only accepts X-Forwarded-Proto from your proxy IPs:

[server]
trusted_proxies = ["127.0.0.1", "10.0.0.0/8", "172.16.0.0/12"]

The default is loopback (127.0.0.0/8 and ::1), so a proxy on the same host needs no configuration. A proxy on another address must be listed, and an explicit list replaces the default rather than extending it.

An explicit empty list, trusted_proxies = [], trusts no proxy. PRISM then ignores X-Forwarded-For and X-Forwarded-Proto entirely and treats whatever opened the socket as the client.

Behind a proxy that is every visitor collapsed onto one address: your proxy's. The per-IP render limit (rate_limit_per_ip, three by default) then applies to that one address on behalf of all your traffic, and renders stop after the third visitor. If PRISM receives X-Forwarded-For from a private peer it does not trust, it logs a WARN (at most every 10 minutes) and counts prism_untrusted_forwarded_requests_total; see trusted_proxies.