bash
log_format webapp_log '[$time_local] $remote_addr - from $server_addr:$server_port to $upstream_addr - $remote_user \n'
'"$request" $status $body_bytes_sent '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream webapp {
server 10.190.27.110 backup;
server 10.190.27.111 backup;
server 127.0.0.1 down;
}
server{
listen 8081;
listen 10.27.190.109:8081;
listen [::]:8081;
server_name localhost;
location / {
proxy_pass http://webapp;
}
}
server {
listen 8001;
listen 10.27.190.109:8001;
listen [::]:8001;
server_name localhost;
#log data format
if ($time_iso8601 ~ '(\d{4})-(\d{2})-(\d{2})') {
set $year $1;
set $month $2;
set $day $3;
}
#É趨±¾ÐéÄâÖ÷>>úµÄ·ÃÎÊÈÕÖ¾
access_log /var/log/nginx/webapp/access_$year$month$day.log webapp_log;
error_log /var/log/nginx/webapp/error.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://webapp;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
jenkins 通过nginx 热部署 应用
1.Git-分支 参数化
2.编译环境 参数化
3.大版本 参数化
git地址配置,并使用 分支参数
创建SSH-Server步骤
第一个应用:
bash
分2步处理,也可直接 脚本
1.创建目录 按日期建 BUILD_YM是 build-timestamper 全局变量
if [ ! -d "~/srv/webapp/PublishHistory/${BUILD_YM}" ]; then
mkdir -p ~/srv/webapp/PublishHistory/${BUILD_YM}
fi
if [ ! -d "/srv/webapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}" ]; then
mkdir -p /srv/webapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}
fi
if [ ! -d "/srv/webapp/backup" ]; then
mkdir -p /srv/webapp/backup
fi
if [ ! -d "/srv/webapp/api/publish" ]; then
mkdir -p /srv/webapp/api/publish
fi
2.ssh 传输文件完毕后 备份旧代码、更新代码(一定要是 服务否则 ssh停止后所有相关进程都会被停止)
mv ~/srv/webapp/PublishHistory/${BUILD_YM}/webapp.WebApi.zip /srv/webapp/PublishHistory/${BUILD_YM}/webapp.WebApi.${VersionMajor}.${BUILD_VERSION}.zip
zip -qr /srv/webapp/backup/webappaip.${VersionMajor}.${BUILD_VERSION}.${BUILD_YM}.bak.zip /srv/webapp/api/publish -x "/srv/webapp/api/publish/Logs/*"
unzip /srv/webapp/PublishHistory/${BUILD_YM}/webapp.WebApi.${VersionMajor}.${BUILD_VERSION}.zip -d /srv/webapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}
systemctl stop webapp.api
rm -rf /srv/webapp/api/publish/wwwroot
mv -bf /srv/webapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}/* /srv/webapp/api/publish
chmod u+rwx /srv/webapp/api/publish/webapp.WebApi
rmdir /srv/webapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}
rm -rf /srv/webapp/api/publish/*~
systemctl start webapp.api
第二个应用(正式应用)
先停止 应用,让 nginx 使用 backup 的webapi
bash
looptimes=3
result_code=0
ok_status=0
for((i=0;i<=looptimes;i++))
do
result_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} 10.190.27.111`
if [ $result_code -eq 200 ]; then
echo "webapp backup api deploment ok"
sed -i "10c server 127.0.0.1 down;" /etc/nginx/conf.d/webapp.conf
nginx -s reload
ok_status=1
break
else
sleep 3s
fi
done
if [$ok_status -eq 0]; then
echo "webapp api is not alive(wait 4 $[3*looptimes]s times already down)"
fi
if [ ! -d "~/srv/webapp/PublishHistory/${BUILD_YM}" ]; then
mkdir -p ~/srv/webapp/PublishHistory/${BUILD_YM}
fi
if [ ! -d "/srv/webapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}" ]; then
mkdir -p /srv/webapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}
fi
if [ ! -d "/srv/webapp/backup" ]; then
mkdir -p /srv/webapp/backup
fi
if [ ! -d "/srv/webapp/api/publish" ]; then
mkdir -p /srv/webapp/api/publish
fi
bash
mv ~/srv/webiapp/PublishHistory/${BUILD_YM}/webiapp.WebApi.zip /srv/webiapp/PublishHistory/${BUILD_YM}/webiapp.WebApi.${VersionMajor}.${BUILD_VERSION}.zip
zip -qr /srv/webiapp/backup/webiappaip.${VersionMajor}.${BUILD_VERSION}.${BUILD_YM}.bak.zip /srv/webiapp/api/publish -x "/srv/webiapp/api/publish/Logs/*"
unzip /srv/webiapp/PublishHistory/${BUILD_YM}/webiapp.WebApi.${VersionMajor}.${BUILD_VERSION}.zip -d /srv/webiapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}
systemctl stop webiapp.api
rm -rf /srv/webiapp/api/publish/wwwroot
mv -bf /srv/webiapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}/* /srv/webiapp/api/publish
chmod u+rwx /srv/webiapp/api/publish/webiapp.WebApi
rmdir /srv/webiapp/PublishHistory/${BUILD_YM}/${VersionMajor}.${BUILD_VERSION}
rm -rf /srv/webiapp/api/publish/*~
systemctl start webiapp.api
looptimes=3
result_code=0
ok_status=0
for((i=0;i<=looptimes;i++))
do
result_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} localhost`
if [ $result_code -eq 200 ]; then
echo "webiapp api deploment ok"
sed -i "10c server 127.0.0.1;" /etc/nginx/conf.d/webiapp.conf
nginx -s reload
ok_status=1
break
else
sleep 3s
fi
done
if [$ok_status -eq 0]; then
echo "webiapp api is not alive(wait 4 $[3*looptimes]s and already down)"
fi