Setting Up Nginx Access Log Visualization with Loki and Grafana
This guide walks through installing Loki, Promtail, and Grafana (via Docker), configuring Nginx to emit JSON‑formatted access logs, collecting them with Promtail, storing them in Loki, and visualizing the data in Grafana dashboards, including geo‑IP enrichment and world‑map panels.
A customer needed a simple way to view website traffic without using Google or Baidu analytics, so the solution chosen is the Loki‑Grafana stack combined with Nginx.
Install Nginx (standard installation) and download binary releases of Promtail and Loki, then start them with their respective configuration files.
Promtail configuration (saved as promtail.yaml ) sets the HTTP listening port, positions file, Loki push endpoint, and a scrape job for Nginx logs, including a replace stage to mask IP addresses:
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://localhost:3100/loki/api/v1/push
scrape_configs:
- job_name: nginx
pipeline_stages:
- replace:
expression: '(?:[0-9]{1,3}\.){3}([0-9]{1,3})'
replace: '***'
static_configs:
- targets:
- localhost
labels:
job: nginx_access_log
host: expatsxxxxs
agent: promtail
__path__: /var/log/nginx/expatshxxxxs.access.logModify Nginx to output logs in JSON format by adding a log_format json_analytics escape=json block that captures fields such as $msec , $remote_addr , $request , $status , $upstream_addr , $geoip_country_code , etc.
log_format json_analytics escape=json '{'
"msec": "$msec",
"connection": "$connection",
...
"geoip_country_code": "$geoip_country_code"'
}';Install GeoIP libraries ( yum -y install GeoIP GeoIP-data GeoIP-devel ), then recompile Nginx with --with-http_geoip_module to enable the geoip directive. Replace the old binary using a graceful reload ( kill -USR2 ).
After confirming Nginx emits JSON logs, start Grafana via Docker:
docker run -d -p 3000:3000 grafana/grafanaLog in with the default admin/admin credentials, add Loki as a data source, and verify log ingestion using the Explore view.
Import a pre‑built dashboard (by ID) to visualize request counts, response times, status codes, and geo‑IP information. The world‑map panel initially lacks map tiles, so install the required plugin:
grafana-cli plugins install grafana-worldmap-panelRestart Grafana, reload the dashboard, and the map now displays (subject to external map access restrictions). The final setup provides a quick, self‑hosted solution for detailed website access analytics using Loki and Grafana.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.