2017-06-13 47 views
0

我想用條件access_logging配置nginx/1.13.0。Nginx:登錄多個條件

如果access_logging是隻在$狀態代碼的條件,一切工作正常:

http { 
    [..] 
    map $status $logworthy_status { 
     ~^[4] 1; 
     default 0; 
    } 
    [..] 
    server { 
      [..] 
      access_log /var/log/nginx_access.log combined if=$logworthy_status; 
      [..] 
    } 
} 

的調試日誌顯示在地圖上表現爲預期:

2017/06/13 11:34:14 [debug] 23153#0: *203 http map started 
2017/06/13 11:34:14 [debug] 23153#0: *203 http script var: "401" 
2017/06/13 11:34:14 [debug] 23153#0: *203 http map: "401" "1" 

但是,如果我嘗試重寫這允許建議多個條件here

http { 
    [..] 
    map $status $logworthy_status { 
     ~^[4] 1; 
     default 0; 
    } 
    [..] 
    server { 
      [..] 
      set $logworthy 0; 
      if ($logworthy_status = 1) { 
       set $logworthy 1; 
      } 
      access_log /var/log/nginx_access.log combined if=$logworthy; 
      [..] 
    } 
} 

不會生成日誌消息。檢查調試日誌顯示,甚至$狀態映射似乎搞砸:

2017/06/13 11:38:12 [debug] 23631#0: *204 rewrite phase: 0 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script value: "0" 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script set $logworthy 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var 
2017/06/13 11:38:12 [debug] 23631#0: *204 http map started 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var: "000" 
2017/06/13 11:38:12 [debug] 23631#0: *204 http map: "000" "0" 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script var: "0" 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script value: "1" 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script equal 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script equal: no 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script if 
2017/06/13 11:38:12 [debug] 23631#0: *204 http script if: false 

有人可以解釋這一點嗎?請求按預期得到處理,nginx返回401,但它不記錄這個。

回答

0

您可以使用多個map指令,並使用前一個塊的默認值。例如,用戶代理不包含「AppleWebKit」的日誌4xx狀態請求

map $status $loggable_status { 
    ~^[4] 1; 
    default 0; 
} 
map $http_user_agent $loggable_user_agent { 
    ~*AppleWebKit 0; 
    default $loggable_status; 
} 
access_log /var/log/nginx/error.log combined if=$loggable_user_agent;