vue-router+nginx 非根路径配置方法
vue-router+nginx 非根路径配置方法
vue-router 的默认数据hash模式-使用url的hash来模拟一个完整的URL,因而当URL改变时,页面不会从新加载。html
通常状况下,咱们不喜欢丑丑的hash,相似于index.html#/matchResult,能够使用路由的history模式。history模式是利用history.pushState API来实现页面跳转。vue
可是有个问题,在使用nginx的时候,咱们须要添加一些配置。nginx
直接配置在根路径下 直接配置在根路径下,访问的时候只用输入http://yoursite.com,在nginx的配置以下vue-router
location / {
try_files $uri $uri/ /index.html;
}
非根路径配置
若是一个域名下有多个项目,那么使用根路径配置就不合适了,咱们须要在根路径下指定一层路径,好比说url
A项目code
http://yoursite.com/A
B项目router
http://yoursite.com/B
nginx的配置
location ^~/A {
alias /XX/A;//此处为A的路径
index index.html;
try_files $uri $uri/ /A/index.html;
}
location ^~/B {
alias /XX/B;//此处为B的路径
index index.html;
try_files $uri $uri/ /B/index.html;
}
头部方面其他设置
try_files $uri $uri/ /index.html;
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;
Last updated