sq66 commited on
Commit
7fa1e63
·
verified ·
1 Parent(s): ee87060

Update nginx.conf

Browse files
Files changed (1) hide show
  1. nginx.conf +85 -58
nginx.conf CHANGED
@@ -1,62 +1,89 @@
1
- # Nginx 的主配置块
2
- server {
3
- # 监听 8080 端口 (这是容器内部的端口,Hugging Face 会把它映射出去)
4
- listen 8080;
5
- # 服务器名,这里用 _ 表示接受任意域名,因为 Hugging Face 会分配域名
6
- server_name _;
7
-
8
- # 处理所有以 /ai/ 开头的请求
9
- location /ai/ {
10
- # 关键步骤:重写 URL
11
- # 这行代码的意思是:
12
- # ^/ai/(.*)$ : 匹配以 /ai/ 开头的路径,并将 /ai/ 后面的所有内容捕获到变量 $1 中
13
- # /$1 : 将请求路径替换为捕获到的内容 $1 (即去掉了 /ai/ 前缀)
14
- # break : 停止处理后续的 rewrite 规则,并使用当前的 URI 进行后续处理
15
- rewrite ^/ai/(.*)$ /$1 break;
16
-
17
- # 将重写后的请求转发给你的目标服务器
18
- # 注意:这里使用 https://cc.cwapi.me
19
- proxy_pass https://cc.cwapi.me;
20
-
21
- # --- 以下是一些推荐的设置,帮助目标服务器正确识别请求 ---
22
- # 将原始请求的 Host 头部传递给后端服务器
23
- proxy_set_header Host $host;
24
- # 将客户端的真实 IP 地址传递给后端服务器
25
- proxy_set_header X-Real-IP $remote_addr;
26
- # 传递客户端请求经过的代理服务器列表
27
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
28
- # 告诉后端服务器原始请求是 http 还是 https
29
- proxy_set_header X-Forwarded-Proto $scheme;
30
-
31
- # 增加缓冲区大小,以防 API 响应过大导致问题 (可选,但有时有用)
32
- proxy_buffers 16 16k;
33
- proxy_buffer_size 32k;
34
- }
 
 
 
 
 
 
 
 
35
 
36
- # 处理根路径 / 的请求 (可选,可以给一个提示,避免直接访问时 Nginx 报错)
37
- location / {
38
- # 返回一个简单的 404 Not Found 或提示信息
39
- return 404 'Not Found: Please access via /ai/ path\n';
40
- # 或者可以返回一个简单的 HTML 页面
41
- # add_header Content-Type text/html;
42
- # return 200 '<html><body><h1>Proxy Ready</h1><p>Access your API via /ai/</p></body></html>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  }
44
 
45
- # (可选) 禁用 Nginx 错误日志输出到控制台,如果你不希望看到 Nginx 自身的错误信息
46
- # error_log /dev/null crit;
47
  }
48
-
49
- # 你可以保留你的 http, events, user 配置,但对于这个简单的反代,
50
- # 上面的 server 块通常放在 http 块内部。
51
- # 如果只用上面的 server 块,Nginx 会使用默认的 user 和 events 设置。
52
- # 一个更完整的结构可能是:
53
- # events { worker_connections 1024; }
54
- # http {
55
- # # 把上面的 server { ... } 块放在这里
56
- # server {
57
- # ...
58
- # }
59
- # # error_log /dev/null crit; # 可以放在 http 块里
60
- # }
61
- # 但对于这个 Dockerfile 里的 nginx.conf,只需要上面的 server { ... } 块内容通常就够了。
62
- # Nginx 镜像的默认配置会包含必要的 http 和 events 块。
 
1
+ # 定义 Nginx 全局配置
2
+ # user nginx; # 通常不需要在非 root 容器中指定,可以注释掉或删除
3
+ # worker_processes auto; # 可以根据需要调整
4
+
5
+ # events 块定义连接处理模型
6
+ events {
7
+ worker_connections 1024; # 每个工作进程允许的最大连接数
8
+ }
9
+
10
+ # http 块定义 HTTP 服务器相关配置
11
+ http {
12
+ # --- 新增:指定临时文件路径到 /tmp ---
13
+ # Nginx 存放客户端请求体的临时文件路径
14
+ client_body_temp_path /tmp/client_body_temp;
15
+ # Nginx 存放从后端服务器接收到的数据的临时文件路径 (用于 proxy_pass)
16
+ proxy_temp_path /tmp/proxy_temp;
17
+ # (下面几个通常用不到,但也加上以防万一)
18
+ fastcgi_temp_path /tmp/fastcgi_temp;
19
+ uwsgi_temp_path /tmp/uwsgi_temp;
20
+ scgi_temp_path /tmp/scgi_temp;
21
+ # --- 新增结束 ---
22
+
23
+ # 包含 MIME 类型定义
24
+ include /etc/nginx/mime.types;
25
+ # 默认文件类型
26
+ default_type application/octet-stream;
27
+
28
+ # 日志格式 (可选)
29
+ # log_format main '$remote_addr - $remote_user [$time_local] "$request" '
30
+ # '$status $body_bytes_sent "$http_referer" '
31
+ # '"$http_user_agent" "$http_x_forwarded_for"';
32
+ # access_log /var/log/nginx/access.log main;
33
+
34
+ # 启用 sendfile 优化文件传输
35
+ sendfile on;
36
+ # tcp_nopush on;
37
+
38
+ # 连接保持超时 (可选)
39
+ # keepalive_timeout 65;
40
+
41
+ # 启用 Gzip 压缩 (可选)
42
+ # gzip on;
43
 
44
+ # 定义我们的反向代理服务器
45
+ server {
46
+ # 监听 8080 端口
47
+ listen 8080;
48
+ # 服务器名,接受所有指向这个 Space 的域名
49
+ server_name _;
50
+
51
+ # 处理所有以 /ai/ 开头的请求
52
+ location /ai/ {
53
+ # 重写 URL,去掉 /ai/ 前缀
54
+ rewrite ^/ai/(.*)$ /$1 break;
55
+
56
+ # 转发请求到目标服务器
57
+ proxy_pass https://cc.cwapi.me;
58
+
59
+ # --- 保持这些重要的头部设置 ---
60
+ proxy_set_header Host $host; # 注意这里可能需要改成 proxy_set_header Host cc.cwapi.me; 如果你的后端服务需要正确的 Host 头
61
+ proxy_set_header X-Real-IP $remote_addr;
62
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
63
+ proxy_set_header X-Forwarded-Proto $scheme;
64
+
65
+ # --- 缓冲区设置 (现在应该可以工作了) ---
66
+ proxy_buffers 16 16k;
67
+ proxy_buffer_size 32k;
68
+
69
+ # --- 增加超时设置 (可选,但对 API 调用有好处) ---
70
+ proxy_connect_timeout 60s; # 连接后端超时时间
71
+ proxy_send_timeout 60s; # 发送请求到后端超时时间
72
+ proxy_read_timeout 120s; # 读取后端响应超时时间 (API 可能需要较长时间响应)
73
+ }
74
+
75
+ # 处理根路径 / 的请求
76
+ location / {
77
+ add_header Content-Type text/plain;
78
+ return 404 'Not Found: Please access via /ai/ path\n';
79
+ }
80
+
81
+ # (可选) 将 Nginx 自身的错误日志定向到标准错误输出,方便在 HF Logs 查看
82
+ error_log /dev/stderr warn;
83
+ # (可选) 将 Nginx 的访问日志定向到标准输出
84
+ access_log /dev/stdout; # 可以用上面的 log_format main;
85
  }
86
 
87
+ # (可选) 你可以禁用错误日志到文件,如果你不希望它们写入容器文件系统
88
+ # error_log /dev/null crit; # 禁用错误日志文件
89
  }