반응형

nginx의 access log가 두 번이나 씌여지는 현상을 발견하여 포스팅 해본다.

location = '/getid'
{
     if ($scheme !~* "https") {
            return 308 https://$host$request_uri;
     } 
    access_log /home1/irteam/dmp_log_collector/nginx/logs/dmp_applog.${hostname} appmlog;
}

위와 같이 request의 scheme이 'http'가 아닌 경우 'https'로 리다이렉트 하도록 되어져 있다.

따라서 http로 요청을 하였을 때는 당연히 acces_log를 안찍을 거라고 생각을 했는데....아니였다.....

nginx는 기본적으로 요청이 들어왔을 때 현재의 스코프내에서 정의되어져 있는 access_log가 있다면 해당 경로에 access log를 남기고 그렇지 않다면 그 상위 스코프에 있는 access_log에 접근해 access log를 남긴다는 것이다. 자바스크립트의 hoisting(호이스팅)과 비슷한 느낌이다.

따라서 'http'요청일 경우 access log를 남기고 싶지 않다면 if 블럭에 'access_log off'를 주어 access log를 남기지 않도록 한다.

location = '/getid' 

     if ($scheme !~* "https") { 
            access_log off;
            return 308 https://$host$request_uri; 
     }
     access_log /home1/irteam/dmp_log_collector/nginx/logs/dmp_applog.${hostname} appmlog;
}

 

자세한 내용은 nginx의 documentation을 참고하자.

Similar to the  error_log directive, the access_log directive defined on a particular configuration level overrides the settings from the previous levels. When processing of a request is completed, the message is written to the log that is configured on the current level, or inherited from the previous levels. If one level defines multiple access logs, the message is written to all of them.

https://docs.nginx.com/nginx/admin-guide/monitoring/logging/

 

NGINX Docs | Configuring Logging

Capture detailed information about errors and request processing in log files, either locally or via syslog.

docs.nginx.com

 

반응형

+ Recent posts