前端采用vue框架
路由采用hash模式
把前端打包好的项目直接放进 nginx/html 目录下面
nginx/nginx.conf 部分配置
server {listen 80;server_name www.axample.com;#rewrite ^ https://$server_name$request_uri permanent;#charset koi8-r;#access_log logs/host.access.log main;# 在443端口配置里设置location / {root html;index index.html index.htm;try_files $uri $uri/ /index.html; # uri统一资源标志符}}# 浏览器访问:https://www.axample.com/lily/https://www.axample.com/mimosa/
路由采用的都是history模式
1、router.js设置
new Router({mode: 'history',base: '/mimosa/',routes,});# base 应用的基路径# 默认值: "/"。# 例如,如果整个单页应用服务在 /app/ 下,然后 base 就应该设为 "/app/"。
2、vue.config.js修改publicPath
如果只修改了router的设置,没有修改publicPath,浏览器地址栏键入 https://www.axample.com/lily/ 首次进入由于vue-router设置的 redirect 会重定向到首页地址,正常显示网页,但是再次刷新的时候,浏览器请求的是重定向过后的地址,静态文件地址指向错乱,404一般来说就是配置和真实的路径不符合,需要指定try_files
publicPath: IS_PROD ? '/lily/' : '/', // 公共路径
publicPath
部署应用包时的基本 URL。
用法和 webpack 本身的 output.publicPath 一致,
但是 Vue CLI 在一些其他地方也需要用到这个值,所以请始终使用 publicPath 而不要直接修改 webpack 的 output.publicPath。
- 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上,
- 如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。
例如,如果你的应用被部署在 https://www.my-app.com/my-app/,则设置 publicPath 为 /my-app/。
3、nginx设置
# 443端口配置location / {root html;index index.html index.htm;try_files $uri $uri/ /index.html;}location /lily {#alias /usr/local/nginx/html/lily/;#把匹配到的路径重写, 注意要以/结尾root /usr/local/nginx/html; #在匹配到的路径前面,增加root基础路径配置try_files $uri $uri/ /lily/index.html; # 特定目录,匹配不到文件的话,增加/lily/index.html配置index index.html index.htm;}location /mimosa {#alias /usr/local/nginx/html/mimosa/;root /usr/local/nginx/html;try_files $uri $uri/ /mimosa/index.html;index index.html index.htm;}浏览器访问https://www.axample.com/lily/https://www.axample.com/mimosa/
域名+端口访问配置
静态资源访问配置
add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
相关连接:
vue-router之mode: https://www.yuque.com/allblue-byynd/izub4k/pttgel
网站更换https :https://www.yuque.com/allblue-byynd/izub4k/qcfkg3
