NGINX-1.14.0添加JSON日志模块仍出明文日志,求解决方案及配置示例
Hey there! Let's work through your NGINX JSON logging problem step by step. First, let's clear up a key misunderstanding, then fix your existing module setup, and finally cover other reliable methods to get JSON-formatted logs in NGINX 1.14.0.
First: The nginx-json-status-module Isn't for Logging
That second module you installed (nginx-json-status-module) is designed to return JSON-formatted server status data when you hit an endpoint like /status—it has nothing to do with access or error logging. That's probably why it wasn't changing your log format at all! Let's focus on the first module and other logging-specific solutions.
Fixing the nginx-http-json-log Module Setup
First, confirm the module is actually compiled into your NGINX build. Run this command to check:
nginx -V
Look for --add-module=/path/to/nginx-http-json-log in the configure arguments section. If it's not there, you need to recompile NGINX with the module properly included.
If the module is loaded, here's the correct nginx.conf configuration to use it:
http { # Define a JSON log format using the module's directive json_log_format json_logs { "timestamp": "$time_iso8601", "remote_addr": "$remote_addr", "remote_user": "$remote_user", "request": "$request", "status": "$status", "body_bytes_sent": "$body_bytes_sent", "referer": "$http_referer", "user_agent": "$http_user_agent", "request_time": "$request_time" } server { listen 80; server_name your-domain.com; # Use the JSON log format for access logs access_log /var/log/nginx/access.json.log json_logs; # Your other server configs (locations, root, etc.) location / { root /usr/share/nginx/html; index index.html; } } }
After updating the config, reload NGINX:
nginx -s reload
Check your log file—you should see JSON entries now. If not, double-check the module path in your compile command and ensure there are no typos in the json_log_format block.
Alternative 1: Native NGINX Variable Splicing (No Extra Modules)
If you don't want to rely on third-party modules, you can manually build a JSON log format using NGINX's native variables. This works perfectly in 1.14.0 and requires no extra compilation.
Here's the nginx.conf example:
http { # Define a manual JSON log format log_format json_logs '{' '"timestamp": "$time_iso8601",' '"remote_addr": "$remote_addr",' '"remote_user": "$remote_user",' '"request": "$request",' '"status": "$status",' '"body_bytes_sent": "$body_bytes_sent",' '"referer": "$http_referer",' '"user_agent": "$http_user_agent",' '"request_time": "$request_time"' '}'; server { listen 80; server_name your-domain.com; access_log /var/log/nginx/access.json.log json_logs; # Your server configs location / { root /usr/share/nginx/html; index index.html; } } }
Note: Make sure there are no trailing commas in the JSON object (NGINX will throw errors if you leave one after the last key-value pair).
Alternative 2: ngx_log_json Module (Mature Third-Party Option)
Another reliable third-party module is ngx_log_json (a more widely used alternative to the one you tried). To use it:
- Recompile NGINX with
--add-module=/path/to/ngx_log_json - Use this config in
nginx.conf:
http { # Enable the module's JSON logging log_json on; # Optional: Customize which fields to include log_json_fields timestamp=$time_iso8601 remote_addr=$remote_addr status=$status request=$request; server { listen 80; server_name your-domain.com; # The log will automatically be JSON-formatted access_log /var/log/nginx/access.json.log; # Your server configs location / { root /usr/share/nginx/html; index index.html; } } }
This module simplifies the config by handling JSON formatting automatically, so you don't have to build the object manually.
Final Checks
- Always test your config with
nginx -tbefore reloading to catch syntax errors. - If logs still aren't JSON, verify the log file path is correct and NGINX has write permissions to it.
内容的提问来源于stack exchange,提问作者Jeeukrishnan Kayshyap

