當拒絕位置區塊中的所有內容時,NGINX 日誌記錄不起作用

當拒絕位置區塊中的所有內容時,NGINX 日誌記錄不起作用

我覺得這種行為很奇怪,所以我要在這裡問。

取這個位置塊;

location ~ /(wp-config.php|readme.html|licence.txt|license.txt|readme.txt) {
    access_log /var/log/nginx/blocked.log blocked;
    deny all;
}

它不會將任何內容記錄到日誌檔案中,但是如果我刪除否認一切; - 確實如此。同樣適用於返回 403ETC

我想記錄被阻止的請求。

nginx version: nginx/1.8.0

更新

造成這種情況的原因似乎是您定義自己的錯誤頁面時。例如:

http {  ..
    error_page          403          /error/403.html;
    error_page          404          /error/404.html;
.. }

刪除這些行允許記錄

答案1

需要檢查的事項:

  1. 你定義了嗎blocked
  2. 或者日誌檔案的路徑是否正確?
  3. 檢查location順序,因為wp-config.php最有可能匹配location ~ \.php$或相似

它適用於我的nginx: 1.7.11.1Gryphon,儘管它是 Windows 環境。

這是我的設定:

http {
    ...
    log_format robots   '$remote_addr - $remote_user [$time_local] '
                        '$host "$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';
    ...
    server {

    ...
        location ~/(robots.txt|readme.html) {
            access_log  logs/nginx-access-robots.log robots;
            deny all;
        }
    ...
    }

}

相關內容