Skip to Content
Suffering builds character
아카이브8.Nginx특징로깅2. 요청 경로별로 로깅 파일 분리하기

2. 요청 경로별로 로깅 파일 분리하기

모든 요청에 대해 로깅을 수행할 경우,
로그 파일의 용량이 커지게 되고, 매번 로그 처리 연산을 수행하느라 서버의 부하에 영향을 미칠 수 있음

또한 서로 관련있는 요청별로 로그 파일들을 구분하여 모아두는 것이 유지보수에 편하기 때문에 적절한 분리를 통해 로깅을 처리하는 것이 권장됨

요청 경로별로 로깅 파일 분리하기

nginx.conf
events {} http { include mime.types; server { listen 80; server_name localhost; root /nginx-practice; index index.html; # location 블록 내에 지정하여 해당 요청 시에만 아래의 파일에 로깅 처리 수행 location /custom { access_log /var/log/nginx/custom.access.log; return 200 'custom route'; } } }

설정 파일 리로드

terminal
nginx -s reload

custom.access.log 파일 생성 여부 확인

terminal
ls logs # access.log custom.access.log error.log nginx.pid # → log 파일이 생성되었음

해당 경로로 접근 테스트

terminal
curl localhost/custom

요청 로그 확인

terminal
cat logs/custom.access.log 127.0.0.1 - - [16/Jul/2025:21:59:57 +0900] "GET /custom HTTP/1.1" 200 12 "-" "curl/8.14.1" # → 해당 파일에 로그가 기록됨

access.log의 요청 로그 확인

terminal
cat access.log 127.0.0.1 - - [16/Jul/2025:21:48:28 +0900] "GET / HTTP/1.1" 200 403 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36" 127.0.0.1 - - [16/Jul/2025:21:48:28 +0900] "GET /style.css HTTP/1.1" 200 527 "http://localhost/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36" 127.0.0.1 - - [16/Jul/2025:21:48:28 +0900] "GET /.well-known/appspecific/com.chrome.devtools.json HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36" 127.0.0.1 - - [16/Jul/2025:21:50:11 +0900] "GET /nothing HTTP/1.1" 404 153 "-" "curl/8.14.1"

→ 전역 로그 파일에는 /custom 경로에 대한 로그가 기록되지 않음

💡
Tip

전역 로그 파일에도 추가하고 싶을 경우에는 다음과 같이 access_log 디렉티브를 한줄 추가하여 작성하면 됨

location /custom { access_log /var/log/nginx/custom.access.log; access_log /var/log/nginx/access.log; # 전역 로깅도 수행 return 200 'custom route' }
Last updated on