天天看点

vue路由模式history 配置nginx

在nginx里配置了以下配置后, 可能首页没有问题,但链接其他会出现(404)

location / {
            root   D:\Test\exprice\dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            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';
        }
        
        location ^~/api/ {
            proxy_pass   http://39.105.109.245:8080/;
        }
           

 为了解决404,需要通过以下两种方式:

  1、官网推荐

location / {
  root   D:\Test\exprice\dist;
  index  index.html index.htm;
  try_files $uri $uri/ /index.html;
}
           

2、匹配errpr_page

location /{
  root   /data/nginx/html;
  index  index.html index.htm;
  error_page 404 /index.html;
}      

继续阅读