先安装nginx

然后配置nginx.conf文件,这里我总结了三种比较简单的方法
第一种:一个server,配置多个location


server {
listen 80 ;
server_name localhost;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /a/ {

proxy_pass http://localhost:8080/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
location /b/ {

proxy_pass http://localhost:8070/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-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;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

这里的a和b是你的项目名,也就是你的context-path

第二种:
多个server,同一端口,根据域名server_name来匹配

server {
listen 80 ;
server_name www.bbb.com;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /{

proxy_pass http://localhost:8080/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-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;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80 ;
server_name www.aaa.com;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /{

proxy_pass http://localhost:8080/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-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;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}


第三种,多个server,根据监听不同的端口来匹配

server {
listen 80 ;
server_name www.bbb.com;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /{

proxy_pass http://localhost:8080/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-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;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 81 ;
server_name www.aaa.com;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /{

proxy_pass http://localhost:8080/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-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;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

由于我的项目是jar包发布,所以最后我用了一个shell脚本去批量启动jar包。

 


#! /bin/sh
# 端口号
PORTS=(8080 8070)
# 系统模块
MODULES=(bterminal wechat)
# 系统模块名称
MODULE_NAMES=(b端程序 微信小程序)
# jar包数组
JARS=(printshopweb-0.0.1-SNAPSHOT.jar wechatprinting-0.0.1-SNAPSHOT.jar)
# jar包路径
JAR_PATH='/root'
# 日志路径
LOG_PATH='/root'
start() {
local MODULE=
local MODULE_NAME=
local JAR_NAME=
local command="$1"
local commandOk=0
local count=0
local okCount=0
local port=0
for((i=0;i<${#MODULES[@]};i++))
do
MODULE=${MODULES[$i]}
MODULE_NAME=${MODULE_NAMES[$i]}
JAR_NAME=${JARS[$i]}
PORT=${PORTS[$i]}
if [ "$command" == "all" ] || [ "$command" == "$MODULE" ];then
commandOk=1
count=0
PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ];then
echo "$MODULE---$MODULE_NAME:已经运行,PID=$PID"
else
exec nohup java -jar $JAR_PATH/$JAR_NAME >> $LOG_PATH/nohup.out &
PID=`netstat -apn | grep $PORT | awk '{print $7}' | cut -d/ -f 1`
while [ -z "$PID" ]
do
if (($count == 30));then
echo "$MODULE---$MODULE_NAME:$(expr $count \* 10)秒内未启动,请检查!"
break
fi
count=$(($count+1))
echo "$MODULE_NAME启动中.................."
sleep 10s
PID=`netstat -apn | grep $PORT | awk '{print $7}' | cut -d/ -f 1`
done
okCount=$(($okCount+1))
echo "$MODULE---$MODULE_NAME:已经启动成功,PID=$PID"
fi
fi
done
if(($commandOk == 0));then
echo "第二个参数请输入:baozhi|wechat"
else
echo "............本次共启动:$okCount个服务..........."
fi
}

stop() {
local MODULE=
local MODULE_NAME=
local JAR_NAME=
local command="$1"
local commandOk=0
local okCount=0
for((i=0;i<${#MODULES[@]};i++))
do
MODULE=${MODULES[$i]}
MODULE_NAME=${MODULE_NAMES[$i]}
JAR_NAME=${JARS[$i]}
if [ "$command" = "all" ] || [ "$command" = "$MODULE" ];then
commandOk=1
PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ];then
echo "$MODULE---$MODULE_NAME:准备结束,PID=$PID"
kill -9 $PID
PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
while [ -n "$PID" ]
do
sleep 3s
PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
done
echo "$MODULE---$MODULE_NAME:成功结束"
okCount=$(($okCount+1))
else
echo "$MODULE---$MODULE_NAME:未运行"
fi
fi
done
if (($commandOk == 0));then
echo "第二个参数请输入:baozhi|wechat"
else
echo "............本次共停止:$okCount个服务............"
fi
}


case "$1" in
start)
start "$2"
;;
stop)
stop "$2"
;;
restart)
stop "$2"
sleep 3s
start "$2"
;;
*)
echo "第一个参数请输入:start|stop|restart"
exit 1
;;
esac