Skip to content

Commit feaa7e4

Browse files
authored
Merge pull request #67 from yokowu/fix-proxy
fix(proxy): 优化流式输出
2 parents 31798c2 + 69285fd commit feaa7e4

2 files changed

Lines changed: 58 additions & 45 deletions

File tree

backend/internal/proxy/proxy.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ func (p *LLMProxy) handleChatCompletionStream(ctx context.Context, w http.Respon
703703
w.Header().Set("Cache-Control", "no-cache")
704704
w.Header().Set("Connection", "keep-alive")
705705
w.Header().Set("Transfer-Encoding", "chunked")
706+
w.Header().Set("X-Accel-Buffering", "no")
706707

707708
rc := &domain.RecordParam{
708709
RequestID: c.RequestID,
@@ -714,8 +715,6 @@ func (p *LLMProxy) handleChatCompletionStream(ctx context.Context, w http.Respon
714715
Prompt: prompt,
715716
Role: consts.ChatRoleAssistant,
716717
}
717-
buf := bufio.NewWriterSize(w, 32*1024)
718-
defer buf.Flush()
719718

720719
ch := make(chan []byte, 1024)
721720
defer close(ch)
@@ -769,10 +768,13 @@ func (p *LLMProxy) handleChatCompletionStream(ctx context.Context, w http.Respon
769768

770769
err = streamRead(ctx, resp.Body, func(line []byte) error {
771770
ch <- line
772-
if _, err := buf.Write(line); err != nil {
771+
if _, err := w.Write(line); err != nil {
773772
return fmt.Errorf("写入响应失败: %w", err)
774773
}
775-
return buf.Flush()
774+
if f, ok := w.(http.Flusher); ok {
775+
f.Flush()
776+
}
777+
return nil
776778
})
777779
return err
778780
})

ui/nginx.conf

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,65 @@
11
worker_processes auto;
22
user nginx nginx;
3+
34
events {
4-
worker_connections 4096;
5+
worker_connections 4096;
56
}
67

78
http {
8-
include /etc/nginx/mime.types;
9-
default_type application/octet-stream;
10-
11-
log_format main '$remote_addr - $remote_user [$time_local] $status '
12-
'"$request" $body_bytes_sent "$http_referer" '
13-
'"$http_user_agent" "$http_x_forwarded_for" "$host"';
14-
15-
error_log /var/log/nginx/error.log error;
16-
access_log /var/log/nginx/access.log main;
17-
18-
sendfile on;
19-
keepalive_timeout 65;
20-
client_body_buffer_size 128k;
21-
client_header_buffer_size 2k;
22-
23-
gzip on;
24-
gzip_types applicaiton/javascript text/css image/png image/jpeg image/gif;
25-
26-
upstream backend {
27-
server monkeycode-server:8888;
28-
}
29-
30-
server {
31-
listen 80;
32-
listen [::]:80;
33-
server_name _;
34-
35-
proxy_set_header Host $host;
36-
proxy_set_header X-Real-IP $remote_addr;
37-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
38-
proxy_set_header X-Forwarded-Proto $scheme;
39-
40-
location / {
41-
root /usr/share/nginx/html;
42-
index index.html index.htm;
43-
try_files $uri $uri/ /index.html;
9+
include /etc/nginx/mime.types;
10+
default_type application/octet-stream;
11+
12+
log_format main '$remote_addr - $remote_user [$time_local] $status "$request" $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$host"';
13+
14+
error_log /var/log/nginx/error.log error;
15+
access_log /var/log/nginx/access.log main;
16+
17+
sendfile on;
18+
keepalive_timeout 65;
19+
client_body_buffer_size 128k;
20+
client_header_buffer_size 2k;
21+
22+
gzip on;
23+
gzip_types application/javascript text/css image/png image/jpeg image/gif;
24+
25+
upstream backend {
26+
server monkeycode-server:8888;
4427
}
4528

46-
location /api {
29+
server {
30+
listen 80;
31+
listen [::]:80;
32+
server_name _;
33+
34+
proxy_set_header Host $host;
35+
proxy_set_header X-Real-IP $remote_addr;
36+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
37+
proxy_set_header X-Forwarded-Proto $scheme;
38+
39+
location / {
40+
root /usr/share/nginx/html;
41+
index index.html index.htm;
42+
try_files $uri $uri/ /index.html;
43+
}
44+
45+
location /api {
4746
proxy_pass http://backend;
48-
}
47+
}
48+
49+
location /v1 {
50+
proxy_pass http://backend;
51+
}
4952

50-
location /v1 {
53+
location /v1/chat/completions {
5154
proxy_pass http://backend;
55+
proxy_http_version 1.1;
56+
proxy_set_header Connection "";
57+
proxy_buffering off;
58+
proxy_cache off;
59+
proxy_read_timeout 24h;
60+
proxy_connect_timeout 24h;
61+
proxy_send_timeout 24h;
62+
keepalive_timeout 65;
63+
}
5264
}
53-
}
5465
}

0 commit comments

Comments
 (0)