From 337a6ace919709046dc5283c221b173ccef55b96 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Sun, 11 Oct 2020 02:10:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0openresty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/nginx.conf | 15 ++++++++++----- shell/checkPort | 4 ++-- shell/findPortPid | 5 +++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/conf/nginx.conf b/conf/nginx.conf index f374719..1b17a2b 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -6,15 +6,20 @@ events { http { server { listen 8080; +set $target ''; location / { access_by_lua ' + local t = io.popen("/app/checkPort") + local a = t:read("*all") - local t = io.popen("checkPort") - local a = t:read("*all") - - ngx.say("

当前进程"..a.."

") - + if( string.gsub(a,"%D","") == "0" ) + then + ngx.var.target="http://127.0.0.1:5000" + else + ngx.var.target="http://127.0.0.1:2233" + end '; +proxy_pass $target; } } } \ No newline at end of file diff --git a/shell/checkPort b/shell/checkPort index abe9e22..05acad9 100644 --- a/shell/checkPort +++ b/shell/checkPort @@ -1,8 +1,8 @@ #!/bin/sh # 检查客户端和服务端端口是否存在 #存在返回0否则返回1 -source riskenv -if [[ `findPortPid $PORT|wc -l` -eq 1 && `findPortPid $serverPort|wc -l` -eq 1 ]] +source /app/riskenv +if [[ `/app/findPortPid $PORT|wc -l` -eq 1 && `/app/findPortPid $serverPort|wc -l` -eq 1 ]] then echo 0 else diff --git a/shell/findPortPid b/shell/findPortPid index a457137..4a40edd 100644 --- a/shell/findPortPid +++ b/shell/findPortPid @@ -1,6 +1,7 @@ #!/bin/sh # 查找端口pid -if [[ $# == 1 && `netstat -antp|grep $1|wc -l` -eq 1 ]] +port=`netstat -antpl|grep $1|awk '{printf $7}'|cut -d/ -f1` +if [[ $# == 1 && `echo $port|wc -l` -eq 1 ]] then - netstat -anp|grep $1|awk '{printf $7}'|cut -d/ -f1 + echo $port fi