# 一.环境配置

# 1.dev配置

server {
     server_name dev.zhoubichuan.com;

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types text/css application/javascript;
          root /usr/src/zhoubichuan/dev/;
          try_files $uri $uri/ /index.html;
          index index.html index.htm;
     }

     location ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
     }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }

     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }
}
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

# 2.sit配置

server {
     server_name sit.zhoubichuan.com;

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types text/css application/javascript;
          root /usr/src/zhoubichuan/sit/;
          try_files $uri $uri/ /index.html;
          index index.html index.htm;
     }

     location ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
     }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }

     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }
}
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

# 3.uat配置

server {
     server_name uat.zhoubichuan.com;

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types text/css application/javascript;
          root /usr/src/zhoubichuan/uat/;
          try_files $uri $uri/ /index.html;
          index index.html index.htm;
     }

     location ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
     }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }

     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }
}
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

# 4.pet配置

server {
     server_name pet.zhoubichuan.com;

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types text/css application/javascript;
          root /usr/src/zhoubichuan/pet/;
          try_files $uri $uri/ /index.html;
          index index.html index.htm;
     }

     location ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
     }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }

     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }
}
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

# 5.sim配置

server {
     server_name sim.zhoubichuan.com;

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types text/css application/javascript;
          root /usr/src/zhoubichuan/sim/;
          try_files $uri $uri/ /index.html;
          index index.html index.htm;
     }

     location ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
     }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }

     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }
}
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

# 6.prod配置

server {
     server_name zhoubichuan.com; # 填写相应的子域名,不需要加 https:// 的前缀。填写这个使得 Nginx 可以正确匹配不同子域的请求

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types text/css application/javascript;
          root /usr/src/zhoubichuan/prod/; # 指示这个子域名所对应的文件夹根目录的位置
          try_files $uri $uri/ /index.html;
          index index.html index.htm; # 显示主页时所使用的文件名
     }

     location ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
          proxy_http_version 1.1; # 设置 HTTP 版本,SSE 需要 HTTP/1.1
          proxy_set_header Connection ''; # 保持连接活性,不发送连接关闭的信号
          proxy_buffering off; #不对 SSE 响应进行缓冲,直接透传给客户端
          proxy_cache off; # 关闭代理缓存
          proxy_read_timeout 3600s; # 设置代理读取服务器响应的超时时间
          proxy_send_timeout 3600s;
          proxy_connect_timeout 1h; # 设置客户端连接的超时时间
          proxy_set_header Host $host; # 配置代理传递的头部,确保 Host 头部正确传递
          proxy_set_header X-Accel-Buffering no; # 设置代理的响应头部,保持传输编码为 chunked
          add_header Cache-Control no-cache;
          # add_header X-Accel-Buffering "no";
          chunked_transfer_encoding on; # 启用分块传输编码
          gzip off;


          # 设置跨域资源共享 (CORS),如果你的客户端和服务器不在同一个域上
          add_header 'Access-Control-Allow-Origin' '*' always;
          add_header 'Access-Control-Allow-Credentials' 'true' always;
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
          add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With' always;
          if ($request_method = 'OPTIONS') {
               # 如果请求方法为 OPTIONS,则返回 204 (无内容)
               add_header 'Access-Control-Allow-Origin' '*';
               add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
               add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With';
               add_header 'Access-Control-Max-Age' 1728000;
               add_header 'Content-Type' 'text/plain charset=UTF-8';
               add_header 'Content-Length' 0;
               return 204;
          }
     }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }
     location ~* ^(/api/docs/) {
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $remote_addr;
          #proxy_set_header Host $host:$server_port;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Port $server_port;
          proxy_pass http://localhost:7005; # 后端服务地址
     }
     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }

     location ^~/child/resume {
          root html;
          index index.html index.htm;
          try_files $uri $uri/ /child/resume/index.html;
     }

          # todo-rust API 反向代理(v1.2.0 增量更新)
     # 遵循增量修改原则,保留原有功能,仅增强 CORS 和路径处理

     # agricultural-smart-rust 反向代理(HTTP)
     location ^~/agricultural-smart-rust/ {
          proxy_pass http://127.0.0.1:8088;
          proxy_http_version 1.1;
          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;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_connect_timeout 60s;
          proxy_send_timeout 60s;
          proxy_read_timeout 60s;
          add_header Access-Control-Allow-Origin * always;
          add_header Access-Control-Allow-Methods 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
          add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;
          if ($request_method = 'OPTIONS') { return 204; }
     }

     location ^~/todo-rust/api {
          # 代理到后端服务(注意:后端路由已包含 /todo-rust/api,此处直接透传)
          proxy_pass http://127.0.0.1:8080;

          # 标准代理头
          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;

          # 超时设置
          proxy_connect_timeout 60s;
          proxy_send_timeout 60s;
          proxy_read_timeout 60s;

          # CORS 配置(由 Nginx 统一处理,Rust 层不再处理)
          add_header Access-Control-Allow-Origin * always;
          add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS' always;
          add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;

          # 预检请求直接返回 204
          if ($request_method = 'OPTIONS') {
               return 204;
          }
     }

     # antdpro-express API proxy
    location /antdpro-express/api/ {
        rewrite ^/antdpro-express/api/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:7001;
        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_connect_timeout 60s;
        proxy_read_timeout 60s;
    }
}
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

# 7.micro配置

server {
     server_name micro.zhoubichuan.com;

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types text/css application/javascript;
          root /usr/src/zhoubichuan/micro/;
          try_files $uri $uri/ /index.html;
          index index.html index.htm;
     }

     location ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
     }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
          # include /etc/nginx/conf.d/*.conf;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }

     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }
}
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

# 9.http配置

server {
     listen 80; # Nginx 要监听 80 号端口
     server_name _; # 无论什么 hostname 都会被匹配
     return 301 https://$host$request_uri; # 用户的 HTTP 请求永久重定向(301 Moved Permanently)到 HTTPS 那边,并且保证链接的剩余部分不变
}
1
2
3
4
5

# 10.https配置



server {
     listen 443 ssl; # managed by Certbot
     ssl_certificate /etc/letsencrypt/live/zhoubichuan.com/fullchain.pem; # managed by Certbot
     ssl_certificate_key /etc/letsencrypt/live/zhoubichuan.com/privkey.pem; # managed by Certbot
     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

     root /usr/src/zhoubichuan/prod/; # 与 prod.conf 保持一致的根目录
     index index.html index.htm;

     gzip on;
     gzip_min_length 1k;
     gzip_http_version 1.1;
     gzip_comp_level 9;
     gzip_types text/css application/javascript text/plain application/json;

     # agricultural-smart-rust 反向代理(HTTPS)
     location ^~/agricultural-smart-rust/ {
          proxy_pass http://127.0.0.1:8088;
          proxy_http_version 1.1;
          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;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_connect_timeout 60s;
          proxy_send_timeout 60s;
          proxy_read_timeout 60s;
          add_header Access-Control-Allow-Origin * always;
          add_header Access-Control-Allow-Methods 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
          add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;
          if ($request_method = 'OPTIONS') { return 204; }
     }

     # todo-rust API 反向代理(HTTPS 端口 443)
     location ^~/todo-rust/api {
          proxy_pass http://127.0.0.1:8080;
          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;
          proxy_connect_timeout 60s;
          proxy_send_timeout 60s;
          proxy_read_timeout 60s;
          add_header Access-Control-Allow-Origin * always;
          add_header Access-Control-Allow-Methods 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
          add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;
          if ($request_method = 'OPTIONS') { return 204; }
     }

     # todo-admin SPA 路由
     location ^~/todo-admin {
          alias /usr/src/zhoubichuan/prod/todo-admin;
          index index.html;
          try_files $uri $uri/ /todo-admin/index.html;
     }

     location / {
          try_files $uri $uri/ /index.html;
     }

     # 静态资源缓存优化
     location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
          expires 30d;
          add_header Cache-Control "public, immutable";
     }

     error_page 500 502 503 504 /50x.html;
     location = /50x.html {
          root /usr/share/nginx/html;
     }
}
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

# 11.默认配置

server {
    server_name zhoubichuan.com;

     location ~*^.+$ {
          gzip on;
          gzip_min_length 1k;
          gzip_http_version 1.1;
          gzip_comp_level 9;
          gzip_types  text/css application/javascript;
          root   /usr/src/zhoubichuan/prod/;
          try_files $uri $uri/ /index.html;
          index  index.html index.htm;
      }

     location  ^~/common-node/api {
          proxy_pass http://localhost:3000/api;
      }

     location ^~/antdpro-express/api {
          proxy_pass http://localhost:7001/api;
          # include /etc/nginx/conf.d/*.conf;
     }

     location ^~/abstract-express/api {
          proxy_pass http://localhost:7005/api;
     }

     location ^~/abstract-egg/api {
          proxy_pass http://localhost:7006/api;
     }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
                                                                                                                                                                                          1,8          顶端

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/zhoubichuan.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/zhoubichuan.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = zhoubichuan.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen       80;
    server_name zhoubichuan.com;
    return 404; # managed by Certbot

}
                                                                                                                                                                                          89,1         底端
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
刷新
全屏/自适应