#Nginx
[Nginx] 누가 내 서버에 접근했는지 알아보자(feat. 접근로그)
18 June 2024 / 16:24

1. 접근 로그 형식을 설정해보자

Ubuntu 기준으로

vim /etc/nginx/nginx.conf

nginx 설정 파일을 vim으로 연다.

여러개의 내장 변수를 통해 남기고 싶은 정보만 로그에 남길 수 있다.

http {
  log_format my_log
    '[$time_local] $remote_addr '
    '$request_method $server_protocol '
    '$scheme $server_name $uri $status '
    '$request_time $body_bytes_sent '
    '"$http_referer" "$http_user_agent"';
}

$time_local : 서버가 요청 받은 시점의 로컬 시간
$remote_addr : 방문자 IP
$request_method : 요청 메소드
$scheme : HTTP or HTTPS
$status : HTTP 응답코드
$request_time : 요청 처리에 걸린 시간
$body_bytes_sent : 보낸 데이터 크기
$http_referer : 해당 요청을 보내기 전 거쳐 온 URL
$http_user_agent : 클라이언트가 사용한 브라우저 정보

2. 서버별로 로그 파일 생성 경로를 지정해보자

server { 
    access_log /var/log/nginx/my_blog/access.log my_log;
}

access_log [로그 파일 생성 경로] [로그 포맷 이름];

형식을 통해 로그 포맷과 로그 파일 생성 경로를 지정할 수 있다.

위의 설정을 통해 생성된 로그는

[18/Jun/2024:21:43:57 +0900] *.*.*.* GET HTTP/1.1 https example.com /api/category/all 200 0.001 93 "-" "node"

와 같이 출력된다.

error_log 또한 서버 별로 로그 파일 생성 경로를 지정할 수 있다.

server { 
    error_log /var/log/nginx/my_blog/error.log warn;
}

error_log는 로그 레벨을 설정할 수 있다.

debug, info, notice, warn, error, erit, alert, emerg 이다. (오른쪽으로 갈수록 심각하다)

error_log는 access_log와는 다르게 로그 형식 지정이 불가능하다.

@Designed By gomdolog.