What you'll get from this article
If you've hit any of the following, this article will get you unstuck for good:
- A Next.js app on Vercel that's painfully slow from mainland China
- Static assets timing out with 522 after you set up a reverse proxy
- Login failing with
Invalid Server Actions request - Intermittent "Failed to verify your browser, Code 99" pages that disappear on refresh
/.well-known/acme-challenge/__resolve-check404s piling up in your error logs- You've tweaked the config a dozen times and still can't tell what's wrong
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 -> 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:
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:
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:
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)
↓
↓
↓
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:
proxy_set_header Host my-app.vercel.app;
# Vercel rewrites X-Forwarded-Host to vercel.app
# Origin != X-Forwarded-Host -> errorproxy_set_header Host app.example.com;
# Vercel recognizes this domain, no rewrite
# Origin = X-Forwarded-Host = app.example.com -> passesPitfall #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
| Header | Meaning | Example |
|---|---|---|
Status | HTTP status code | 403 Forbidden |
X-Vercel-Mitigated | Marker that Vercel issued the challenge | challenge |
X-Vercel-Challenge-Token | JS challenge token | 2.1776496838.60... |
X-Vercel-Id | The Vercel edge node that handled it | hkg1::... |
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:
- Compare
ChallengedvsAllowed-- if Challenged is even outpacing Allowed, this is almost certainly your problem - Check the Top IPs card: is the #1 entry your HK server's egress IP, accounting for 95%+ of traffic?
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
| Header | Meaning | Example |
|---|---|---|
Custom WAF Rule (Action: Bypass) | Only skips your own rules + managed rulesets | ❌ Does NOT skip baseline DDoS |
System Bypass Rule | Skips 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 2: Vercel Dashboard -> your project -> Firewall -> Configure -> find the DDoS mitigation rules section -> click Configure New System Bypass
Step 3: fill in:
- IP Address or CIDR: the egress IP from the previous step
- Domain: the affected custom domain (e.g.
app.example.com) - Note: describe the purpose, e.g.
HK reverse proxy (Aliyun ECS)
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
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
| Header | Meaning | Example |
|---|---|---|
X-Forwarded-For | Client's real IP (leftmost = most original) | 114.114.114.114, 10.0.0.1 |
X-Forwarded-Host | Domain the user originally requested (the heart of reverse proxying) | app.example.com |
X-Forwarded-Proto | Protocol the user originally requested | https |
X-Real-IP | Client'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.
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
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.