> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/centos/vue/vuerouter+nginx-fei-gen-lu-jing-pei-zhi-fang-fa.md).

# 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>

```bash
location / {
  try_files $uri $uri/ /index.html;
}
```

#### 非根路径配置

若是一个域名下有多个项目，那么使用根路径配置就不合适了，咱们须要在根路径下指定一层路径，好比说url

**A项目code**

```bash
http://yoursite.com/A
```

**B项目router**

```bash
http://yoursite.com/B
```

**nginx的配置**

```bash

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;
    }
```

{% hint style="info" %}
注意: 要用alias不能用root
{% endhint %}

### 头部方面其他设置

```python
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;
```
