Apache HTTP Server는 Nginx와 함께 가장 널리 사용되는 웹 서버입니다. Apache의 Combined Log Format은 업계 표준이 되어 많은 웹 서버가 이 포맷을 따르고 있습니다. 이 글에서는 Apache 로그의 구조와 분석 방법을 자세히 설명합니다.
| 배포판 | 로그 경로 | |
|---|---|---|
| Ubuntu/Debian | /var/log/apache2/access.log | |
| CentOS/RHEL | /var/log/httpd/access_log | |
| macOS (Homebrew) | /usr/local/var/log/httpd/access_log | |
| 필드 | 의미 | 예시 값 |
%h | 클라이언트 IP | 192.0.2.100 |
%l | 식별자 (대부분 -) | - |
%u | 인증된 사용자 | frank (없으면 -) |
%t | 요청 시각 | [10/Oct/2024:13:55:36 -0700] |
%r | 요청 라인 | GET /admin HTTP/1.1 |
%>s | 최종 응답 코드 | 403 |
%b | 응답 바이트 수 | 287 (없으면 -) |
%{Referer}i | 참조 URL | http://example.com/ |
%{User-Agent}i | 클라이언트 식별자 | Mozilla/5.0 |
10/May/2024:03:21:05 +0000 (초 단위, UTC 명시)10/Oct/2024:13:55:36 -0700 (초 단위, 타임존 offset)-로 표기Nginx (nginx.conf):
log_format main '$remote_addr - $remote_user [$time_local] ...'; access_log /var/log/nginx/access.log main;
Apache (httpd.conf 또는 apache2.conf):
LogFormat "%h %l %u %t \"%r\" %>s %b" common CustomLog /var/log/apache2/access.log common
기본 포맷에는 응답시간이 없습니다. 성능 분석을 위해 %D 또는 %T를 추가하세요.
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %T/%D" combined_time
CustomLog /var/log/apache2/access.log combined_time
%T: 초 단위 (정수, 예: 1)%D: 마이크로초 단위 (예: 1234567 = 1.234567초)192.0.2.100 - - [10/Oct/2024:14:00:00 -0700] "GET /api/data HTTP/1.1" 200 4821 "-" "curl/7.68.0" 0/312442
마지막 0/312442는 0초 / 312,442 마이크로초 = 약 0.312초
Apache는 가상 호스트마다 별도 로그 파일을 만들 수 있습니다.
<VirtualHost *:80>
ServerName example.com
CustomLog /var/log/apache2/example.com-access.log combined
ErrorLog /var/log/apache2/example.com-error.log
</VirtualHost>
<VirtualHost *:80> ServerName blog.example.com CustomLog /var/log/apache2/blog-access.log combined ErrorLog /var/log/apache2/blog-error.log </VirtualHost>
awk '{print $7}' /var/log/apache2/access.log | sort | uniq -c | sort -rn | head -20
awk '$9 ~ /40[13]/ {print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -rn | head -20
awk '{print $NF, $7}' /var/log/apache2/access.log | sed 's/\/.*//' | sort -rn | head -20
awk '{print $4}' /var/log/apache2/access.log | cut -d: -f2 | sort | uniq -c
액세스 로그는 요청을, 에러 로그는 서버 내부 문제를 기록합니다.
tail -f /var/log/apache2/error.log
에러 로그 예시:
[Fri Oct 10 14:05:22.123456 2024] [core:error] [pid 12345] [client 192.0.2.100:54321] File does not exist: /var/www/html/favicon.ico
[Fri Oct 10 14:06:00.987654 2024] [php7:error] [pid 12346] [client 192.0.2.101:54322] script '/var/www/html/index.php' not found or unable to stat
Ubuntu/Debian은 logrotate가 자동으로 로그를 관리합니다.
/etc/logrotate.d/apache2:
/var/log/apache2/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
if /etc/init.d/apache2 status > /dev/null ; then \
/etc/init.d/apache2 reload > /dev/null; \
fi;
endscript
}
daily: 매일 로테이션rotate 14: 14일치 보관compress: 오래된 로그 gzip 압축postrotate: 로테이션 후 Apache 리로드CDN/프록시를 사용하면 모든 요청 IP가 프록시 IP로 기록됩니다. X-Forwarded-For 헤더를 로그에 포함하세요.
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" cloudflare
CustomLog /var/log/apache2/access.log cloudflare
Apache 로그는 Nginx와 유사하지만 날짜 포맷, 응답 크기 표기, 설정 문법이 다릅니다. 가상 호스트별 로그 분리, 응답시간 추가, 실제 IP 기록 설정을 통해 더 정확한 분석이 가능합니다.