forked from EtherDream/jsproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bed122
commit 3c15436
Showing
17 changed files
with
378 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
nginx 日志备份服务 | ||
|
||
## 说明 | ||
|
||
nginx 长时间运行会导致日志文件过大,该服务定期备份日志到 `backup` 目录,并进行压缩。 | ||
|
||
|
||
## 依赖 | ||
|
||
用到了 `brotli` 压缩工具,执行 `setup-brotli.sh` 安装。 | ||
|
||
最终安装在 `/home/jsproxy/tools/brotli`。 | ||
|
||
|
||
## 启动 | ||
|
||
```bash | ||
./svc.sh & | ||
``` | ||
|
||
使用 `jsproxy` 用户运行,无需 `root`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env bash | ||
# 功能:备份 nginx 日志到 backup 目录 | ||
|
||
SVC_DIR=/home/jsproxy/server | ||
LOG_DIR=$SVC_DIR/nginx/logs | ||
|
||
LOG_FILE=$LOG_DIR/proxy.log | ||
LOG_SIZE=$(( 32 * 1024 * 1024 )) | ||
|
||
ERR_FILE=$LOG_DIR/error.log | ||
ERR_SIZE=$(( 1 * 1024 * 1024 * 1024 )) | ||
|
||
|
||
# error.log 达到 ERR_SIZE,开始备份(目前只清理) | ||
errsize=$(stat --printf=%s $ERR_FILE) | ||
if (( $errsize >= $ERR_SIZE )); then | ||
echo > $ERR_FILE | ||
fi | ||
|
||
# proxy.log 达到 LOG_SIZE,开始备份 | ||
logsize=$(stat --printf=%s $LOG_FILE) | ||
if (( $logsize < $LOG_SIZE )); then | ||
exit | ||
fi | ||
|
||
logtime=$(date "+%Y-%m-%d-%H-%M-%S") | ||
logfile=$SVC_DIR/log-svc/backup/$logtime.log | ||
|
||
# | ||
# 先移走日志文件,然后创建新的日志文件,通知 nginx 重新打开 | ||
# https://www.nginx.com/resources/wiki/start/topics/examples/logrotation/ | ||
# | ||
mv $LOG_FILE $logfile | ||
touch $LOG_FILE | ||
kill -USR1 $(< $LOG_DIR/nginx.pid) | ||
sleep 1 | ||
|
||
# | ||
# 日志压缩 | ||
# 根据实际情况调整策略,在不影响系统的前提下,充分利用剩余 CPU | ||
# 可尝试其他工具(例如 7z),在开销和效果之间找一个平衡点 | ||
# | ||
echo "compress $logtime ($logsize bytes)" | ||
|
||
if (( $logsize > 100 * 1024 * 1024 )); then | ||
# 日志较大,使用快速压缩 | ||
nice -n 19 \ | ||
gzip $logfile | ||
else | ||
# 日志不大,使用高强度压缩 | ||
nice -n 19 \ | ||
~/tools/brotli $logfile --rm | ||
fi | ||
|
||
echo "done" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
该目录存放临时备份的日志。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env bash | ||
# 功能:安装 brotli 压缩工具 | ||
# 依赖:cmake(yum install -y cmake) | ||
|
||
git clone --depth 1 https://github.com/google/brotli.git | ||
cd brotli | ||
|
||
./configure-cmake | ||
make | ||
|
||
mkdir -p ~/tools | ||
mv brotli ~/tools | ||
|
||
cd .. | ||
rm -rf brotli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env bash | ||
# 功能:定时调用 backup.sh | ||
|
||
echo "log svc running" | ||
|
||
# 也可用 crontab | ||
while true | ||
do | ||
./backup.sh | ||
sleep 60 | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
local _M = {} | ||
local traff = ngx.shared.traff | ||
|
||
local nReq = 0 | ||
|
||
|
||
function _M.inc() | ||
nReq = nReq + 1 | ||
end | ||
|
||
function _M.syn() | ||
traff:incr('nReq', nReq) | ||
nReq = 0 | ||
end | ||
|
||
function _M.update() | ||
local nReq = traff:get('nReq') | ||
traff:set('nReq', 0) | ||
return nReq | ||
end | ||
|
||
function _M.getStat() | ||
return traff:get('stat') | ||
end | ||
|
||
function _M.setStat(stat) | ||
return traff:set('stat', stat) | ||
end | ||
|
||
function _M.reset() | ||
if traff:get('nReq') == nil then | ||
traff:add('nReq', 0) | ||
traff:add('stat', '') | ||
end | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
-- 功能:还原 HTTP 请求头 | ||
-- 阶段:access_by_lua | ||
|
||
local hdrs, err = ngx.req.get_headers() | ||
local extHdrs | ||
|
||
for k, v in pairs(hdrs) do | ||
if k:sub(1, 2) ~= '--' then | ||
goto continue | ||
end | ||
|
||
ngx.req.clear_header(k) | ||
k = k:sub(3) | ||
|
||
if k == 'url' then | ||
ngx.var._url = v | ||
elseif k == 'ver' then | ||
ngx.var._ver = v | ||
elseif k == 'aceh' then | ||
ngx.ctx._aceh = 1 | ||
elseif k == 'ext' then | ||
extHdrs = require('cjson').decode(v) | ||
else | ||
ngx.req.set_header(k, v) | ||
end | ||
|
||
::continue:: | ||
end | ||
|
||
if extHdrs then | ||
for k, v in pairs(extHdrs) do | ||
ngx.req.set_header(k, v) | ||
end | ||
end |
Oops, something went wrong.