golang: 用nginx部署go应用
一,go应用的运行
我们没有在代码中设置daemon方式,
这里用nohup运行应用:
#!/bin/bash
export BUILD_ID=dontKillme
whoami
WORKSPACE=/data/gy
BIN_NAME=goappbinname
PID=`ps -ef | grep ${BIN_NAME} | grep -v grep | grep -v startup | awk '{print \$2}'`
echo $PID;
if [ ! "$PID" ] ;then
echo "进程不存在"
else
echo "进程存在,杀死进程PID$PID"
kill -9 $PID
fi
#sleep
sleep 1s
chmod 777 ${WORKSPACE}/${BIN_NAME}
echo "服务启动开始"
cd ${WORKSPACE}
pwd
nohup ${WORKSPACE}/${BIN_NAME} >> ${WORKSPACE}/logs/console.log 2>&1 &
pwd
二,nginx的配置:
server {
listen 80;
server_name web.yourdomain.net;
location / {
proxy_pass http://localhost:3000;
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_set_header X-Forwarded-Proto $scheme;
}
access_log /logs/nginxlogs/web.access_log;
error_log /logs/nginxlogs/web.error_log;
}
说明:3000是我们的go应用所守护的端口