浏览代码

nginx 错误提示更新;日志压缩使用 xz 命令;安装脚本小调整

zjcqoo 6 年之前
父节点
当前提交
d852118158
共有 5 个文件被更改,包括 54 次插入28 次删除
  1. 26 10
      api.conf
  2. 2 2
      i.sh
  3. 6 8
      log-svc/backup.sh
  4. 17 7
      lua/http-enc-res-hdr.lua
  5. 3 1
      nginx.conf

+ 26 - 10
api.conf

@@ -1,12 +1,3 @@
-if ($_origin_id = '') {
-  return              403   'ERROR: origin `$http_origin` is not allowed';
-}
-if ($http_x_jsproxy) {
-  return              500   'ERROR: circular dependency';
-}
-proxy_set_header      x-jsproxy   1;
-proxy_set_header      Connection  $http_connection;
-
 set                   $_level     '';
 set                   $_switched  '';
 set                   $_url       '';
@@ -16,6 +7,20 @@ set                   $_type      '';
 set                   $_mode      '';
 set                   $_bodyhash  '';
 
+error_page            500 502 504 /error;
+
+location = /error {
+  internal;
+  access_log          off;
+  more_set_headers
+    'access-control-allow-origin: *'
+    'access-control-expose-headers: gateway-err--'
+    'gateway-err--: {"msg": "$arg_msg", "addr": "$upstream_addr"}'
+  ;
+  return              200;
+}
+
+
 location = /preflight {
   internal;
   access_log          off;
@@ -31,8 +36,19 @@ location = /preflight {
 
 # HTTP(S) Proxy
 location = /http {
+  # see ./allowed-sites.conf
+  if ($_origin_id = '') {
+    rewrite             ^   /error?msg=ORIGIN_NOT_ALLOWED;
+  }
+  if ($http_x_jsproxy) {
+    rewrite             ^   /error?msg=CIRCULAR_DEPENDENCY;
+  }
+  proxy_set_header      x-jsproxy   1;
+  proxy_set_header      Connection  $http_connection;
+  
+
   if ($http_access_control_request_headers) {
-    rewrite           ^   /preflight;
+    rewrite             ^   /preflight;
   }
 
   access_by_lua_file    ../lua/http-dec-req-hdr.lua;

+ 2 - 2
i.sh

@@ -57,7 +57,7 @@ gen_cert() {
     ip=$(curl -s $i)
 
     if [[ ! $ip ]]; then
-      warn "获取失败"
+      warn "获取失败"
       continue
     fi
 
@@ -179,7 +179,7 @@ install() {
   fi
 
   if [ -d server ]; then
-    backup="$INSTALL_DIR/bak/$(date +%Y_%m_%d_%H_%M_%S)"
+    local backup="$INSTALL_DIR/bak/$(date +%Y_%m_%d_%H_%M_%S)"
     warn "当前 server 目录备份到 $backup"
     mkdir -p $backup
     mv server $backup

+ 6 - 8
log-svc/backup.sh

@@ -3,12 +3,13 @@
 
 SVC_DIR=/home/jsproxy/server
 LOG_DIR=$SVC_DIR/nginx/logs
+DST_DIR=$SVC_DIR/log-svc/backup
 
 LOG_FILE=$LOG_DIR/proxy.log
-LOG_SIZE=$(( 32 * 1024 * 1024 ))
+LOG_SIZE=$(( 256 * 1024 * 1024 ))
 
 ERR_FILE=$LOG_DIR/error.log
-ERR_SIZE=$(( 1 * 1024 * 1024 * 1024 ))
+ERR_SIZE=$(( 256 * 1024 * 1024 ))
 
 
 # error.log 达到 ERR_SIZE,开始备份(目前只清理)
@@ -24,12 +25,11 @@ if (( $logsize < $LOG_SIZE )); then
 fi
 
 logtime=$(date "+%Y-%m-%d-%H-%M-%S")
-logfile=$SVC_DIR/log-svc/backup/$logtime.log
 
 #
 # 先移走日志文件,然后创建新的日志文件,通知 nginx 重新打开
 #
-mv $LOG_FILE $logfile
+mv $LOG_FILE $DST_DIR/$logtime.log
 touch $LOG_FILE
 $SVC_DIR/run.sh reopen
 sleep 1
@@ -37,11 +37,9 @@ sleep 1
 #
 # 日志压缩
 # 根据实际情况调整策略,在不影响系统的前提下,充分利用剩余 CPU
-# 可尝试其他工具(例如 7z),在开销和效果之间找一个平衡点
 #
-echo "compress $logtime ($logsize bytes)"
+echo "compress ..."
 
-nice -n 19 \
-  gzip $logfile
+nice -n 19 xz $DST_DIR/*.log
 
 echo "done"

+ 17 - 7
lua/http-enc-res-hdr.lua

@@ -23,17 +23,27 @@ end
 
 local function flushHdr()
   if detail then
-    expose = expose .. ',--s'
+    if status ~= 200 then
+      expose = expose .. ',--s'
+    end
     -- 该字段不在 aceh 中,如果浏览器能读取到,说明支持 * 通配
     ngx.header['--t'] = '1'
   end
 
-  local status = ngx.status
-
   ngx.header['access-control-expose-headers'] = expose
   ngx.header['access-control-allow-origin'] = '*'
   ngx.header['vary'] = vary
-  ngx.header['--s'] = status
+
+  local status = ngx.status
+
+  -- 前端优先使用该字段作为状态码
+  if status ~= 200 then
+    ngx.header['--s'] = status
+  end
+
+  -- 保留原始状态码,便于控制台调试
+  -- 例如 404 显示红色,如果统一设置成 200 则没有颜色区分
+  -- 需要转义 30X 重定向,否则不符合 cors 标准
   if
     status == 301 or
     status == 302 or
@@ -112,9 +122,9 @@ local function nodeSwitched()
 end
 
 -- 节点切换功能,目前还在测试中(demo 中已开启)
--- if nodeSwitched() then
---   return
--- end
+if nodeSwitched() then
+  return
+end
 
 
 local h, err = ngx.resp.get_headers()

+ 3 - 1
nginx.conf

@@ -45,7 +45,9 @@ http {
   proxy_buffer_size       16k;
   proxy_buffers           4 32k;
   proxy_busy_buffers_size 64k;
-  proxy_send_timeout      10s;
+  proxy_send_timeout      30s;
+  proxy_read_timeout      30s;
+  proxy_connect_timeout   10s;
 
   lua_load_resty_core     off;