Skip to content

Navigation Menu

Sign in
Sign up

Commit d852118

Browse files
nginx 错误提示更新;日志压缩使用 xz 命令;安装脚本小调整
2 parents 8a3fb1d + a73cb9d commit d852118

5 files changed

Lines changed: 54 additions & 28 deletions

File tree

‎api.conf‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
if ($_origin_id = '') {
2-
return 403 'ERROR: origin `$http_origin` is not allowed';
3-
}
4-
if ($http_x_jsproxy) {
5-
return 500 'ERROR: circular dependency';
6-
}
7-
proxy_set_header x-jsproxy 1;
8-
proxy_set_header Connection $http_connection;
9-
101
set $_level '';
112
set $_switched '';
123
set $_url '';
@@ -16,6 +7,20 @@ set $_type '';
167
set $_mode '';
178
set $_bodyhash '';
189

10+
error_page 500 502 504 /error;
11+
12+
location = /error {
13+
internal;
14+
access_log off;
15+
more_set_headers
16+
'access-control-allow-origin: *'
17+
'access-control-expose-headers: gateway-err--'
18+
'gateway-err--: {"msg": "$arg_msg", "addr": "$upstream_addr"}'
19+
;
20+
return 200;
21+
}
22+
23+
1924
location = /preflight {
2025
internal;
2126
access_log off;
@@ -31,8 +36,19 @@ location = /preflight {
3136

3237
# HTTP(S) Proxy
3338
location = /http {
39+
# see ./allowed-sites.conf
40+
if ($_origin_id = '') {
41+
rewrite ^ /error?msg=ORIGIN_NOT_ALLOWED;
42+
}
43+
if ($http_x_jsproxy) {
44+
rewrite ^ /error?msg=CIRCULAR_DEPENDENCY;
45+
}
46+
proxy_set_header x-jsproxy 1;
47+
proxy_set_header Connection $http_connection;
48+
49+
3450
if ($http_access_control_request_headers) {
35-
rewrite ^ /preflight;
51+
rewrite ^ /preflight;
3652
}
3753

3854
access_by_lua_file ../lua/http-dec-req-hdr.lua;

‎i.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ gen_cert() {
5757
ip=$(curl -s $i)
5858

5959
if [[ ! $ip ]]; then
60-
warn "获取失败"
60+
warn "获取失败"
6161
continue
6262
fi
6363

@@ -179,7 +179,7 @@ install() {
179179
fi
180180

181181
if [ -d server ]; then
182-
backup="$INSTALL_DIR/bak/$(date +%Y_%m_%d_%H_%M_%S)"
182+
localbackup="$INSTALL_DIR/bak/$(date +%Y_%m_%d_%H_%M_%S)"
183183
warn "当前 server 目录备份到 $backup"
184184
mkdir -p $backup
185185
mv server $backup

‎log-svc/backup.sh‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
SVC_DIR=/home/jsproxy/server
55
LOG_DIR=$SVC_DIR/nginx/logs
6+
DST_DIR=$SVC_DIR/log-svc/backup
67

78
LOG_FILE=$LOG_DIR/proxy.log
8-
LOG_SIZE=$(( 32 * 1024 * 1024 ))
9+
LOG_SIZE=$(( 256 * 1024 * 1024 ))
910

1011
ERR_FILE=$LOG_DIR/error.log
11-
ERR_SIZE=$(( 1*1024 * 1024 * 1024 ))
12+
ERR_SIZE=$(( 256 * 1024 * 1024 ))
1213

1314

1415
# error.log 达到 ERR_SIZE,开始备份(目前只清理)
@@ -24,24 +25,21 @@ if (( $logsize < $LOG_SIZE )); then
2425
fi
2526

2627
logtime=$(date "+%Y-%m-%d-%H-%M-%S")
27-
logfile=$SVC_DIR/log-svc/backup/$logtime.log
2828

2929
#
3030
# 先移走日志文件,然后创建新的日志文件,通知 nginx 重新打开
3131
#
32-
mv $LOG_FILE $logfile
32+
mv $LOG_FILE $DST_DIR/$logtime.log
3333
touch $LOG_FILE
3434
$SVC_DIR/run.sh reopen
3535
sleep 1
3636

3737
#
3838
# 日志压缩
3939
# 根据实际情况调整策略,在不影响系统的前提下,充分利用剩余 CPU
40-
# 可尝试其他工具(例如 7z),在开销和效果之间找一个平衡点
4140
#
42-
echo "compress $logtime ($logsize bytes)"
41+
echo "compress ..."
4342

44-
nice -n 19 \
45-
gzip $logfile
43+
nice -n 19 xz $DST_DIR/*.log
4644

4745
echo "done"

‎lua/http-enc-res-hdr.lua‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,27 @@ end
2323

2424
local function flushHdr()
2525
if detail then
26-
expose = expose .. ',--s'
26+
if status ~= 200 then
27+
expose = expose .. ',--s'
28+
end
2729
-- 该字段不在 aceh 中,如果浏览器能读取到,说明支持 * 通配
2830
ngx.header['--t'] = '1'
2931
end
3032

31-
local status = ngx.status
32-
3333
ngx.header['access-control-expose-headers'] = expose
3434
ngx.header['access-control-allow-origin'] = '*'
3535
ngx.header['vary'] = vary
36-
ngx.header['--s'] = status
36+
37+
local status = ngx.status
38+
39+
-- 前端优先使用该字段作为状态码
40+
if status ~= 200 then
41+
ngx.header['--s'] = status
42+
end
43+
44+
-- 保留原始状态码,便于控制台调试
45+
-- 例如 404 显示红色,如果统一设置成 200 则没有颜色区分
46+
-- 需要转义 30X 重定向,否则不符合 cors 标准
3747
if
3848
status == 301 or
3949
status == 302 or
@@ -112,9 +122,9 @@ local function nodeSwitched()
112122
end
113123

114124
-- 节点切换功能,目前还在测试中(demo 中已开启)
115-
--if nodeSwitched() then
116-
-- return
117-
--end
125+
if nodeSwitched() then
126+
return
127+
end
118128

119129

120130
local h, err = ngx.resp.get_headers()

‎nginx.conf‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ http {
4545
proxy_buffer_size 16k;
4646
proxy_buffers 4 32k;
4747
proxy_busy_buffers_size 64k;
48-
proxy_send_timeout 10s;
48+
proxy_send_timeout 30s;
49+
proxy_read_timeout 30s;
50+
proxy_connect_timeout 10s;
4951

5052
lua_load_resty_core off;
5153

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /