使用nginx部署多个vue项目,亲测有效(使用域名https方式访问)

28 篇文章 1 订阅
2 篇文章 0 订阅

1.修改nginx.conf配置

    server {
        listen   443 ssl;
server_name  你的域名;
#ssl on; # 开启ssl证书访问
ssl_certificate  ./1_bundle.crt;  #你的证书crt文件地址
ssl_certificate_key  ./2_.key;  #你的证书key文件地址
ssl_session_timeout  60m; # session有效时间10分钟
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; # 按照这个套件配置
#ssl_prefer_server_ciphers on;        
        #charset koi8-r;

        #access_log  logs/host.access.log  main
        #项目一的配置                                                                                                             
        location / {
           proxy_pass http://你的网址:80;
           proxy_set_header Host $proxy_host;	#获取正确的IP和端口号
           proxy_set_header X-Real-IP $remote_addr;	#获取正确请求的远程IP
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;	#获取正确的ip
            #proxy_set_header X-Forwarded-Proto $scheme;		#获取正确的请求协议
            #proxy_set_header X-Forwarded-Port $server_port; #获取正确的访问端口号
     index  index.html index.htm;
     root html;
     try_files $uri $uri/ /index.html;  //映射到index.html上
      client_max_body_size 100M;
        }
         #项目二的配置  
     location ^~ /manager/{
       proxy_set_header  Host       $host;
       proxy_set_header  X-Real-IP    $remote_addr;
       proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
}
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

2.第二个vue项目打包时需要修改两处(其中"/manager/"可根据自己的需要修改)

2.1 修改config/index.js文件中的build{}里的assetsPublicPath:"/manager/";
2.2 修改router/index.js添加 base: ‘/manager/’ ;

3.在nginx的html文件夹下创建manger文件夹,如下图
在这里插入图片描述
4.将第二个项目打包的dist文件夹中的内容拷到manager文件夹下,如下图:

在这里插入图片描述
5.重启nginx,即可通过https://域名/manager/访问第二个网站了.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Nginx部署多个Vue项目,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Nginx。如果尚未安装,请根据你的操作系统的要求进行安装。 2. 在Nginx的配置文件夹中创建一个新的配置文件,比如 `/etc/nginx/conf.d`。在该文件夹中创建一个名为 `myproject.conf` 的文件,用于配置你的项目。 3. 打开 `myproject.conf` 文件,并输入以下内容: ``` server { listen 80; server_name your_domain.com; // 替换为你的域名或IP地址 location /project1 { alias /path/to/project1/dist; // 替换为第一个Vue项目的dist目录路径 try_files $uri $uri/ /project1/index.html; } location /project2 { alias /path/to/project2/dist; // 替换为第二个Vue项目的dist目录路径 try_files $uri $uri/ /project2/index.html; } // 可以继续添加其他项目的配置 error_page 404 /index.html; } ``` 上述配置中,我们使用了 `alias` 指令将每个项目的 `dist` 目录映射到对应的路径。`try_files` 指令用于处理路由请求,确保能够正确访问到每个Vue项目的路由。 4. 保存并关闭文件。 5. 检查配置文件是否存在语法错误:执行 `nginx -t` 命令。 6. 如果没有错误,重新加载Nginx配置文件:执行 `sudo service nginx reload`(根据你的操作系统和Nginx安装方式可能会有所不同)。 7. 确保你的域名或IP地址正确解析到服务器,并尝试访问 `http://your_domain.com/project1` 和 `http://your_domain.com/project2`,应该能够分别访问到两个Vue项目。 按照上述步骤,你可以配置Nginx部署多个Vue项目。你可以根据需要添加更多的项目配置。在配置完成后,确保每个项目的 `dist` 目录已经构建并位于正确的路径上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值