前端项目打包

打包之前在Vue的配置文件里去修改公共路径的配置,改成前端项目服务的名字。例如 admin

vue.config.js

  1. module.exports = {
  2. publicPath: '/admin/' //公共路径
  3. }

同时在项目的router目录下 index.js 为Vue项目指定路由基本路径为 /admin/
router/index.js

  1. const createRouter = () => new Router({
  2. mode: 'history',
  3. base: '/admin/', //配置nginx访问结构
  4. })

对应的 nginx 配置文件

  1. http {
  2. server {
  3. # 监听的端口号
  4. listen 9200;
  5. # 服务名称 生产环境要修改成 公网ip 如 47.105.134.120
  6. server_name localhost;
  7. # 配置根目录的地址是以 nginx 下的 html 文件夹为根目录来查找的
  8. root html;
  9. # 配置默认的主页显示 比如 47.105.134.120:8080 显示的 index 页面
  10. location / {
  11. try_files $uri $uri/ /index.html;
  12. }
  13. # 配置我们的 admin 的前台服务 比如 47.105.134.120:8080/admin/index.html
  14. location ^~ /admin {
  15. # 处理 Vue 单页面应用 路由模式为 history 模式刷新页面 404 的问题
  16. try_files $uri $uri/ /admin/index.html;
  17. }
  18. # 如果你想配置多个项目的话,可以复制上面的,修改一下就可以同时部署多个前端项目了
  19. # 比如
  20. # location ^~ /blog {
  21. # 处理 Vue 单页面应用 路由模式为 history 模式刷新页面 404 的问题
  22. # try_files $uri $uri/ /blog/index.html;
  23. # }
  24. #error_page 404 /404.html;
  25. # redirect server error pages to the static page /50x.html
  26. #
  27. error_page 500 502 503 504 /50x.html;
  28. location = /50x.html {
  29. root html;
  30. }
  31. }
  32. include servers/*;
  33. }

配置文件 参考

参考1

  1. server {
  2. # 需要被监听的端口号,前提是此端口号没有被占用,否则在重启 Nginx 时会报错
  3. listen 8888;
  4. # 服务名称即部署的IP,生产环境需要改为服务器的公网
  5. server_name localhost;
  6. # 上述端口指向的根目录 即 配置前端打包文件的映射路径
  7. root /opt/asing1elife/teamnote;
  8. # 项目根目录中指向项目首页
  9. index index.html;
  10. client_max_body_size 20m;
  11. client_body_buffer_size 128k;
  12. # 根请求会指向的页面
  13. location / {
  14. # 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404
  15. # 处理vue单页面应用路由模式为history模式刷新页面404问题
  16. try_files $uri $uri/ @router; #try_files $uri $uri/ /index.html $uri/ =404;
  17. # 请求指向的首页
  18. index index.html;
  19. }
  20. # 由于路由的资源不一定是真实的路径,无法找到具体文件
  21. # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源
  22. location @router {
  23. rewrite ^.*$ /index.html last;
  24. }
  25. # 关键步骤,这里表示将所有的 http://192.168.7.8:8888/teamnote/api/ 开头的请求都转发到下面 proxy_pass 指定的链接中
  26. # 这里使用 /teamnote/api/ 而不是 /teamnote/ ,是因为前端项目本身的访问链接就是 http:192.168.7.8:8888/teamnote/
  27. # 为了防止在访问页面时请求就被 Nginx 代理转发,这里需要更具体的配置,才能和前端访问请求区分开
  28. location /teamnote/api/ {
  29. # 后端的真实接口
  30. proxy_pass http://192.168.7.8:2592/teamnote/api/;
  31. proxy_redirect off;
  32. proxy_set_header Host $host;
  33. proxy_set_header X-Real-IP $remote_addr;
  34. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  35. proxy_set_header Cookie $http_cookie;
  36. # for Ajax
  37. #fastcgi_param HTTP_X_REQUESTED_WITH $http_x_requested_with;
  38. proxy_set_header HTTP-X-REQUESTED-WITH $http_x_requested_with;
  39. proxy_set_header HTTP_X_REQUESTED_WITH $http_x_requested_with;
  40. proxy_set_header x-requested-with $http_x_requested_with;
  41. client_max_body_size 10m;
  42. client_body_buffer_size 128k;
  43. proxy_connect_timeout 90;
  44. proxy_send_timeout 90;
  45. proxy_read_timeout 90;
  46. proxy_buffer_size 128k;
  47. proxy_buffers 32 32k;
  48. proxy_busy_buffers_size 128k;
  49. proxy_temp_file_write_size 128k;
  50. }
  51. }

参考2

  1. #仓储管理
  2. server {
  3. listen 9528;
  4. server_name 192.168.1.95;
  5. location / {
  6. root /opt/server/store-ui/warehouse-management/dist;
  7. try_files $uri $uri/ /index.html $uri/ =404;
  8. index index.html index.htm;
  9. add_header X-Content-Type-Options 'nosniff';#禁止嗅探文件类型
  10. add_header Content-Security-Policy "script-src 'self' 'unsafe-eval' 'unsafe-inline' http://192.168.1.25:7000";#只允许同源下的js
  11. add_header X-XSS-Protection "1; mode=block";#开启XSS过滤器
  12. }
  13. }