最近在nginx中配置vue的项目时,碰到了刷新页面404的问题,问了前端,原来vue的模式改为了history,所以nginx的配置需要改变。
server {
listen 80;
listen 443 ssl;
server_name xx.xx.xx.com;
ssl_certificate /xx/xx/xx.pem;
ssl_certificate_key /xx/xx/xx.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
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;
charset utf-8;
# 访问 xx.xx.xx.com/aa, aa=vue项目的别名, nginx中的文件夹也是aa,
location /aa {
index index.html index.htm;
try_files $uri $uri/ @router_aa;
}
# 访问 xx.xx.xx.com/bb, bb=vue项目的别名, nginx中的文件夹也是bb
location /bb {
index index.html index.htm;
try_files $uri $uri/ @router_bb;
}
# 以下两个配置中的/aa和/bb的/, 其实是root, 也就是nginx默认的html
location @router_aa {
rewrite ^.*$ /aa/index.html last;
}
location @router_bb {
rewrite ^.*$ /bb/index.html last;
}
}
vue中的配置如下:
// vue.config.js
module.exports = {
// 注意:项目根路径
publicPath: '/aa',
// 注意:静态文件路径
assetsDir: "static",
}
如果配置有什么问题或者说明补充,欢迎留言探讨