nignx部署前端项目

个人总结了3种方法来实现在一台服务器上使用nginx部署多个前端项目的方法。

  • 基于域名配置
  • 基于端口配置
  • 基于location配置

基于域名配置(常用)

基于域名配置,前提是先配置好了域名解析。比如说你自己买了一个域名:www.fly.com。 然后你在后台配置了2个它的二级域名: a.fly.com、 b.fly.com。

配置文件如下:

  • 配置 a.fly.com 的配置文件:vim /usr/nginx/modules/a.conf
  • server {
    listen 80;
    server_name a.fly.com;

    location / {
    root /data/web-a/dist;
    index index.html;
    }
    }

  • 配置 b.fly.com 的配置文件:vim /usr/nginx/modules/b.conf
  • server {
    listen 80;
    server_name b.fly.com;

    location / {
    root /data/web-b/dist;
    index index.html;
    }
    }

基于端口配置(不常用)

配置文件如下:

  • 配置 a.fly.com 的配置文件:vim /usr/nginx/modules/a.conf
  • server {
    listen 8000;

    location / {
    root /data/web-a/dist;
    index index.html;
    }
    }

    # nginx 80端口配置 (监听a二级域名)
    server {
    listen 80;
    server_name a.fly.com;

    location / {
    proxy_pass http://localhost:8000; #转发
    }
    }

  • 配置 b.fly.com 的配置文件:vim /usr/nginx/modules/b.conf
  • server {
    listen 8001;

    location / {
    root /data/web-b/dist;
    index index.html;
    }
    }

    # nginx 80端口配置 (监听b二级域名)
    server {
    listen 80;
    server_name b.fly.com;

    location / {
    proxy_pass http://localhost:8001; #转发
    }
    }

基于location配置(常用)

配置文件如下:

  • 配置 a.fly.com 的配置文件:vim /usr/nginx/modules/ab.conf
  • server {
    listen 80;

    location / {
    root /data/web-a/dist;
    index index.html;
    }

    location /web-b {
    alias /data/web-b/dist;
    index index.html;
    }
    }

参考:https://www.cnblogs.com/zhaoxxnbsp/p/12691398.html

posted @ 2022-03-07 10:42  0o飞行天下o0  阅读(77)  评论(0编辑  收藏  举报