1.Vue路由打包方式

(1)hash方式

即地址栏后面带#号的方式。
可以设置vue.config.js或者config.js,总之就是配置vue的地方。一般长这样:
1657778193936.png
首选设置:publicPath,可以使用./设置相对路径,这样打包完的静态文件放到网站任何位置都可以,此时设置src/routers/index.js文件,设置路由模式为hash,如下:

  1. export default new Router({
  2. mode: 'hash', // 去掉url中的#
  3. scrollBehavior: () => ({ y: 0 }),
  4. routes: constantRoutes
  5. })

(2)history方式

即地址栏中显示的path路径,拼接方式为:主路径+路由路径
这种方式下,publicPath必须使用相对站点根目录的路径方式,
例如:https://www.baidu.com/vue/test/index.html,像这样的地址路径,则publicPath必须设置为/vue/test
同时需要设置路由history模式并设置base为/vue/test:

  1. export default new Router({
  2. mode: 'history', // 去掉url中的#
  3. base: '/vue/test',//根目录相对路径
  4. scrollBehavior: () => ({ y: 0 }),
  5. routes: constantRoutes
  6. })

2.nginx配置

(1)配置转发

对于hash模式,nginx只需要配置静态路径的转发即可。但是对history需要做如下转发设置。

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. # 非根目录的转发
  5. location ^~ /vue/test {
  6. index index.html index.htm;
  7. alias html/admin;
  8. try_files $uri $uri/ /admin/index.html;
  9. }
  10. # 根目录的转发
  11. location / {
  12. root html;
  13. index index.html index.htm;
  14. add_header Cache-Control no-store;
  15. if (!-e $request_filename) {
  16. rewrite ^(.*)$ /index.html?s=$1 last;
  17. break;
  18. }
  19. }
  20. }

(2)开启GZIP压缩

  1. gzip on;
  2. gzip_http_version 1.1;
  3. gzip_vary on;
  4. gzip_comp_level 6;
  5. gzip_proxied any;
  6. gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  7. gzip_min_length 1k;
  8. gzip_buffers 16 8k;
  9. gzip_disable "MSIE [1-6]\.";

(3)完整配置文件

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. server_tokens off;
  22. proxy_hide_header X-Application-Context;
  23. proxy_hide_header Server;
  24. gzip on;
  25. gzip_http_version 1.1;
  26. gzip_vary on;
  27. gzip_comp_level 6;
  28. gzip_proxied any;
  29. gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  30. gzip_min_length 1k;
  31. gzip_buffers 16 8k;
  32. gzip_disable "MSIE [1-6]\.";
  33. server {
  34. listen 80;
  35. server_name localhost;
  36. #charset koi8-r;
  37. #access_log logs/host.access.log main;
  38. location ^~ /prod-api {
  39. proxy_pass http://127.0.0.1:8080;
  40. index index.html index.htm;
  41. proxy_set_header Host $host;
  42. proxy_set_header X-Real-IP $remote_addr;
  43. proxy_set_header REMOTE-HOST $remote_addr;
  44. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  45. set $realip $remote_addr;
  46. if ($http_x_forwarded_for ~ "^(\d+\.\d+\.\d+\.\d+)") {
  47. set $realip $1;
  48. }
  49. fastcgi_param REMOTE_ADDR $realip;
  50. }
  51. location ^~ /admin {
  52. index index.html index.htm;
  53. alias html/admin;
  54. try_files $uri $uri/ /admin/index.html;
  55. }
  56. location / {
  57. root html;
  58. index index.html index.htm;
  59. add_header Cache-Control no-store;
  60. if (!-e $request_filename) {
  61. rewrite ^(.*)$ /index.html?s=$1 last;
  62. break;
  63. }
  64. }
  65. #error_page 404 /404.html;
  66. # redirect server error pages to the static page /50x.html
  67. #
  68. error_page 500 502 503 504 /50x.html;
  69. location = /50x.html {
  70. root html;
  71. }
  72. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  73. #
  74. #location ~ \.php$ {
  75. # proxy_pass http://127.0.0.1;
  76. #}
  77. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  78. #
  79. #location ~ \.php$ {
  80. # root html;
  81. # fastcgi_pass 127.0.0.1:9000;
  82. # fastcgi_index index.php;
  83. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  84. # include fastcgi_params;
  85. #}
  86. # deny access to .htaccess files, if Apache's document root
  87. # concurs with nginx's one
  88. #
  89. #location ~ /\.ht {
  90. # deny all;
  91. #}
  92. }
  93. # another virtual host using mix of IP-, name-, and port-based configuration
  94. #
  95. #server {
  96. # listen 8000;
  97. # listen somename:8080;
  98. # server_name somename alias another.alias;
  99. # location / {
  100. # root html;
  101. # index index.html index.htm;
  102. # }
  103. #}
  104. # HTTPS server
  105. #
  106. #server {
  107. # listen 443 ssl;
  108. # server_name localhost;
  109. # ssl_certificate cert.pem;
  110. # ssl_certificate_key cert.key;
  111. # ssl_session_cache shared:SSL:1m;
  112. # ssl_session_timeout 5m;
  113. # ssl_ciphers HIGH:!aNULL:!MD5;
  114. # ssl_prefer_server_ciphers on;
  115. # location / {
  116. # root html;
  117. # index index.html index.htm;
  118. # }
  119. #}
  120. }