让你一台服务器支持http和https同时运行多个项目(jar包和war包部署)

主要基于Nginx的负载均衡思路,http(80)和https(443)端口运行在Nginx上,配置nginx.conf里的location参数,映射到项目实际运行的端口上

1、阿里云linux服务器(Centos)下载和安装nginx

下载工具包和nginx安装包

yum install -y wget  
yum install -y vim-enhanced  
yum install -y make cmake gcc gcc-c++  
wget http://nginx.org/download/nginx-1.6.2.tar.gz

下载依赖包

yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

解压到/usr/local

tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/

进行configure配置 进入nginx-1.6.2目录然后执行./configure命令,--with是为了后面支持ssl,即使用443(https)访问

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

编译和安装

make && make install

Nginx常用命令

#启动
/usr/local/nginx/sbin/nginx
#查看端口
ps -ef | grep nginx
#关闭
/usr/local/nginx/sbin/nginx -s stop
#重新热启动nginx
/usr/local/nginx/sbin/nginx -s reload

2、配置nginx

nginx.conf,支持80端口(http)

#/usr/local/nginx/conf/nginx.conf
server {
#地址栏输入--http:你的域名/(服务器上访问到http://localhost:80/),经过你服务器的nginx处理,映射到http://localhost:8089/
        listen       80;
        server_name  localhost;
		#springboot内置的tomcat运行的jar包项目
        location / {
                proxy_pass    http://localhost:8089/;
         }
		#部署到tomcat的war包项目
         location /mjcd/ {
           proxy_pass    http://localhost:8086/;
        }
}

80,8089,8086端口要提前在阿里云控制台的安全组开启,tomcat和jar运行时,分别按照对应端口运行,tomcat原来的80端口改为8086

配置nginx.conf,支持443端口(https)

#/usr/local/nginx/conf/nginx.conf
#地址栏输入--https:你的域名/mjcd/(服务器上访问到http://localhost:443/mjcd/),经过你服务器的nginx处理,映射到https://localhost:8086/mjcd/
server {
        listen      443 ssl;
        server_name localhost;
        ssl on;
        ssl_certificate      /usr/local/nginx/XXX/XXXX.cn.pem;
        ssl_certificate_key  /usr/local/nginx/XXX/XXXX.cn.key;
        ssl_session_timeout  5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
        ssl_prefer_server_ciphers on;
        #springboot内置的tomcat运行的jar包项目
        location / {
           proxy_pass    http://localhost:8089/;
        }
        #部署到tomcat的war包项目
         location /mjcd/ {
           proxy_pass    https://localhost:8087/;
        }
}

443,8087端口要提前在阿里云控制台的安全组开启,tomcat和jar运行时,分别按照对应端口运行,tomcat原来的443端口,改为8087

3、运行jar

在阿里云控制台,配置8089端口,图片中的80/80改为8089/8089

安全组


编写脚本文件,运行jar

stop.sh用于kill,jar运行的进程

#!/bin/bash
PID=$(ps -ef | grep qcl80.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Application is already stopped
else
    echo kill $PID
    kill $PID
fi

start.sh 80端口运行项目,qcl80.jar为包名

#!/bin/bash
nohup java -jar qcl80.jar --server.port=80 &

run.sh 将stop和start结合

#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh

参考

4、运行tomcat

tomcat下载和安装。网上教程多,在此不表

将war包命名为ROOT.war,直接放在tomcat的webapps文件夹里,省去配置xml的功夫(默认你只有一个war包项目,多个项目老老实实去配置xml里的项目路径)

  • 作者:seibert(联系作者)
  • 发表时间:2020-06
  • 版权声明:未经站长允许,不可转载
  • 公众号转载:请在文末添加作者公众号二维码
  • 评论