nginx 部署多个前端项目

NGINX中单设置一个web前端很容易,改变端口,改变root指向基本就搞定了,那么,假设我们有多个前端项目部署在同一个域名下,该怎么设置呢?

1. 先假设我们有一个应用,http://domain就可以访问了, 现在改版了希望可以访问旧的应用http://domain,同时也可以通过http://domain/new访问新应用,那么首先第一步就是需要对新的前端项目进行一些配置。
     a. 在vue.config.js中设置 publicPath: '/new/',

     b. 在路由index.js中设置   base:'/new/',


     c. 在index.html中加入<meta base=/new/>

    2. 修改NGINX设置,基本就是使location /指向原来的web前端,  location /new指向新的web前端

          a. 先在NGINX目录下/usr/share/nginx/html 创建新的文件夹new,把新项目的static,index.html放置于new目录,注意不是dist放到new目录


         new 目录结构

b.  vim /etc/nginx/nginx.conf,修改server 部份

 


 
 
  1. #For more information on configuration, see:
  2. # * Official English Documentation: http: //nginx.org/en/docs/
  3. # * Official Russian Documentation: http: //nginx.org/ru/docs/
  4. #user nginx;
  5. user root;
  6. worker_processes auto;
  7. error_log / var/log/nginx/error.log;
  8. pid /run/nginx.pid;
  9. # Load dynamic modules. See /usr/share/nginx/README.dynamic.
  10. include /usr/share/nginx/modules /*.conf;
  11. events {
  12. worker_connections 1024;
  13. }
  14. http {
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18. access_log /var/log/nginx/access.log main;
  19. sendfile on;
  20. tcp_nopush on;
  21. tcp_nodelay on;
  22. keepalive_timeout 65;
  23. types_hash_max_size 2048;
  24. include /etc/nginx/mime.types;
  25. default_type application/octet-stream;
  26. # Load modular configuration files from the /etc/nginx/conf.d directory.
  27. # See http://nginx.org/en/docs/ngx_core_module.html#include
  28. # for more information.
  29. #include /etc/nginx/conf.d/*.conf;
  30. server {
  31. listen 8099;
  32. server_name localhost;
  33. #此为nginx本身index目录
  34. #root /usr/share/nginx/html;
  35. # Load configuration files for the default server block.
  36. include /etc/nginx/default.d/*.conf;
  37. location / {
  38. 此为旧应用index,static目录
  39. root /usr/share/nginx/html/dist;
  40. try_files $uri $uri/ @router;
  41. index index.html index.htm;
  42. }
  43. location /new {
  44. #此为新应用index,static目录,同时注意这里是alias,不是root,还有以及new的后面有/结尾
  45. alias /usr/share/nginx/html/new/;
  46. try_files $uri $uri/ /new/index.html;
  47. index index.html index.htm;
  48. }
  49. location @router {
  50. rewrite ^.*$ /index.html last;
  51. }
  52. error_page 404 /404.html;
  53. location = /40x.html {
  54. }
  55. error_page 500 502 503 504 /50x.html;
  56. location = /50x.html {
  57. }
  58. }
  59. # Settings for a TLS enabled server.
  60. #
  61. # server {
  62. # listen 443 ssl http2 default_server;
  63. # listen [::]:443 ssl http2 default_server;
  64. # server_name _;
  65. # root /usr/share/nginx/html;
  66. #
  67. # ssl_certificate "/etc/pki/nginx/server.crt";
  68. # ssl_certificate_key "/etc/pki/nginx/private/server.key";
  69. # ssl_session_cache shared:SSL:1m;
  70. # ssl_session_timeout 10m;
  71. # ssl_ciphers HIGH:!aNULL:!MD5;
  72. # ssl_prefer_server_ciphers on;
  73. #
  74. # # Load configuration files for the default server block.
  75. # include /etc/nginx/default.d/*.conf;
  76. #
  77. # location / {
  78. # }
  79. #
  80. # error_page 404 /404.html;
  81. # location = /40x.html {
  82. # }
  83. #
  84. # error_page 500 502 503 504 /50x.html;
  85. # location = /50x.html {
  86. # }
  87. # }
  88. }

c.  /usr/sbin/nginx -t 验证conf文件是否配置正确

d. /usr/sbin/nginx -s reload 重新加载NGINX配置生效
    3. 最后通过http://localhost:8099就能访问到旧前端,而通过http://localhost:8099/new就能访问到新前端

网上有其他很多写的NGINX反向代理部署多个前端,其前提应该是这个应用都已经可以正常访问,只不过是在某个域和端口进行转发,使用户访问方便。
    
在些过程中遇到的一些问题:
    1. 一开始只对NGINX进行了设置,在new文件夹下放了一个index.html,这样只能访问到index,而不能访问到新的项目,因为一些编译好的文件路径不对,导致没法加载
    2. 404错误,一般来说就是配置和真实的路径不符合
    3. 设置多个前端的时候需要把原来默认的全局ROOT 路径注释掉
    4. 注意每行设置最后的分号,反正我自己好几次忘了
    5. 重启NGINX可能发生的错误: 
        a. nginx[13499]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use),使用 netstat -ntpl或者ps -ef | grep nginx找到相应进程,然后kill 进程ID
        b. nginx start failed,pid找不到,   ./nginx -c -/etc/nginx/nginx.conf
    

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值