Skip to content
← All writing
2026-04-14 · 14 min read nginxvercelnextjsdevops

Reverse-Proxying Vercel: Five Pitfalls, Fully Explained

Vercel is slow from mainland China? Put an Nginx reverse proxy in Hong Kong in front of it. This guide breaks down the five traps along the way -- 522 handshake timeouts, Server Actions CSRF errors, broken static caching, intermittent Code 99 challenges, __resolve-check 404s -- and the mechanics behind each one.

What you'll get from this article

If you've hit any of the following, this article will get you unstuck for good:

The goal isn't just to hand you the fix -- it's to make sure you understand why it works, so next time you can diagnose a variant of the problem on your own.


Target architecture

Here's the chain we're building:

Goal: route mainland users to Vercel via a Hong Kong server

🇨🇳 Mainland user
app.example.com
→
🇭🇰 Hong Kong ECS
Nginx reverse proxy
→
🌐 Vercel
my-app.vercel.app

Mainland -> Hong Kong (30ms) -> Vercel (20ms) ~= 50ms total latency

Why not connect to Vercel directly?

Vercel's servers sit in the US. Direct connections from mainland China run 200ms+ of latency, and CDN origin fetches that cross the GFW frequently fail the SSL handshake (error 522). Hong Kong to the US, on the other hand, is stable at 20-30ms, and mainland-to-Hong-Kong is fast too. Add one hop in the middle and total latency stays in the 50-80ms range.


Pitfall #1: 522 SSL handshake timeouts

Symptom

After setting up the Nginx reverse proxy, the homepage loads -- but every static asset (CSS/JS) returns 522:

Diagnostic command
$curl -I "https://app.example.com/_next/static/chunks/main.js" | grep -E "HTTP/|522|via"
HTTP/1.1 522
via: ens-cache14.l2cn3167[...], 522-1281
X-Site-Cache-Status: BYPASS

Diagnosis

Look at the node name in the via header: l2cn = a mainland China node. 522-1281 = SSL handshake timeout.

Root cause: the ESA/CDN origin-fetch node is in mainland China. A mainland node connecting directly to Vercel has to cross the GFW, and the HTTPS handshake times out constantly. This isn't a configuration problem -- it's a network topology problem.

Fix

Relay through a Hong Kong ECS instance so the origin fetch happens from Hong Kong:

Verify Hong Kong-to-Vercel connectivity
$curl -I https://my-app.vercel.app/ | grep x-vercel-id
x-vercel-id: hkg1::iad1::xxx <- Hong Kong edge node, looking good!

Pitfall #2: the baseline Nginx config

The lines that matter most

location / {
    proxy_pass https://my-app.vercel.app;
    proxy_set_header Host app.example.com;        # <- Critical! NOT the vercel domain
    proxy_ssl_server_name on;                     # <- Must be on! SNI
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
}

Key Concept

What does proxy_ssl_server_name on do?

Vercel hosts millions of projects behind a handful of IPs. When Nginx connects upstream, it has to use SNI (Server Name Indication) to tell Vercel which domain's certificate it wants. Without it, Vercel has no idea which certificate to present during the TLS handshake, and the connection fails (522).

The hidden trap in aaPanel/BT Panel

The default Nginx config generated by BT Panel (Baota) includes this "security rule", which 404s every /_next/ path:

❌ BT Panel default (broken)
✅ Fixed (remove .next)
location ~* /(.git|.ssh|.next|.nuxt|node_modules)/ {
  return 404;
}
location ~* /(.git|.ssh|node_modules|runtime)/ {
  return 404;
  # Removed .next and .nuxt
  # Next.js serves everything under /_next/ -- never block it!
}
🚨

You must remove \.next

This regex matches /_next/static/, /_next/image/, and every other Next.js static asset path -- so all of your CSS, JS, and images come back as 404s.


Pitfall #3: Server Actions CSRF errors

Symptom

The site loads fine, but logging in or submitting a form throws:

x-forwarded-host header does not match origin header.
Error: Invalid Server Actions request.

This error only shows up for Server Actions -- plain GET requests work fine.

Why?

Click the button below to compare the full request chain before and after the fix:

Request chain comparison (click to toggle)

🌐 Browser
POST /login (Server Action)
Origin: https://app.example.com
Host: app.example.com

↓

🖥️ Hong Kong Nginx
proxy_set_header Host my-app.vercel.app
proxy_set_header X-Forwarded-Host app.example.com

↓

⚡ Vercel Edge
Routes to the project ✅
Rewrites X-Forwarded-Host → my-app.vercel.app ⚠️

↓

⚛️ Next.js Server Actions
Origin = app.example.com
X-Forwarded-Host = my-app.vercel.app
❌ Mismatch!
💥 Invalid Server Actions request. Aborting.

Key Concept

Why does Vercel rewrite X-Forwarded-Host?

Vercel is a multi-tenant platform that routes requests to projects by Host header. When a request comes in, it writes the domain it actually routed to into X-Forwarded-Host before handing off to Next.js. If the Host you send is a domain Vercel doesn't recognize, it overwrites the header with its own vercel.app domain. There is no way to disable this behavior.

Key Concept

Why does Next.js validate Origin at all?

Server Actions don't have dedicated URL paths, so a traditional CSRF token won't protect them. Instead, Next.js relies on an Origin vs. X-Forwarded-Host same-origin check as its only CSRF defense. If they don't match, it refuses to execute -- that's deliberate security design, not a bug.

Fix: two steps, both required

Step 1: in Vercel -> Settings -> Domains, add app.example.com. It will show "Invalid Configuration" -- that's fine. All that matters is that Vercel recognizes the domain.

Step 2: change the Host header in Nginx to your custom domain:

❌ Before (triggers the error)
✅ After (correct)
proxy_set_header Host my-app.vercel.app;
# Vercel rewrites X-Forwarded-Host to vercel.app
# Origin != X-Forwarded-Host -> error
proxy_set_header Host app.example.com;
# Vercel recognizes this domain, no rewrite
# Origin = X-Forwarded-Host = app.example.com -> passes

Pitfall #4: Code 99 browser verification (Vercel System Mitigation)

Symptom

Everything deploys cleanly, but intermittently users hit this page. A few refreshes and it goes away -- it's hard to reproduce reliably:

Failed to verify your browser
Code 99

Open DevTools and look at the response headers, and the source becomes obvious:

The decisive response headers

HeaderMeaningExample
StatusHTTP status code403 Forbidden
X-Vercel-MitigatedMarker that Vercel issued the challengechallenge
X-Vercel-Challenge-TokenJS challenge token2.1776496838.60...
X-Vercel-IdThe Vercel edge node that handled ithkg1::...

The page also loads challenge.v2.min.js -- Vercel's JS challenge script.

⚠️

It's not BT Panel's WAF, and it's not Alibaba Cloud Shield

The "Failed to verify your browser, Code 99" wording looks a lot like a browser challenge from EdgeOne or BT Panel's free firewall, and it's easy to misattribute. The conclusive evidence is X-Vercel-Mitigated: challenge in the response headers -- if it's there, Vercel issued the challenge, regardless of what's running on your HK server.

Why a single-IP reverse proxy is especially prone to this

Vercel runs an always-on baseline DDoS / WAF layer that never appears in the Firewall dashboard, and it flags "many requests from one IP in a short window" as an attack pattern.

Key Concept

Why doesn't direct .vercel.app traffic trigger it, but proxied traffic does?

With direct connections, every user arrives with their own real IP, so traffic is naturally spread out. Behind a reverse proxy, every request Vercel sees comes from your Hong Kong ECS egress IP. From the edge's point of view, that's "one IP firing 30 requests per second" -- a textbook L7 DDoS signature -- so it issues a challenge.

Diagnosis: the Vercel Firewall gives you hard evidence

Vercel Dashboard -> your project -> Firewall -> set the time range to Past Day:

Fix: you need a System Bypass, not a Custom WAF Rule

Here's the easiest mistake to make: Vercel's Firewall has two distinct "allow" mechanisms, and they are not interchangeable.

Custom WAF Rule (Bypass) vs System Bypass Rule

HeaderMeaningExample
Custom WAF Rule (Action: Bypass)Only skips your own rules + managed rulesets❌ Does NOT skip baseline DDoS
System Bypass RuleSkips Vercel's platform-level system mitigations✅ Required for Code 99
🚨

Vercel's own warning when saving a Custom WAF Bypass

"Please note that custom WAF rules with the bypass action will not bypass system mitigations. Please create a system bypass rule if you wish to bypass the system mitigations."

Setting up the System Bypass

Step 1: get your HK server's egress IP
# The IP shown in your panel isn't necessarily the egress IP -- run this on the server itself
$curl ifconfig.me
47.239.236.210

Step 2: Vercel Dashboard -> your project -> Firewall -> Configure -> find the DDoS mitigation rules section -> click Configure New System Bypass

Step 3: fill in:

Step 4: click Create System Bypass -- it propagates globally within 5 minutes.

Verification

Back on the Firewall Traffic page (Past Day), the Challenged count should stop growing immediately. Hit the domain a few times in a fresh incognito window -- Code 99 should be gone for good.

⚡ Iron Rule #3

Single-IP reverse proxy to Vercel: a System Bypass is mandatory

A Bypass action on a custom WAF rule does not skip Vercel's platform-level baseline DDoS. A reverse proxy funnels all user traffic through a single IP, which is guaranteed to keep tripping baseline DDoS challenges. The System Bypass is the only fix: allowlist your proxy server's egress IP. On the Pro plan, System Bypass rules are per-project, so multi-project setups need one rule each (account-level IP allowlisting is Enterprise-only).


Pitfall #5: __resolve-check 404s in the error log

Symptom

The reverse proxy's error log keeps filling up with:

[error] open() "/.well-known/acme-challenge/__resolve-check"
failed (2: No such file or directory)
client: 54.x.x.x, server: app.example.com

The source IPs cluster in AWS US-West ranges (54.x.x.x, 3.x.x.x, 18.x.x.x).

Why

/.well-known/acme-challenge/__resolve-check is Vercel's own domain-ownership probe -- it periodically sends requests to confirm the domain still points at Vercel.

But this block in your nginx config:

location /.well-known {
    allow all;
}

wins the location match even over location / (the proxy_pass block) because the prefix is longer. So Vercel's probe gets intercepted, nginx falls back to looking for a file on disk, and -- 404.

What happens if you don't fix it

It won't trigger Code 99, but Vercel keeps recording "domain verification failed" over time, which may lower the platform's tolerance for that domain. Worth fixing while you're in there.

The fix: add a more specific location

❌ Before
✅ After
location /.well-known {
  allow all;
}
# Pass Vercel's probe straight through
location = /.well-known/acme-challenge/__resolve-check {
  proxy_pass https://my-app.vercel.app;
  proxy_set_header Host app.example.com;
  proxy_ssl_server_name on;
}

# Keep the original block for Let's Encrypt validation
location /.well-known {
  allow all;
}

location = is an exact match with the highest priority, so it handles only Vercel's probe path and leaves every other .well-known use case untouched.


The X-Forwarded-* family

Any time a reverse proxy is in the path, this family of headers comes into play:

Common X-Forwarded-* headers

HeaderMeaningExample
X-Forwarded-ForClient's real IP (leftmost = most original)114.114.114.114, 10.0.0.1
X-Forwarded-HostDomain the user originally requested (the heart of reverse proxying)app.example.com
X-Forwarded-ProtoProtocol the user originally requestedhttps
X-Real-IPClient's real IP (single value, Nginx extension)114.114.114.114

Static asset caching

Without caching, every static file is fetched from Vercel via Hong Kong on every request. With caching, the second hit comes back in < 10ms:

# Must come BEFORE location / !
location ^~ /_next/static/ {
    proxy_pass https://my-app.vercel.app;
    proxy_set_header Host app.example.com;
    proxy_ssl_server_name on;
    proxy_http_version 1.1;

    proxy_cache my_cache;
    proxy_cache_valid 200 365d;    # Cache forever (hashed filenames make this safe)
    proxy_cache_use_stale error timeout updating;

    add_header Cache-Control "public, max-age=31536000, immutable";
    add_header X-Cache-Status $upstream_cache_status;  # Debugging: HIT/MISS
}
⚠️

The location priority trap

location ^~ (prefix match) takes priority over location ~* (regex match). The static asset rule must use ^~ and sit before the generic location / block -- otherwise the generic rule matches first and your caching never kicks in.

Verify the cache is working
$curl -I https://app.example.com/_next/static/chunks/main.js | grep X-Cache-Status
X-Cache-Status: MISS <- first request: fetched from origin and cached
$curl -I https://app.example.com/_next/static/chunks/main.js | grep X-Cache-Status
X-Cache-Status: HIT <- cache hit!

Two iron rules

⚡ Iron Rule #1

Reverse-proxying to Vercel: Host must be a registered domain

The Host header must be a domain that's been added to your Vercel project. Otherwise Vercel rewrites X-Forwarded-Host, and Server Actions, NextAuth, and middleware all break in confusing ways.

⚡ Iron Rule #2

Proxying to multi-tenant platforms: always enable proxy_ssl_server_name on

For platforms that host many domains -- Vercel, Cloudflare Workers, and the like -- Nginx must send SNI on the upstream connection or the TLS handshake fails (522). It's off by default in Nginx, which is why it gets missed so often.


Diagnostic checklist

Work through these in order when something breaks
# 1. Confirm DNS resolves to the Hong Kong ECS (turn off your VPN first)
$dig +short app.example.com @8.8.8.8
47.239.236.210 <- should be your Hong Kong server's IP
# 2. Confirm the Hong Kong ECS can reach Vercel
$curl -I https://my-app.vercel.app/
x-vercel-id: hkg1::... <- Hong Kong edge node
# 3. Confirm the domain routes to your Vercel project
$curl -I https://app.example.com/ | grep x-vercel-id
x-vercel-id: hkg1::... <- if this is present, routing is correct
# 4. Confirm static assets hit the cache
$curl -I https://app.example.com/_next/static/chunks/main.js | grep X-Cache-Status
X-Cache-Status: HIT
# 5. Seeing Code 99? Check the response headers for Vercel's signature
$curl -I https://app.example.com/ | grep -iE 'x-vercel-(mitigated|challenge)'
x-vercel-mitigated: challenge <- if present, go set up a System Bypass
# 6. Get the HK server's egress IP (run it ON the server, not your SSH client)
$curl ifconfig.me
47.239.236.210 <- this goes into the Vercel System Bypass
💡

dig returns 198.18.x.x?

That's a fake IP from Clash/your VPN -- local DNS is being hijacked by the proxy. Turn off the proxy and test again, or SSH into the server and run the command there for the most reliable result.