Skip to content

Commit 9b795ca

Browse files
committed
fix (chat): nginx buffering off
1 parent 794d945 commit 9b795ca

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/server/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ENV PYTHONPATH=/app
1313
# - curl
1414
RUN apt-get update && apt-get install -y --no-install-recommends \
1515
supervisor \
16+
nginx \
1617
curl \
1718
dos2unix \
1819
unzip \
@@ -21,6 +22,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2122
# Create necessary directories
2223
RUN mkdir -p /etc/supervisor/conf.d /var/log/supervisor
2324

25+
# Remove the default Nginx site configuration.
26+
RUN rm /etc/nginx/sites-enabled/default
27+
2428
# Set the working directory
2529
WORKDIR /app
2630

@@ -32,6 +36,9 @@ RUN pip install --no-cache-dir --upgrade pip && \
3236
# Copy the supervisor configuration file from the build context (src/server)
3337
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
3438

39+
# Copy our custom Nginx configuration
40+
COPY nginx.conf /etc/nginx/sites-available/sentient
41+
3542
# Copy all the server-side Python code from the build context (src/server) into the container
3643
COPY . .
3744
RUN if [ -f /app/.env ]; then dos2unix /app/.env; fi
@@ -40,6 +47,9 @@ RUN if [ -f /app/.env ]; then dos2unix /app/.env; fi
4047
COPY start.sh .
4148
RUN dos2unix ./start.sh && chmod +x ./start.sh
4249

50+
# Link our Nginx config to make it active.
51+
RUN ln -s /etc/nginx/sites-available/sentient /etc/nginx/sites-enabled/sentient
52+
4353
# Expose the public port for the main FastAPI server
4454
EXPOSE 5000
4555

src/server/nginx.conf

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This is a new file: src/server/nginx.conf
2+
3+
upstream backend_app {
4+
# This points to your FastAPI/Uvicorn server running on localhost inside the container.
5+
server 127.0.0.1:5000;
6+
}
7+
8+
server {
9+
# Nginx will listen on port 80 inside the container.
10+
# GCP's Load Balancer will handle HTTPS termination and forward traffic to this port.
11+
listen 80;
12+
server_name _; # Catch all domains forwarded by GCP
13+
14+
# Location block for your chat stream
15+
location /api/chat/message {
16+
proxy_pass http://backend_app;
17+
18+
# Streaming-Specific Settings
19+
proxy_set_header Host $host;
20+
proxy_set_header X-Real-IP $remote_addr;
21+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
22+
proxy_set_header X-Forwarded-Proto $scheme;
23+
24+
# Critical: Disable buffering for this endpoint
25+
proxy_buffering off;
26+
27+
# Increase timeouts for long-lived stream
28+
proxy_read_timeout 86400s;
29+
proxy_send_timeout 86400s;
30+
31+
# Required for keep-alive connections
32+
proxy_http_version 1.1;
33+
proxy_set_header Connection '';
34+
35+
# Add CORS headers if needed, though your app likely handles this.
36+
add_header 'Access-Control-Allow-Origin' '*' always;
37+
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
38+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
39+
if ($request_method = 'OPTIONS') {
40+
add_header 'Access-Control-Max-Age' 1728000;
41+
add_header 'Content-Type' 'text/plain; charset=utf-8';
42+
add_header 'Content-Length' 0;
43+
return 204;
44+
}
45+
}
46+
47+
# General location block for all other API requests
48+
location / {
49+
proxy_pass http://backend_app;
50+
proxy_set_header Host $host;
51+
proxy_set_header X-Real-IP $remote_addr;
52+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
53+
proxy_set_header X-Forwarded-Proto $scheme;
54+
}
55+
}

src/server/supervisord.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
nodaemon=true
55
user=root
66

7+
[program:nginx]
8+
command=/usr/sbin/nginx -g "daemon off;"
9+
autostart=true
10+
autorestart=true
11+
priority=10
12+
stdout_logfile=/dev/stdout
13+
stdout_logfile_maxbytes=0
14+
stderr_logfile=/dev/stderr
15+
stderr_logfile_maxbytes=0
16+
717
[program:main-server]
818
command=uvicorn main.app:app --host 0.0.0.0 --port 5000 --lifespan on
919
directory=/app

0 commit comments

Comments
 (0)