개인적으로 프로그램을 디버깅 할 때 로그보다는 break point 를 잡아서 확인 하는 것을 선호한다. 그래서 개발환경을 구축할 때 어떻게든 break point 로 디버깅 할 수 있는 환경을 구축하려고 노력한다. 그러나 이미 다른 사람들이 발견한 버그 특히 사용자가 버그에 대해서 잘 설명하지 못하는 경우에서는 꼭 로그 분석이 필요하다. 이 경우에서는 특정 값에 대해서 어떤 동작으로 반응한느지에 대한 문제인 경우가 많아서 어떠한 상태였고, 어떠한 상태로 데이터를 넣었는지 확인이 필요하다.

 Nginx 에서 로그 포멧에 대한 환경설정은 /etc/nginx/nginx.conf 내에서 세팅한다. 그리고 로그를 생성하는 코드는 /etc/nginx/sites-available 폴더내 파일에서 설정한다. 

참고 : stackoverflow.com/a/49701625 
log_format testlog '$remote_addr - $remote_user [$time_local] '
               '"$request" $status $bytes_sent '
               '"$http_referer" "$http_user_agent" "$request_body"';

여기서 가장 중요한 것은 $request_body 이다. 여기에 post 로 전달하는 key 와 value 가 key1=value1&key2=value2 형태로 전달된다. 

이 로그가 좋긴 하지만 경우에 따라서는 민감한 정보가 파일로 저장되는 문제가 있다. 로그인 하는 패스워드라든지 패스워드 변경시에 current_password 와 new_password 가 저장될 수 있다.  이 로그를 지워주는 방법이 있다. 
참고 : stackoverflow.com/a/45047352
map 이라는 directive를 이용하면 regular expression 을 이용할 수 있다. 그리고 nginx 가 1.11.0 이상이면 regular expression 을 이용해서 치환을 유연성있게 사용할 수 있다. 그래서 패스워드가 들어가는 부분을 **** 로 별표시로 변경 시킬 수 있다. 

map_hash_bucket_size 256;

map $request_body $req_body_pw1 {
    "~*(?<start>.*)(?<pw_name>password)=(?<pw_value>[^\&]*)(?<end>.*)"      $start&$pw_name=****$end;
    default        $request_body;
}
map $req_body_pw1 $req_body_pw2 {
	"~*(?<start>.*)(?<pw_name>current_password)=(?<pw_value>[^\&]*)(?<end>.*)"       $start&$pw_name=****$end;
	default        $req_body_pw1;
}
map $req_body_pw2 $req_body_pw3 {
	"~*(?<start>.*)(?<pw_name>new_password)=(?<pw_value>[^\&]*)(?<end>.*)"       $start&$pw_name=****$end;
	default        $req_body_pw2;
}
map $req_body_pw3 $req_body_pw4 {
	"~*(?<start>.*)(?<pw_name>confirm_password)=(?<pw_value>[^\&]*)(?<end>.*)"       $start&$pw_name=****$end;
	default        $req_body_pw3;
}


log_format  post_log '$remote_addr - $remote_user [$time_local]    "$request" '
	'$status $body_bytes_sent "$http_referer" '
	'"$http_user_agent" $request_time "$req_body_pw4" '

 

위에서 current_password 나 new_password 나 confirm_password 를 적절하게 변경해서 사용한다면 원하는 대로 masking 이 가능하다. 다만 이 형태가 다양하다면 $req_body_pw숫자 의 개수를 늘려줘야 한다.