项目部署在Tomcat
vue-cli开发过程中路由#不好看,去掉可使用history模式,开发模式路径访问都没问题,部署到服务器之后访问路径时报404,这种情况需要配置服务器默认访问路径index.html.官网只提供了apache、nginx的配置方式,没有tomcat的配置方式
文件夹下新建WEB-INF/web.xml 其中添加404错误跳转路径
<?xml version='1.0' encoding='UTF-8'?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd"id="scplatform" version="2.5"><display-name>/</display-name><error-page><error-code>404</error-code><location>/index.html</location></error-page></web-app>
项目部署在Nginx
# html设置history模式location / {index index.html index.htm;proxy_set_header Host $host;# history模式最重要就是这里try_files $uri $uri/ /index.html;# index.html文件不可以设置强缓存 设置协商缓存即可add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';}# 接口反向代理location ^~ /api/ {# 跨域处理 设置头部域名add_header Access-Control-Allow-Origin *;# 跨域处理 设置头部方法add_header Access-Control-Allow-Methods 'GET,POST,DELETE,OPTIONS,HEAD';# 改写路径rewrite ^/api/(.*)$ /$1 break;# 反向代理proxy_pass http://static_env;proxy_set_header Host $http_host;}location ~* \.(?:css(\.map)?|js(\.map)?|gif|svg|jfif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {# 静态资源设置七天强缓存expires 7d;access_log off;}
