Appearance
Nginx Vue 应用生产级配置示例
Nginx
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
map $sent_http_content_type $yourapp_expires {
default off;
~^text/html epoch; # html 不缓存!
application/javascript 7d;
text/css 7d;
~^image/ 30d;
~^font/ 30d;
~*woff 30d; # 同时匹配 woff 和 woff2
}
server {
listen 443 ssl http2; # 同时支持 http/1.1 和 http/2
listen [::]:443 ssl http2;
server_name app.yourcompany.com;
include /path/to/your/ssl.conf;
add_header X-Frame-Options "SAMEORIGIN"; # 防止网站被放在 iframe、frame 或 object 中
add_header X-Content-Type-Options "nosniff"; # 禁止浏览器对 Content-Type 进行猜测
root /var/www/yourapp/dist;
index index.html;
# 禁止访问隐藏文件
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# API 发到后端(根据实际情况配置)
location ~ ^/(?:api|gw)(/.*)?$ {
proxy_pass http://127.0.0.1:3000$1$is_args$args; # 或 upstream 负载均衡
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 X-Forwarded-Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location / {
try_files $uri $uri/ $uri.html /index.html; # 兼容 history 路由、SSG
expires $yourapp_expires;
add_header Cache-Control "public";
access_log off;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
# 开启 gzip
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_types
text/plain
text/css
application/javascript
application/x-javascript
text/xml
application/xml
application/xml+rss
text/javascript
image/svg+xml
application/json;
# access_log /var/log/nginx/yourapp.access.log main buffer=32k flush=5m;
# error_log /var/log/nginx/yourapp.error.log warn;
}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
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
/path/to/your/ssl.conf参考配置;