Hướng Dẫn Cấu Hình Nginx JSON Access Logging

Nginx là một trong những máy chủ web mạnh mẽ và phổ biến nhất hiện nay. Một trong những tính năng hữu ích của Nginx là khả năng ghi log truy cập (access log) ở định dạng JSON. Định dạng JSON giúp việc phân tích log trở nên dễ dàng hơn, đặc biệt khi tích hợp với các công cụ như ELK Stack, Splunk, hay các hệ thống phân tích log khác. Trong bài blog này, chúng ta sẽ tìm hiểu cách cấu hình Nginx để ghi access log ở định dạng JSON.
Tại Sao Nên Sử Dụng JSON Access Logging?
Dễ phân tích: JSON là định dạng có cấu trúc, dễ dàng được các công cụ xử lý log đọc và phân tích.
Tích hợp tốt: Nhiều công cụ hiện đại hỗ trợ JSON một cách tự nhiên, giúp giảm thiểu bước chuyển đổi định dạng.
Tùy chỉnh linh hoạt: Bạn có thể chọn các trường thông tin cần ghi vào log, tùy thuộc vào nhu cầu.
Các Bước Cấu Hình Nginx JSON Access Logging
1. Xác Định Định Dạng Log JSON
Để ghi log ở định dạng JSON, chúng ta cần định nghĩa một định dạng log tùy chỉnh bằng cách sử dụng chỉ thị log_format trong tệp cấu hình Nginx. Dưới đây là một ví dụ về định dạng JSON:
log_format main_json escape=json '{'
'"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
'"connection": "$connection", ' # connection serial number
'"connection_requests": "$connection_requests", ' # number of requests made in connection
'"pid": "$pid", ' # process pid
'"request_id": "$request_id", ' # the unique request id
'"request_length": "$request_length", ' # request length (including headers and body)
'"remote_addr": "$remote_addr", ' # client IP
'"remote_user": "$remote_user", ' # client HTTP username
'"remote_port": "$remote_port", ' # client port
'"time_local": "$time_local", '
'"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
'"request": "$request", ' # full path no arguments if the request
'"request_uri": "$request_uri", ' # full path and arguments if the request
'"args": "$args", ' # args
'"status": "$status", ' # response status code
'"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
'"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
'"http_referer": "$http_referer", ' # HTTP referer
'"http_user_agent": "$http_user_agent", ' # user agent
'"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
'"http_host": "$http_host", ' # the request Host: header
'"server_name": "$server_name", ' # the name of the vhost serving the request
'"request_time": "$request_time", ' # request processing time in seconds with msec resolution
'"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
'"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
'"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
'"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
'"upstream_response_length": "$upstream_response_length", ' # upstream response length
'"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
'"ssl_protocol": "$ssl_protocol", ' # TLS protocol
'"ssl_cipher": "$ssl_cipher", ' # TLS cipher
'"scheme": "$scheme", ' # http or https
'"request_method": "$request_method", ' # request method
'"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
'"pipe": "$pipe", ' # “p” if request was pipelined, “.” otherwise
'"gzip_ratio": "$gzip_ratio", '
'"http_cf_ray": "$http_cf_ray"'
'}';
Giải thích:
escape=json: Đảm bảo các giá trị được mã hóa đúng theo định dạng JSON, xử lý các ký tự đặc biệt như dấu ngoặc kép.
Các biến như $remote_addr, $request_method,... là các biến mặc định của Nginx, đại diện cho thông tin về yêu cầu HTTP.
Bạn có thể thêm hoặc bớt các trường tùy theo nhu cầu.
2. Cấu Hình Access Log
Sau khi định nghĩa định dạng log, bạn cần chỉ định nơi lưu trữ log và sử dụng định dạng vừa tạo. Thêm đoạn cấu hình sau vào khối http hoặc server trong tệp cấu hình Nginx (thường là /etc/nginx/nginx.conf hoặc một tệp trong /etc/nginx/conf.d/):
access_log /var/log/nginx/access.json json_log;
/var/log/nginx/access.json: Đường dẫn đến tệp log.
json_log: Tên định dạng log đã định nghĩa ở bước 1.
3. Kiểm Tra và Khởi Động Lại Nginx
Trước khi áp dụng thay đổi, hãy kiểm tra cú pháp cấu hình:
sudo nginx -t
Nếu không có lỗi, khởi động lại Nginx để áp dụng cấu hình:
sudo systemctl restart nginx
Lưu Ý
Hiệu suất: Ghi log JSON có thể tốn tài nguyên hơn so với định dạng văn bản thông thường. Hãy đảm bảo bạn chỉ ghi các trường cần thiết.
Bảo mật: Tệp log có thể chứa thông tin nhạy cảm như địa chỉ IP hoặc user agent. Hãy bảo vệ tệp log và hạn chế quyền truy cập.
Sao lưu và xoay vòng log: Sử dụng logrotate để quản lý kích thước tệp log, tránh đầy dung lượng đĩa.
Subscribe to my newsletter
Read articles from Kilo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
