OpenAI Reverse Proxy with Nginx: Streaming Config
An OpenAI reverse proxy with nginx is easy to get almost right. Streaming is the part that breaks: buffering, short timeouts and a small body limit all show up as hung or cut-off replies.
Last updated

Summary
- 01Turn off proxy_buffering for the API location so SSE chunks reach the client as they arrive.
- 01Raise proxy_read_timeout and client_max_body_size: nginx defaults are 60 seconds and 1 MB, the API accepts bodies up to 8 MB.
- 01Send the right Host header and enable proxy_ssl_server_name for an HTTPS upstream.
- 01Limit concurrency per client, since a full key allows 8 requests at once.
The working config
Here is a complete server block for an openai reverse proxy nginx setup that forwards /v1/ to this API and keeps streaming alive. It assumes you already terminate TLS on your own domain.
server {
listen 443 ssl;
server_name llm.yourdomain.example;
# certificates omitted
# upstream base URL: https://api.openaireverseproxy.com/v1
client_max_body_size 8m;
location /v1/ {
proxy_pass https://api.openaireverseproxy.com;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host api.openaireverseproxy.com;
proxy_ssl_server_name on;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Reload with nginx -t && nginx -s reload. The sections below explain each line, because the reasons tell you what to change when your case differs.
Why each directive is there
proxy_pass without a path
Writing proxy_pass https://api.openaireverseproxy.com; with no trailing URI passes the request path through unchanged. A request to /v1/chat/completions on your host arrives at the same path upstream. Adding a path to proxy_pass changes how nginx rewrites the URL and is a classic source of 404 errors.
Host and SNI
The upstream is HTTPS and served by name. Without proxy_ssl_server_name on nginx does not send the server name during the TLS handshake, which can end in a handshake error. Without the matching Host header the upstream may route the request wrongly.
HTTP 1.1 and Connection
The defaults talk HTTP 1.0 to the upstream and close the connection after each request. Setting version 1.1 and clearing the Connection header allows keepalive and chunked responses, which streaming uses.
Body size
The API accepts bodies up to 8 MB, and nginx rejects anything over 1 MB by default with a 413. Long chat histories with pasted documents pass 1 MB sooner than you would think, so set client_max_body_size 8m.
Streaming: the buffering problem
With stream: true the API sends Server-Sent Events: a series of data: lines, ending with a usage chunk. If nginx buffers the response it holds those lines until a buffer fills or the upstream closes. The user sees nothing for ten seconds, then the whole answer at once. That looks like a slow model but is a proxy setting.
proxy_buffering off fixes it for the whole location. nginx also honors an X-Accel-Buffering: no header when an upstream sends one, but setting the directive yourself is more reliable. Also check any layer in front of nginx, such as a CDN or load balancer, because each one can buffer again.
Test it from the shell. The -N flag turns off curl's own buffering, so you see chunks as they land:
curl -N https://api.openaireverseproxy.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"uncensored","stream":true,
"messages":[{"role":"user","content":"Count from 1 to 20 slowly."}]}'
Run it first against the API directly, then against your proxy host with the same body. Both should print lines progressively. If only the direct one does, your proxy is buffering somewhere.
Timeouts for long generations
The default proxy_read_timeout is 60 seconds between two reads. Streaming answers send data often, so they rarely trip it. A long non-streamed completion can be silent for a while, and a reply that uses a large part of the 100,000-token window takes longer. Setting 300 seconds is a safe margin.
The same idea applies to anything in front of nginx: load balancers and CDNs have their own idle timeouts, commonly shorter. If requests die at an exact round number of seconds, look there.
Headers and keys
nginx passes the Authorization: Bearer ... header through by default, so clients can keep sending their own key. That is usually what you want: the proxy stays dumb and the key never sits in your config.
If you prefer to hold the key on the server and keep it away from clients, add proxy_set_header Authorization "Bearer YOUR_KEY"; in the location. Then protect your own endpoint, because anyone who can reach it spends your balance. Put it behind basic auth, an allowlist or your app's own session check.
Response headers pass through too, so your clients still see X-Request-Id, X-Balance-USD and the rate-limit headers X-RateLimit-Limit-Requests and X-RateLimit-Limit-Concurrency. Do not strip them; they are useful in logs.
Respecting the API limits at the proxy
A full key allows 8 concurrent requests and 300 requests per minute; a trial key allows 2 and 60. Beyond that the API returns HTTP 429 with the code concurrency or rate_limited, and under temporary overload a 429 with Retry-After: 5. A streamed request holds its slot until the stream ends.
If many users share one key through your proxy, nginx can queue the excess rather than failing it. Add this in the http block and use it in the location:
limit_req_zone $binary_remote_addr zone=llm_rate:10m rate=4r/s;
limit_conn_zone $binary_remote_addr zone=llm_conn:10m;
# inside location /v1/
limit_req zone=llm_rate burst=20;
limit_conn llm_conn 2;
This caps each client address at two simultaneous connections and about four requests per second with a short burst. Tune the numbers so the sum across clients stays under the key's limits. A key-wide cap needs a shared counter, which is outside nginx's simple tools; a small queue in your application handles that better.
Logging without leaking prompts
Chat prompts can be personal. If you log traffic at the proxy, record metadata only. A format like this keeps the useful fields and drops the body:
log_format llm '$remote_addr $time_iso8601 "$request" $status '
'$request_time $upstream_response_time $body_bytes_sent';
access_log /var/log/nginx/llm.log llm;
# upstream base URL: https://api.openaireverseproxy.com/v1
Request bodies are not part of the standard access log, so nothing private is written unless you add it on purpose. The X-Request-Id response header is worth capturing with $upstream_http_x_request_id, because it lets you refer to a specific call when you need to trace a problem. Rotate the log files and keep them short-lived.
On the API side prompts are not used for training, so the only copies that exist outside the service are the ones you choose to keep.
Debugging checklist
- 413 error: raise
client_max_body_size. - 502 or SSL handshake errors: add
proxy_ssl_server_name onand check theHostheader. - 504 after about a minute: raise
proxy_read_timeout. - Reply arrives all at once:
proxy_buffering off, and check CDN settings. - 404: remove any path from
proxy_passor match it with a rewrite. - 401: the client key is missing; confirm the Authorization header reaches the upstream.
For request formats, tool calling and JSON mode read the docs; to get a key and a $0.50 trial, use the key page. The price comparison in the API pricing guide helps you decide how much traffic to route through. Rates are $0.15 per 1M input and $0.50 per 1M output tokens; see pricing.
More guides
FAQ
Do I need nginx to use this API?
No. Any OpenAI SDK works by changing the base URL and key. A proxy is only useful if you want your own domain, logging or access control in front.
Why does streaming arrive all at once through nginx?
Buffering is on. Set proxy_buffering off in the API location and check whether a CDN or load balancer in front is buffering too.
What body size should I allow?
The API accepts up to 8 MB, so set client_max_body_size 8m. The nginx default of 1 MB rejects large chat histories with a 413.
Should the proxy add my API key?
Only if clients are trusted or protected by your own auth. Otherwise let clients send their own Authorization header and keep the key out of the config.