[시스템 관리] 02. 웹 서버의 로그(Log)
카테고리: SYS_MGMT
🔔 웹 서버의 로그(Log)
■ 액세스(Access) 로그
요청받은 페이지가 정상으로 응답되었을 때도 액세스 로그에 기록하고 HTTP 상태 코드로 ‘Not Found(404)’와 같은 오류 상태를 회신했을 때도 액세스 로그에 출력됩니다. (단, 웹 서버에서 요청받은 페이지를 찾지 못한 원인은 로그에 포함되지 않습니다.)
■ 오류(Error) 로그
오류 로그는 HTTP 오류 상태를 응답했다는 정보가 아니라, 웹 서버에서 오류가 발생한 경우에 출력됩니다. (404(Not Found)를 응답한 로그 정보는 액세스 로그에 출력되고, 이때 오류가 발생한 원인에 해당하는 내용이 오류 로그로 출력됩니다.)
■ HTTP 상태 코드 목록
■ apache log 관련 설정 (/etc/httpd/conf/httpd.conf)
~(생략)~
# (1)
ErrorLog "logs/error_log"
# (2)
LogLevel warn
<IfModule log_config_module>
# (3)
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
# (4)
CustomLog "logs/access_log" combined
</IfModule>
~(생략)~
| 옵션 | 내용 |
|---|---|
| ErrorLog | 실제 로그 파일의 경로는 “ServerRoot”의 설정 + 여기서 설정한 경로가 됨 (/etc/httpd/logs/error_log) |
| LogLevel | 오류 로그로 추력할 로그 레벨을 지정 (emerg, alert, crit, error, warn, notice, info, debug) |
| CustomLog | 실제 로그 파일의 경로는 “ServerRoot”의 설정 + 여기서 설정한 경로가 됨 (/etc/httpd/logs/access_log), 포맷은 combined 사용, combined는 LogFormat에 지정되어 있다. |
| LogFormat | %h : 원격 호스트명 , %l : 원격 로그명 , %u : 원격 사용자 , %t : 요청을 받아들인 시간 , %r : 요청의 첫행 , %>s : 마지막 status , %b : 응답 바이트 수 , %{Referer}i : Referer 정보 , %{User-Agent}i : User-Agent 정보 |
■ nginx log 관련 설정 (/etc/nginx/nginx.conf)
user nginx;
worker_processes auto;
# (1)
error_log /var/log/nginx/error.log notice;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
# (2)
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# (3)
access_log /var/log/nginx/access.log main;
...
| 옵션 | 내용 |
|---|---|
| error_log | notice 이상의 오류 정보를 /var/log/nginx/error.log로 출력 |
| access_log | 포맷 main의 정의에 따라 /var/log/nginx/access.log로 출력, 포맷 main은 log_format에 정의되어 있다. |
| log_format | 자세한 내용은 (https://nginx.org/en/docs/http/ngx_http_core_module.html#variables) |
■ webalizer - 웹 서버의 액세스 로그 분석
▶ webalizer 설치
# centos 기준, Enterprise Linux 8 (CentOS 8, RHEL 8, Rocky Linux 8, AlmaLinux 8)이하에서만 가능
$ yum -y install webalizer
# ubuntu 기준
$ apt -y install webalizer
▶ webalizer 설정 파일 작성 - Ubuntu 기준
$ vim /etc/webalizer/webalizer.conf
...
# 로그 파일 경로 수정
LogFile /var/log/apache2/access.log
# OutputDir는 출력 파일을 저장할 위치 - 분석 결과 출력
OutputDir /var/www/webalizer
▶ webalizer로 액세스 로그 분석
$ webalizer -c /etc/webalizer/webalizer.conf
▶ webalizer 출력 디렉터리로의 액세스 허용 - Ubuntu 기준
$ cat <<EOF > /etc/apache2/conf-available/webalizer.conf
Alias /webalizer /var/www/webalizer
<Location /webalizer>
Order deny,allow
Deny from all
Allow from 127.0.0.1
Allow from 192.168.219.1
</Location>
EOF
$ a2enconf webalizer
$ systemctl restart apache2
▶ webalizer 액세스 분석 결과 확인 (http://server_addr/webalizer/)
▶ crontab을 통해 webalizer를 자동으로 매일 실행
$ cat <<EOF >> /etc/crontab
## webalizer
0 4 * * * root /usr/bin/webalizer -c /etc/webalizer/webalizer.conf
EOF
댓글 남기기