Commit 20cf2de
Add nginx load balancing support for multi-instance Streamlit deployments (#336)
* Add nginx load balancer for scaling Streamlit in a single container
When STREAMLIT_SERVER_COUNT > 1, the entrypoint dynamically generates an
nginx config and launches multiple Streamlit instances on internal ports
(8510+), with nginx on port 8501 using ip_hash sticky sessions for
WebSocket compatibility. Default (STREAMLIT_SERVER_COUNT=1) preserves
existing behavior with no nginx overhead.
https://claude.ai/code/session_018VEL5xKZfe4LCcUa8iUHJ9
* Fix nginx config: create /etc/nginx directory before writing config
https://claude.ai/code/session_018VEL5xKZfe4LCcUa8iUHJ9
* Fix nginx: use absolute path /usr/sbin/nginx
The mamba environment activation shadows system binaries on the PATH.
https://claude.ai/code/session_018VEL5xKZfe4LCcUa8iUHJ9
* Switch nginx from ip_hash to least_conn for load balancing
ip_hash pins all users behind the same NAT/VPN/reverse-proxy to a
single backend, defeating the load balancer. least_conn distributes
new connections to the instance with fewest active connections, and
once a WebSocket is established it stays on that backend for the
session lifetime, so sticky sessions are not needed.
https://claude.ai/code/session_018VEL5xKZfe4LCcUa8iUHJ9
* Fix file uploads: disable nginx client_max_body_size limit
nginx defaults to 1MB max body size, which blocks Streamlit file
uploads with a 400 error. Set to 0 (unlimited) to let Streamlit
enforce its own 200MB limit from config.toml.
https://claude.ai/code/session_018VEL5xKZfe4LCcUa8iUHJ9
* Fix file uploads: switch to hash-based sticky sessions
least_conn routes each HTTP request independently, so the file upload
POST (/_stcore/upload_file) can land on a different backend than the
WebSocket session, causing a 400 error.
Use hash $remote_addr$http_x_forwarded_for consistent instead:
- Provides session affinity so uploads hit the correct backend
- Behind a reverse proxy: XFF header differentiates real client IPs
- Direct connections: falls back to remote_addr (like ip_hash)
- "consistent" minimizes redistribution when backends are added/removed
https://claude.ai/code/session_018VEL5xKZfe4LCcUa8iUHJ9
* Implement cookie-based sticky sessions for nginx load balancer
Replace ip_hash/hash-on-IP with cookie-based session affinity using
nginx's built-in map and $request_id:
- map $cookie_stroute $route_key: if browser has a "stroute" cookie,
reuse its value; otherwise fall back to $request_id (a unique random
hex string nginx generates per-request)
- hash $route_key consistent: route based on the cookie/random value
- add_header Set-Cookie on every response to persist the routing key
This ensures each browser gets its own sticky backend regardless of
source IP, fixing both:
- File uploads (POST must hit the same backend as the WebSocket session)
- Load distribution when all users share the same IP (NAT/VPN/proxy)
No new packages required - uses only built-in nginx directives.
https://claude.ai/code/session_018VEL5xKZfe4LCcUa8iUHJ9
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent e8af5fe commit 20cf2de
2 files changed
+55
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
89 | 135 | | |
90 | 136 | | |
91 | 137 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
0 commit comments