File size: 3,870 Bytes
37ce4b7
 
 
 
 
 
 
7fa1e63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3923cf3
7fa1e63
 
 
 
 
 
 
 
 
 
 
5ddf1c4
 
 
 
7fa1e63
 
 
 
 
 
 
e7ca22d
58c8e0c
 
7fa1e63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24b49a4
3923cf3
7fa1e63
 
3923cf3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 定义 Nginx 运行的用户和用户组 (如果需要,通常基础镜像会处理)
# user  nginx; 
# worker_processes auto; # 让 Nginx 自动决定工作进程数
# 指定 PID 文件路径到 /tmp 目录下,那里通常有写入权限
pid /tmp/nginx.pid; 
# 错误日志路径 (可以保持默认或按需修改)
# error_log /var/log/nginx/error.log warn;

# events 块定义连接处理模型
events {
    worker_connections 1024; # 每个工作进程允许的最大连接数
}

# http 块定义 HTTP 服务器相关配置
http {
    # --- 新增:指定临时文件路径到 /tmp ---
    # Nginx 存放客户端请求体的临时文件路径
    client_body_temp_path /tmp/client_body_temp;
    # Nginx 存放从后端服务器接收到的数据的临时文件路径 (用于 proxy_pass)
    proxy_temp_path /tmp/proxy_temp;
    # (下面几个通常用不到,但也加上以防万一)
    fastcgi_temp_path /tmp/fastcgi_temp;
    uwsgi_temp_path /tmp/uwsgi_temp;
    scgi_temp_path /tmp/scgi_temp;
    # --- 新增结束 ---

    # 包含 MIME 类型定义
    include /etc/nginx/mime.types;
    # 默认文件类型
    default_type application/octet-stream;

    # 日志格式 (可选)
    # log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    #                 '$status $body_bytes_sent "$http_referer" '
    #                 '"$http_user_agent" "$http_x_forwarded_for"';
    # access_log /var/log/nginx/access.log main;

    # 启用 sendfile 优化文件传输
    sendfile on;
    # tcp_nopush on;

    # 连接保持超时 (可选)
    # keepalive_timeout 65;

    # 启用 Gzip 压缩 (可选)
    # gzip on;

    # 定义我们的反向代理服务器
    server {
        # 监听 8080 端口
        listen 8080;
        # 服务器名,接受所有指向这个 Space 的域名
        server_name _;

        # 处理所有以 /ai/ 开头的请求
        location /ai/ {
            # 重写 URL,去掉 /ai/ 前缀
            rewrite ^/ai/(.*)$ /$1 break;
            proxy_ssl_server_name on;
            proxy_ssl_protocols TLSv1.2 TLSv1.3;
            proxy_ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;        
           
            # 转发请求到目标服务器
            proxy_pass https://cc.cwapi.me;

            # --- 保持这些重要的头部设置 ---
            proxy_set_header Host $host; # 注意这里可能需要改成 proxy_set_header Host cc.cwapi.me; 如果你的后端服务需要正确的 Host 头
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;          
            proxy_set_header Authorization $http_authorization;

            # --- 缓冲区设置 (现在应该可以工作了) ---
            proxy_buffers 16 16k;
            proxy_buffer_size 32k;

            # --- 增加超时设置 (可选,但对 API 调用有好处) ---
            proxy_connect_timeout 60s; # 连接后端超时时间
            proxy_send_timeout 60s;    # 发送请求到后端超时时间
            proxy_read_timeout 120s;   # 读取后端响应超时时间 (API 可能需要较长时间响应)
        }

        # 处理根路径 / 的请求
        location / {
            add_header Content-Type text/plain;
            return 404 'Not Found: Please access via /ai/ path\n';
        }

        # (可选) 将 Nginx 自身的错误日志定向到标准错误输出,方便在 HF Logs 查看
        error_log /dev/stderr warn;
        # (可选) 将 Nginx 的访问日志定向到标准输出
        access_log /dev/stdout; # 可以用上面的 log_format main;
    }

    # (可选) 你可以禁用错误日志到文件,如果你不希望它们写入容器文件系统
    # error_log /dev/null crit; # 禁用错误日志文件
}