# Next.js — Proxy Mode > Run the REP gateway as a reverse proxy in front of a Next.js SSR server. Environment variables are injected into every HTML response without touching the Next.js build. Source: https://rep-protocol.dev/examples/nextjs-proxy/ The REP gateway runs in **proxy mode** in front of a Next.js server. It intercepts every HTML response and injects the `__rep__` payload — no changes to the Next.js build pipeline required. Source code at [`examples/nextjs-proxy/`](https://github.com/ruachtech/rep/tree/main/examples/nextjs-proxy). ## Architecture ``` Client → REP Gateway :8080 (proxy mode) ↓ injects __rep__ payload into HTML responses Next.js Server :3000 ``` The Next.js server is never exposed directly. All traffic flows through the gateway. Config lives entirely in the gateway's environment variables — the Next.js image is the same across every deployment. ## When to use this pattern - Next.js App Router or Pages Router with server-side rendering (SSR) - You want the full Next.js server (streaming, server actions, API routes) - Docker Compose, Kubernetes single-container pod, or multi-service deployments ## File structure - examples/nextjs-proxy/ - src/ - app/ - layout.tsx Includes RepScript for dev-time injection - page.tsx Server component shell - api/rep/session-key/ - route.ts Dev-only session key endpoint - components/ - EnvDisplay.tsx Client component — reads REP vars - IntegrityBanner.tsx Verifies payload was not modified in transit - Dockerfile Next.js-only image (for docker-compose) - Dockerfile.single Gateway + Next.js in one image (for Kubernetes) - docker-compose.yml Two-service stack (recommended) - docker-entrypoint.sh Startup script for Dockerfile.single - next.config.ts - .rep.yaml - .env.example ## Reading REP vars in Next.js REP runs entirely in the browser. In the App Router, use `'use client'` components: ```tsx 'use client'; import { useRep, useRepSecure } from '@rep-protocol/react'; export function EnvDisplay() { // Synchronous — available on first render, no loading state const apiUrl = useRep('API_URL', 'http://localhost:3001'); const envName = useRep('ENV_NAME', 'development'); const features = useRep('FEATURE_FLAGS', ''); // Async — decrypted via the session-key endpoint const { value: analyticsKey, loading } = useRepSecure('ANALYTICS_KEY'); return (

Environment: {envName}

API: {apiUrl}

Key: {loading ? 'loading…' : analyticsKey}

); } ``` > **Caution** > > Do NOT call `rep.get()` in server components — the `__rep__` payload exists only in the browser DOM. REP vars are always accessed from `'use client'` components. ## Integrity verification The example includes an `IntegrityBanner` client component that verifies the payload was not modified after the gateway injected it. The SDK checks a SHA-256 SRI hash embedded on the `