사이트 내 전체검색
아파치 로그 파일 - 특이한 녀석들은 따로 담거나 없애기
로빈아빠
https://cmd.kr/server/665 URL이 복사되었습니다.

본문

아파치 로그 파일 - 특이한 녀석들은 따로 담거나 없애기 

* 제목이 좀 이상하네요........^.^ 

작성자 : 김칠봉 <san2(at)linuxchannel.net> 
작성일 : 2001. 04. 30 
대상자 : 초보 

- 힌트 URL : 임은재님이 쓴 글을 보고 나서 
  http://kltp.kldp.org/stories.php?story=00/10/22/9724184 

- 관련 문서 : 아파치 제공 문서 
  http://httpd.apache.org/docs/mod/mod_log_config.html 
  http://httpd.apache.org/docs/mod/mod_setenvif.html 


목차 

1. 배경 

2. 기초지식 
  2-1. 로그포맷과 CustomLog 지시자 
  2-2. 아파치 환경변수 설정 

3. 예제 
  3-1. 특정 IP 주소만 환경변수로 설정하기 
  3-2. 특정 타입의 파일만 환경변수로 설정하기 
  3-3. 특정 User-Agent 만 환경변수로 설정하기 
  3-4.  종합예제 : 사오정(?) 로그 분석 피하기 

------------------------------------------ 

1. 배경 

몇 달 전부터 Webalizer 라는 로그 분석기로 제가(이하 '필자') 운영하는 싸이트의 
로그를 대충 분석(?)해 봤는데 사오정(?) 분석이 되어 버렸더군요. 
결정적으로 필자가 운영하는 싸이트의 대부분은 php로 구성되어 있는데, 이는 실제로 

 - 방문자 외에 localhost에서 php가 실행하는 로그 기록 
 - 로봇들의 접근 기록 
 - 운영자(필자)가 접근한 로그 기록 

 등등이 함께 기록되어 있어 순수 방문자 통계에 약간 덜(?) 정확한 통계가 나오더군요. 
 따라서 이런 유형들의 로그는 없애거나 따로 로그기록하는 것이 낫을 것 같더군요. 

 위의 내용이 이하 다루는 내용입니다. 


2. 기초지식 

2-1. 로그포맷과 CustomLog 지시자 

Module mod_log_config 은 아파치 기본 모듈입니다. 

    로그 포맷 스트링 

    %a : 원격의 IP 주소 
    %b : 헤더를 포함한 전송량(bytes) 
    %{var}e : 환경 변수 "var" 
    %f : 파일이름 
    %h : 원격의 호스트 
    %{hdr}i : 서버에 들어오는(요청) 헤더 값 "hdr" 
    %l : 원격의 로그인 ID(지원한다면) 
    %{label}n : 다른 모듈에서 "label" 구성 
    %{hdr}o : 응답 헤더 값 "hdr" 
    %p : 서버의 Canonical 포트 번호 
    %P : 자식 프로세스 ID(PID) 
    %r : 첫번째 요청 라인 
    %s : 상태코드 
    %t : 시간 포맷(CLF 포맷) 
    %{format}t : "format"으로 구성된 시간 포맷 
    %T : 서버에 요청하는 시간(초) 
    %u : 원격의 유저이름(인증시) 
    %U : 요청한 URL 
    %v : 클라이언트 요청에 따른 Canonical 서버네임 
    %V : UseCanonicalName 설정에 따른 서버네임 

일반적으로 아파치를 설치하고 나면, 다음과 같이 기본설정되어 있을 겁니다. 
(굳지 수정할 필요없음) 

    LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined 
    LogFormat "%h %l %u %t \\"%r\\" %>s %b" common 
    LogFormat "%{Referer}i -> %U" referer 
    LogFormat "%{User-agent}i" agent 

    CustomLog /usr/local/apache/logs/access_log common 

물론 이 정도는 다 알고 계시리라 믿습니다. 

    로그 기록 지시자 

    CookieLog 
    CustomLog 
    LogFormat 
    TransferLog 

CookieLogs는 제외하고 나머지 지시자에 대해서 알아봅시다. 

LogFormat 지시자 

Syntax: LogFormat format|nickname [nickname] 
Default: LogFormat "%h %l %u %t \\"%r\\" %>s %b" 
Context: server config, virtual host 
Status: Base 
Compatibility: Nickname only available in Apache 1.3 or later 
Module: mod_log_config 


CustomLog 지시자 

Syntax: CustomLog file|pipe format|nickname [env=[!]environment-variable] 
Context: server config, virtual host 
Status: Base 
Compatibility: Nickname only available in Apache 1.3 or later. 
Conditional logging available in 1.3.5 or later. 
Module: mod_log_config 


TransferLog 지시자 

Syntax: TransferLog file|pipe 
Default: none 
Context: server config, virtual host 
Status: Base 
Module: mod_log_config 

LogFormat 지시자는 앞에서 예제가 있으므로 생략하고 비슷한 기능을 가진 
CustomLog, TransferLog 지지자에 대해서 잠깐 알아봅시다. 

둘다 file 에 로그를 기록한다는 면에서는 동일한 기능입니다.(pipe 기능도 동일) 

    같은점 

    1. 둘다 file에 로그를 기록한다. 
    2. |pipe 에 설정한 외부 프로그램을 인자로 사용할 수 있다. 
    3. 둘다 다중 로그를 사용할 수 있다. 

    다른점 

    1. CustomLog 지시자는 LogFormat 지시자에서 설정한 nickname이나 Log format을 
        인자로 사용할 수 있다. 
    2. CustomLog 지시자는 환경변수를 인자로 가질 수 있다. 


따라서, 
이하 다룰 내용은 환경변수를 설정하고 이 환경변수(env)와 동일(=)하거나 그렇지 않은(!=) 
특정 유형에 대해서 로그에 기록해야하 하므로 당연히 CustomLog 지시자를 사용해야 
합니다. 

*주의) 
환경변수를 인자로 사용할 경우 하나만 가능. 

이해가 되셨는지 모르겠네요.... 


2-2. 아파치 환경변수 설정 

아파치에서 환경변수를 설정하는 방법은 몇가지 있습니다. 
예를 들어, 

    - mod_env 모듈에 의한 
      SetEnv 
      지시자 이용 

    - mod_setenvif 모듈에에 의한 
      BrowserMatch 
      BrowserMatchNoCase 
      SetEnvIf 
      SetEnvIfNoCase 
      지시자 이용 

등이 있습니다. 
여기에서 주로 사용할 지시자는 BrowserMatch 지시자와 SetEnvIf 지시자입니다. 

*참고) 
xNoCase 지시자는 대소문자를 구분하지 않겠다는 의미입니다. 

이 두개의 지시자에 대한 사용법만 간단하게 알아봅시다. 

BrowserMatch 지시자 

Syntax: BrowserMatch regex envar[=value] [envar[=value]] ... 
Default: none 
Context: server config, virtual host, directory, .htaccess 
Override: FileInfo 
Status: Base 
Module: mod_setenvif 
Compatibility: Apache 1.2 and above (in Apache 1.2 this directive was found in the 
now-obsolete mod_browser module); use in .htaccess files only supported with 
1.3.13 and later 


SetEnvIf 지시자 

Syntax: SetEnvIf attribute regex envar[=value] [envar[=value]] ... 
Default: none 
Context: server config, virtual host, directory, .htaccess 
Override: FileInfo 
Status: Base 
Module: mod_setenvif 
Compatibility: Apache 1.3 and above; the Request_Protocol keyword and 
environment-variable 
matching are only available with 1.3.7 and later; use in .htaccess files only supported 
with 1.3.13 and later 

  환경변수 지정 및 값 지정 방법 

  1.varname, or 
  2.!varname, or 
  3.varname=value 

  예 : 

  BrowserMatch ^Mozilla forms jpeg=yes browser=netscape 
  BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript 
  BrowserMatch MSIE !javascript 

  BrowserMatchNoCase Robot is_a_robot 
  SetEnvIfNoCase User-Agent Robot is_a_robot 

  BrowserMatchNoCase mac platform=macintosh 
  BrowserMatchNoCase win platform=windows 

  SetEnvIf Request_URI "\\.gif$" object_is_image=gif 
  SetEnvIf Request_URI "\\.jpg$" object_is_image=jpg 
  SetEnvIf Request_URI "\\.xbm$" object_is_image=xbm 
        : 
  SetEnvIf Referer www\\.mydomain\\.com intra_site_referral 
        : 
  SetEnvIf object_is_image xbm XBIT_PROCESSING=1 

  SetEnvIfNoCase Host Apache\\.Org site=apache 

  SetEnvIf 지자자에서 attribute 에 올 수 있는 것들 : 

  Remote_Host - the hostname (if available) of the client making the request 
  Remote_Addr - the IP address of the client making the request 
  Remote_User - the authenticated username (if available) 
  Request_Method - the name of the method being used (GET, POST, et cetera) 
  Request_Protocol - the name and version of the protocol with which the request 
                                    was made (e.g., "HTTP/0.9", "HTTP/1.1", etc.) 
  Request_URI - the portion of the URL following the scheme and host portion 
  그외 
  Host, User-Agent, and Referer 가능 

  see http://www.rfc-editor.org/rfc/rfc2616.txt 

따라서, 
BrowserMatch와 SetEnvIf User-Agent 는 서로 동일하다는 것을 알 수 있을 겁니다. 

*중요) 
CustomLog 지자자와 다르게 환경변수 인자에 여러 개를 설정할 수 있음. 

여기까지 이해가 되셨다면 다음은 보지 않아도 될듯 하군요.......^.^ 


3. 예제 

3-1. 특정 IP 주소만 환경변수로 설정하기 

- 환경변수 이름 : do_not_log 
- 목적 : 이 환경변수에 해당되는 특정 IP 주소는 access_log에 기록하지 않는다. 

    SetEnvIf Remote_Addr "^127.0.0.1$" do_not_log 
    SetEnvIf Remote_Addr "^211.35.159.12[89]$" do_not_log 
    SetEnvIf Remote_Addr "^211.35.159.1[345][0-9]$" do_not_log 

    위에서 설정한 특정 IP 주소는 
    127.0.0.1 자기자신을 말하는 루프백 주소 
    211.35.159.128, 211.35.159.129 두개의 IP 주소 
    211.35.159.130 ~ 211.35.159.139 10개의 IP 주소 
    211.35.159.140 ~ 211.35.159.149 10 개의 IP 주소 
    211.35.159.150 ~ 211.35.159.159 10 개의 IP 주소 

즉 원격의 IP 주소(Remote_Addr)가 위와 일치하면 do_not_log 환경변수에 지정하는 예임. 

*참고) 
^ 은 시작을 의미 
$ 은 마지막을 의미 
[0-9] 0~9까지의 숫자중 어느 하나 


3-2. 특정 타입의 파일만 환경변수로 설정하기 

    SetEnvIfNoCase Request_URI "\\.(gif|jpg|png|css|js|java)$" do_not_log 

    요청 URI(Request_URI) 파일이 
    *.gif 
    *.jpg 
    *.png 
    *.css 
    *.js 
    *.java 

    로 끝난 파일인 경우(대소문자를 구별하지 않음) do_not_log 환경변수에 지정함 


3-3. 특정 User-Agent 만 환경변수로 설정하기 

- 환경변수 이름 : do_not_log 과 is_a_robot 
- 목적 : 이 환경변수에 해당되는 특정 User-Agent는 access_log에 기록하지 않고, 
            따로 로그(robot-log)에 기록하거나 기록하지 않기 위함. 

    BrowserMatchNoCase "ru-robot" do_not_log is_a_robot 
    BrowserMatchNoCase "Slurp/si" do_not_log is_a_robot 
    BrowserMatchNoCase "Mercator" do_not_log is_a_robot 
    BrowserMatchNoCase "Gulliver" do_not_log is_a_robot <

댓글목록

등록된 댓글이 없습니다.

1,139 (3/23P)

Search

Copyright © Cmd 명령어 18.118.252.87