Nginx
coderljw 2024-10-13 小于 1 分钟
# 1. HTTP 缓存
server {
# 不缓存html
location ~ .*\.(html)$ {
add_header Cache-Control "public, no-cache";
}
# 媒体资源资源缓存一年
location ~ .*\.(jpe?g|png|svg|gif|webp|bmp|ico|cur|heic|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
add_header Cache-Control "public, max-age=31536000";
}
# 字体资源缓存一年
location ~ .*\.(woff2?|ttf|ttc|otf|eot|svgz?)$ {
add_header Cache-Control "public, max-age=31536000";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 2. gzip 配置
server {
gzip on;
gzip_static on;
gzip_comp_level 4;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 3. browserHistory 路由配置
server {
location / {
try_files $uri $uri/ /index.html;
}
}
1
2
3
4
5
2
3
4
5
# 4. HTTPS 配置
- 需要先安装 SSL 模块
server {
listen 443 ssl;
server_name matrixtheone.ga www.matrixtheone.ga;
ssl_certificate cert/matrixtheone.ga.pem;
ssl_certificate_key cert/matrixtheone.ga.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name matrixtheone.ga www.matrixtheone.ga;
return 301 https://$server_name$request_uri;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 5. 利用 Nginx 反向代理解决跨域问题
server {
listen 4396; # 端口
server_name localhost; # 服务名
location / {
proxy_pass http://localhost:3000/; # 前端页面
}
location ^~/agency/ {
proxy_pass http://www.coderljw.ga:5000/; # 后端接口
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
提示
通过 http://localhost:4396/
打开页面