Commit e9313f6
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 9d584de commit e9313f6
2 files changed
Lines changed: 43 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
117 | | - | |
| 116 | + | |
| 117 | + | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
150 | 154 | | |
151 | 155 | | |
152 | 156 | | |
| |||
173 | 177 | | |
174 | 178 | | |
175 | 179 | | |
176 | | - | |
177 | | - | |
178 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
179 | 213 | | |
180 | 214 | | |
181 | 215 | | |
| |||
| 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