
Cập nhật lần cuối: 23/01/2026 17:00
Tài liệu này hướng dẫn cấu hình Alloy để thu thập Metrics, Logs của dịch vụ Nginx
log_format json_analytics 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 spent 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"'
'}';
server {
listen 127.0.0.1:8080;
server_name localhost;
location /stub_status {
stub_status on;
# Optionally restrict access to IP addresses if needed
# allow 127.0.0.1;
# deny all;
}
}
sudo nginx -tsudo systemctl reload nginxLưu ý: Nếu đã có exporter vui lòng bỏ qua bước này
Thay phiên bản và kiến trúc máy tương ứng tại đây.
wget https://github.com/nginx/nginx-prometheus-exporter/releases/download/v1.4.2/nginx-prometheus-exporter_1.4.2_linux_amd64.tar.gztar -xzvf nginx-prometheus-exporter-1.4.2-linux-amd64.tar.gzsudo mv nginx-prometheus-exporter /usr/local/bin/sudo chmod +x /usr/local/bin/nginx-prometheus-exporternginx-prometheus-exporter --nginx.scrape-uri=http://IP_VM:8080/stub_statusLưu ý: Đảm bảo server đã cấu hình metrics thành công
curl http://localhost:8080/stub\_statusTại đường dẫn /etc/alloy/config.alloy, chỉnh sửa Alloy để tích hợp Nginx:
sudo vi /etc/alloy/config.alloyThay các trường ở file cấu hình sau, trong đó:
| Trường | Ý nghĩa | Giá trị mẫu |
| url | Endpoint của dịch vụ APM trên giao diện Console | https://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push |
| Authorization | Token của dịch vụ APM trên giao diện Console | a5130b91-ff9b-41ef-8722-9d923a574123 |
| cluster | Tên dịch vụ APM | Cluster-IDC |
| __address__ | IP của VM | 10.168.17.30 |
Thay đổi các thông tin theo cấu hình:
logging {
level = "warn"
format = "logfmt"
}
prometheus.remote_write "metrics_integrations_nginx" {
endpoint {
url = "<endpoint dịch vụ APM>"
headers = {
Authorization = "<token dịch vụ APM>",
}
}
external_labels = {
tenant = "<tên dịch vụ APM>",
}
}
discovery.relabel "metrics_integrations_nginx" {
targets = [{
__address__ = "<IP VM>:9113",
}]
rule {
source_labels = ["__address__"]
regex = "(.*):.*"
target_label = "instance"
replacement = "$1"
}
}
prometheus.scrape "metrics_integrations_nginx" {
targets = discovery.relabel.metrics_integrations_nginx.output
forward_to = [prometheus.remote_write.metrics_integrations_nginx.receiver]
job_name = "integrations/nginx"
}
Thay các trường ở file cấu hình trên, trong đó:
| Trường | Ý nghĩa | Giá trị mẫu |
| url | Endpoint của dịch vụ APM trên giao diện Console | https://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push |
| Authorization | Token của dịch vụ APM trên giao diện Console | a5130b91-ff9b-41ef-8722-9d923a574123 |
| tenant | Tên dịch vụ APM | Tenant-IDC |
| __address__ | IP của VM | 10.168.17.30 |
Thay đổi các thông tin theo cấu hình:
logging {
level = "warn"
format = "logfmt"
}
loki.write "logs_integrations_nginx" {
endpoint {
url = "<endpoint dịch vụ APM>"
headers = {
Authorization = "<token dịch vụ APM>",
}
}
external_labels = {
tenant = "<tên dịch vụ APM>",
}
}
discovery.relabel "logs_integrations_nginx" {
targets = [{
__address__ = "<IP VM>:9113",
}]
rule {
source_labels = ["__address__"]
regex = "(.*):.*"
target_label = "instance"
replacement = "$1"
}
}
local.file_match "logs_integrations_nginx" {
path_targets = [{
__address__ = "localhost",
__path__ = "/var/log/nginx/*.log",
host = "localhost",
instance = "IP-VM",
job = "integrations/nginx",
}]
}
loki.source.file "logs_integrations_nginx" {
targets = local.file_match.logs_integrations_nginx.targets
forward_to = [loki.write.logs_integrations_nginx.receiver]
}
Thay các trường ở file cấu hình trên, trong đó:
| Trường | Ý nghĩa | Giá trị mẫu |
| url | Endpoint của dịch vụ APM trên giao diện Console | https://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push |
| Authorization | Token của dịch vụ APM trên giao diện Console | a5130b91-ff9b-41ef-8722-9d923a574123 |
| tenant | Tên dịch vụ APM | Tenant-IDC |
| __address__ | IP của VM | 10.168.17.30 |
Thay đổi các thông tin theo cấu hình:
logging {
level = "warn"
format = "logfmt"
}
prometheus.remote_write "metrics_integrations_nginx" {
endpoint {
url = "<endpoint dịch vụ APM>"
headers = {
Authorization = "<token dịch vụ APM>",
}
}
external_labels = {
tenant = "<tên dịch vụ APM>",
}
}
loki.write "logs_integrations_nginx" {
endpoint {
url = "<endpoint dịch vụ APM>"
headers = {
Authorization = "<token dịch vụ APM>",
}
}
external_labels = {
tenant = "<tên dịch vụ APM>",
}
}
discovery.relabel "metrics_integrations_nginx" {
targets = [{
__address__ = "<IP VM>:9113",
}]
rule {
source_labels = ["__address__"]
regex = "(.*):.*"
target_label = "instance"
replacement = "$1"
}
}
prometheus.scrape "metrics_integrations_nginx" {
targets = discovery.relabel.metrics_integrations_nginx.output
forward_to = [prometheus.remote_write.metrics_integrations_nginx.receiver]
job_name = "integrations/nginx"
}
local.file_match "logs_integrations_nginx" {
path_targets = [{
__address__ = "localhost",
__path__ = "/var/log/nginx/*.log",
host = "localhost",
instance = "IP-VM",
job = "integrations/nginx",
}]
}
loki.source.file "logs_integrations_nginx" {
targets = local.file_match.logs_integrations_nginx.targets
forward_to = [loki.write.logs_integrations_nginx.receiver]
}
Thực hiện lệnh sau để khởi động Alloy:
sudo systemctl restart alloy Thực hiện lệnh sau:
sudo systemctl status alloy Hệ thống trả về như sau nếu thành công:
● alloy.service - Vendor-agnostic OpenTelemetry Collector distribution with programmable pipelines
Loaded: loaded (/lib/systemd/system/alloy.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2025-04-24 08:56:32 +07; 2s ago
Docs: https://grafana.com/docs/alloy
Main PID: 3815363 (alloy)
Tasks: 13 (limit: 9385)
Memory: 54.9M
CPU: 583ms
CGroup: /system.slice/alloy.service
└─3815363 /usr/bin/alloy run --server.http.listen-addr=0.0.0.0:30000 --storage.path=/var/lib/alloy/data /etc/alloy/config.alloy
Nếu không thành công, xem log Alloy:
sudo journalctl -u alloy Sau đó chỉnh sửa cấu hình nếu cần và khởi động lại:
sudo systemctl restart alloy Tham khảo hướng dẫn tích hợp dashboard, kiểm tra metrics Nginx trên giao diện Dashboard: