본문 바로가기

리눅스

NGINX에서 POST 데이터를 액세스 로그에 기록하는 방법

반응형

POST 데이터를 NGINX 액세스 로그에 기록하는 방법

NGINX에서 POST 데이터를 액세스 로그에 기록하려면 log_format 디렉티브를 사용하여 로그 형식을 정의하고 $request_body 변수를 포함시키면 됩니다.

 

다음은 POST 데이터를 액세스 로그에 기록하는 예시입니다.

 

1. nginx 설정 파일 (nginx.conf 또는 사이트 구성 파일)을 엽니다.

 

2. http 블록 내에 있는 log_format 디렉티브를 찾습니다. 만약 log_format 디렉티브가 없다면 새로 추가해야 합니다.

 

3. 다음과 같이 log_format 디렉티브를 정의하고 $request_body 변수를 사용하여 POST 데이터를 포함시킵니다.

http {
  log_format custom_log '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $body_bytes_sent '
                       '"$http_referer" "$http_user_agent" '
                       '"$request_body" "$http_x_forwarded_for"';
  # 이하 설정 내용
  access_log /var/log/nginx/access.log main;

}

위 예시에서는 custom_log라는 로그 형식을 정의하고 $request_body 변수를 마지막에 포함시켰습니다.

 

4. 원하는 위치에 액세스 로그를 사용하는 access_log 디렉티브를 추가하고, 로그 형식으로 custom_log을 지정합니다.

예를 들어

server {
  listen 80;
  server_name example.com;
  
  access_log /var/log/nginx/access.log custom_log;
  
  # 이하 설정 내용
}

위 예시에서는 /var/log/nginx/access.log 파일에 액세스 로그를 기록하며, 로그 형식으로 custom_log을 사용합니다.

 

5. nginx 설정을 저장하고 재시작합니다.

# 설정 파일 오류 확인
sudo nginx -t
sudo systemctl restart nginx

 

이제 nginx는 POST 데이터를 액세스 로그에 기록합니다. 로그 파일을 확인하여 POST 요청의 데이터를 확인할 수 있습니다. 단, 보안 및 개인정보 보호를 위해 로그 파일에 POST 데이터를 기록하기 전에 적절한 필터링 및 보안 조치를 적용하는 것이 중요합니다.

728x90

 

nginx 로그 포맷

vim /etc/nginx/nginx.conf
 ...
 http {
    # Logging Settings
    access_log on;
    log_not_found off;
    
    log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$remote_addr"';

    log_format postdata '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$remote_addr" "$request_body"';
                        
    log_format post_logs '[$time_local] "$request" $status '  
                        '$body_bytes_sent "$http_referer" '        
                        '"$http_user_agent" [$request_body]'; 

    log_format json escape=json '{"@time": "$time_iso8601",'
                        '"@fields": { '
                        '"host": "$remote_addr",'
                        '"vhost": "$http_host",'
                        '"status": "$status",'
                        '"protocol": "$server_protocol",'
                        '"method": "$request_method",'
                        '"path": "$uri",'
                        '"querystring": "$query_string",'
                        '"req": "$request",'
                        '"size": "$body_bytes_sent",'
                        '"reqtime": "$request_time",'
                        '"uprtime": "$upstream_response_time",'
                        '"ua": "$http_user_agent",'
                        '"forwardedfor": "$http_x_forwarded_for",'
                        '"forwardedproto": "$http_x_forwarded_proto",'
                        '"referrer": "$http_referer"}}';
...
}
vim /etc/nginx/conf.d/default.conf
...
server {
    listen 80;
...
    #access_log /var/log/nginx/access.log main;
    access_log /var/log/nginx/access.log postdata buffer=32k;;
...
}
  • $request_body : 파마리터
  • buffer=32k : 로그를 버퍼에 저장한 후 기록

 

728x90
반응형

'리눅스' 카테고리의 다른 글

리눅스 ccze 명령어  (0) 2020.10.13
[리눅스] ping 명령어  (0) 2020.10.13
jq 명령어  (0) 2020.10.06
쿠버네티스 클러스터에 Helm을 설치하는 방법  (0) 2020.10.06
[Kubernetes] kubernetes 애플리케이션 배포 -3  (0) 2020.10.06