File size: 3,478 Bytes
eafa1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7a0916
eafa1b0
 
 
 
 
 
 
 
 
8da93e4
eafa1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8da93e4
eafa1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8da93e4
eafa1b0
 
 
 
 
8da93e4
eafa1b0
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 全局配置
user www-data;
worker_processes auto;
worker_rlimit_nofile 51200;
error_log /var/log/nginx/error.log crit;
pid /var/run/nginx.pid;

events {
    use epoll;
    worker_connections 51200;
    multi_accept on;
}

http {
    include mime.types;
    default_type application/octet-stream;
    client_max_body_size 1024m;

    # 日志格式
    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 on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # Gzip 压缩
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # 代理缓存配置
    proxy_cache_path /var/cache/nginx
        levels=1:2
        keys_zone=nginx_cache:10m
        max_size=1g
        inactive=24h
        use_temp_path=off;

    # 主服务器配置
    server {
        listen 3000;  # 修改为 3000 端口
        server_name localhost;

        # 健康检查
        location /health {
            return 200 "OK";
        }

        # 根路径代理
        location / {
            proxy_pass http://127.0.0.1:3000;  # 修改为 3001 端口
            proxy_set_header Host $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;

            # WebSocket 支持
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';

            # WebSocket 超时设置
            proxy_read_timeout 300s;
            proxy_send_timeout 300s;
        }

        # 静态资源缓存
        location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot)(.*) {
            proxy_pass http://127.0.0.1:3000;  # 修改为 3001 端口
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            # 启用浏览器缓存
            add_header Cache-Control "public, max-age=31536000, immutable";
        }

        # 模型接口缓存
        location /api/models {
            proxy_cache nginx_cache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 10m;
            proxy_cache_background_update on;
            proxy_cache_use_stale updating;
            proxy_cache_revalidate on;
            proxy_cache_min_uses 1;
            proxy_pass http://127.0.0.1:3000;  # 修改为 3001 端口
            add_header X-Cache-Status $upstream_cache_status;
        }

        # 排除版本检查 API 的缓存
        location /api/check_version {
            proxy_pass http://127.0.0.1:3000;  # 修改为 3001 端口
            proxy_no_cache 1;
        }

        # 安全头配置
        add_header X-Content-Type-Options "nosniff";
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header Content-Security-Policy "default-src 'self';";
    }
}