项目部署在Tomcat

vue-cli开发过程中路由#不好看,去掉可使用history模式,开发模式路径访问都没问题,部署到服务器之后访问路径时报404,这种情况需要配置服务器默认访问路径index.html.官网只提供了apache、nginx的配置方式,没有tomcat的配置方式

文件夹下新建WEB-INF/web.xml 其中添加404错误跳转路径

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd"
  5. id="scplatform" version="2.5">
  6. <display-name>/</display-name>
  7. <error-page>
  8. <error-code>404</error-code>
  9. <location>/index.html</location>
  10. </error-page>
  11. </web-app>

项目部署在Nginx

  1. # html设置history模式
  2. location / {
  3. index index.html index.htm;
  4. proxy_set_header Host $host;
  5. # history模式最重要就是这里
  6. try_files $uri $uri/ /index.html;
  7. # index.html文件不可以设置强缓存 设置协商缓存即可
  8. add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';
  9. }
  10. # 接口反向代理
  11. location ^~ /api/ {
  12. # 跨域处理 设置头部域名
  13. add_header Access-Control-Allow-Origin *;
  14. # 跨域处理 设置头部方法
  15. add_header Access-Control-Allow-Methods 'GET,POST,DELETE,OPTIONS,HEAD';
  16. # 改写路径
  17. rewrite ^/api/(.*)$ /$1 break;
  18. # 反向代理
  19. proxy_pass http://static_env;
  20. proxy_set_header Host $http_host;
  21. }
  22. 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)$ {
  23. # 静态资源设置七天强缓存
  24. expires 7d;
  25. access_log off;
  26. }