Bottom line: limit_req on a WebSocket upgrade path counts one request per connection, so a normal rate=10r/s looks fine until a client reconnects in a loop and gets 503. Exclude the upgrade location from the limit, or key it on a header the client sets once.

Why the limit misfires

limit_req counts requests. An upgrade is one request that then lasts hours. Two problems follow: legitimate reconnects after network changes count against the burst, and a limit_req_status 503 breaks the client’s handshake retry.

Working config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
map $http_upgrade $is_upgrade {
default "";
websocket "ws";
}

limit_req_zone $binary_remote_addr zone=api:10m rate=20r/s;
limit_conn_zone $binary_remote_addr zone=conns:10m;

server {
location /ws {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $is_upgrade;
proxy_read_timeout 3600s; # > client ping interval
proxy_send_timeout 3600s;
limit_conn conns 50; # concurrency limit, not rate
}
location /api/ {
limit_req zone=api burst=40 nodelay;
limit_req_status 429;
proxy_pass http://backend;
}
}

Concurrency (limit_conn) is the right instrument for WebSockets. Rate (limit_req) belongs on the request path.

Timeouts

The read timeout must exceed the client’s ping interval by a margin. A 60 s ping with a 30 s timeout drops every connection. 3600 s is a safe default and does not leak — the upstream close ends it.

429 vs 503

Return 429 with Retry-After on the API path so clients back off correctly. 503 tells a client “server unavailable” and many retry immediately, adding load.

Testing the limit

1
seq 1 200 | xargs -P 50 -I{} curl -s -o /dev/null -w "%{http_code}\n" https://host/api/x | sort | uniq -c

Expect 200s up to burst and 429s after. Then hold 60 WebSockets open with websocat and confirm none are closed by the limiter.

The failure to watch for

A single-page app that opens its socket on every route change. That is a client bug amplified by any rate limit; limit_conn reveals it as a rising concurrent-connection count per IP.

Operating notes

Rate-limit requests, concurrency-limit sockets. limit_req on an upgrade path counts one request per hours-long connection and 503s the reconnect loop.

1
2
location /ws { limit_conn conns 50; proxy_read_timeout 3600s; }
location /api/ { limit_req zone=api burst=40 nodelay; limit_req_status 429; }

Timeouts

The read timeout must exceed the client ping interval by a wide margin. 60 s ping with 30 s timeout drops every connection. 3600 s is safe and leaks nothing.

429, not 503

503 tells clients “unavailable” and many retry immediately. 429 with Retry-After makes them back off properly.

Testing

seq 1 200 | xargs -P 50 curl -w "%{http_code}\n" — expect 200s to burst, 429s after. Then hold 60 sockets with websocat and confirm none are closed by the limiter.

FAQ

Why does limit_req return 503 on WebSocket reconnects?

limit_req counts requests, and a WebSocket upgrade is one request that then lasts hours. Legitimate reconnects after network changes count against the burst, and limit_req_status 503 breaks the client’s handshake retry loop. The fix is to exclude the upgrade location from the limit or key it on a header the client sets once.

Should I use limit_conn or limit_req for WebSockets?

Use limit_conn for WebSockets. Concurrency is the right instrument for sockets, while rate limiting belongs on the request path. In the working config, the /ws location uses limit_conn conns 50 with no limit_req, and /api/ keeps limit_req zone=api burst=40 nodelay.

What proxy_read_timeout should I set for WebSockets?

The read timeout must exceed the client’s ping interval by a margin. A 60 s ping with a 30 s timeout drops every connection. 3600 s is a safe default and does not leak, because the upstream close ends it.

Why return 429 instead of 503 on the API path?

503 tells a client “server unavailable,” and many clients retry immediately, adding load. 429 with Retry-After makes clients back off correctly. The config sets limit_req_status 429 on the /api/ location.

How do I test that rate limiting is not closing WebSockets?

First run seq 1 200 | xargs -P 50 curl -w "%{http_code}\n" against the API and expect 200s up to burst and 429s after. Then hold 60 WebSockets open with websocat and confirm none are closed by the limiter.