Nginx log format configuration
Nginx logs generally have main format (default) and json formats.
Comparative summary
characteristic | Default text format | JSON format |
---|---|---|
performance | High performance, low resource occupancy | Time-consuming for parsing, relatively high CPU usage |
Disk occupancy | Short logs, small occupancy | Each field has a name, which takes up a large occupancy |
Log Analysis | Need to be manually parsed, which is more complicated | Structured data, easy integration of analysis tools |
readability | Easy to view with human eyes | Not intuitive, mainly rely on the log system to view it |
Extensibility | Not convenient to add new fields | Flexible addition of new fields |
Integration | Compatible with traditional logging tools | Suitable for log docking with ELK and EFK |
💡 Best practices
- Performance priority: If the server performance is insufficient, use text format first.
- Log analysis is preferred: If you want to perform centralized log analysis (such as ELK), use the JSON format first.
- Log hierarchy: The error log uses text format, and the access log uses JSON format, which can not only efficiently record errors, but also analyze business data.
- Log cutting and archiving: Timely log cutting and cleaning to avoid full disks.
🎯 Summary
- Default text format: high performance, low resource consumption, suitable for lightweight or high performance requirements production environments.
- JSON format: Structured logs are easy to analyze and monitor, and are suitable for modern operation and maintenance and log management needs.
1. Main log variable
1. Request related variables
Variable name | meaning | Example Values |
---|---|---|
$remote_addr | Client IP address | 192.168.1.1 |
$remote_user | Client authentication username | admin |
$request | The complete string of requested (method + URI + protocol) | GET / HTTP/1.1 |
$request_method | Request method | GET, POST |
$request_uri | Original request URI (including parameters) | /?user=123 |
$uri | The requested URI does not include query parameters | / |
$args | Requested query parameter string | user=123&name=abc |
$query_string | Equivalent to $args, query string | user=123&name=abc |
$status | Responsive HTTP status code | 200, 404 |
$body_bytes_sent | Response body size (number of bytes) | 2048 |
$http_referer | Recommended page URL | / |
$http_user_agent | Client browser information | Mozilla/5.0 (Windows NT) |
2. Time-dependent variables
Variable name | meaning | Example Values |
---|---|---|
$time_local | Local Timestamp | 19/Mar/2025:04:03:51 +0800 |
$time_iso8601 | Timestamp in ISO 8601 format | 2025-03-19T04:03:51+08:00 |
$msec | Unix timestamps accurate to milliseconds | 1614768000.123 |
3. Client-related variables
Variable name | meaning | Example Values |
---|---|---|
$remote_addr | Client IP address | 192.168.1.1 |
$remote_port | Client port number | 52345 |
$http_x_forwarded_for | The original IP address of the client, usually used in the proxy link | 192.168.1.1 |
$http_user_agent | User-Agent information for the client | Mozilla/5.0 (Windows NT) |
$http_referer | The source URL of the request (if any) | / |
4. Upstream server-related variables
Variable name | meaning | Example Values |
---|---|---|
$upstream_addr | The address of the upstream server (including port) | 127.0.0.1:8080 |
$upstream_status | Status code of upstream server response | 200, 502 |
$upstream_response_time | Response time of upstream server (seconds) | 0.125 |
$upstream_connect_time | Time to establish a connection with the upstream server | 0.050 |
5. Request to process related variables
Variable name | meaning | Example Values |
---|---|---|
$request_time | Total time (seconds) taken to process a request | 0.123 |
$response_time | Total time (seconds) returned by the response | 0.098 |
$upstream_response_time | Time (seconds) for the response received from the upstream server | 0.125 |
$pipe | Whether the request is processed through pipeline connection, p means yes, . means no | p |
6. Nginx internal variables
Variable name | meaning | Example Values |
---|---|---|
$hostname | Nginx hostname | nginx-server |
$server_name | The name of the server currently processing the request | |
$server_addr | The IP address of the server currently processing the request | 192.168.1.1 |
$nginx_version | Current version number of Nginx | 1.21.3 |
Sample configuration: Nginx log format
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/ main
#Output result
192.168.200.1 - - [19/Mar/2025:03:26:35 -0400] "GET /prod-api/captchaImage HTTP/1.1" 200 4137 "http://192.168.200.14/login?redirect=%2Findex" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0" "-"
192.168.200.1 - - [19/Mar/2025:03:26:40 -0400] "POST /prod-api/login HTTP/1.1" 200 239 "http://192.168.200.14/login?redirect=%2Findex" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0" "-"
Field meaning:
- time:
- Log timestamp, in ISO 8601 format.
-
"2025-03-19T04:51:55-04:00"
: Indicates March 19, 2025, 04:51:55 (-04:00 time zone).
-
- Log timestamp, in ISO 8601 format.
- request_time:
- The total time required is in seconds.
-
"0.000"
: Indicates that the time from receiving the request to returning the response is 0 seconds, and the response is extremely fast.
-
- The total time required is in seconds.
- upstream_response_time:
- The response time of the upstream server (such as Tomcat) is in seconds.
-
"-"
: Indicates that the current request has not passed through the upstream server (such as static resource request).
-
- The response time of the upstream server (such as Tomcat) is in seconds.
- remote_addr:
- Client IP address.
-
"192.168.200.1"
: The request is issued from this IP address.
-
- Client IP address.
- remote_user:
- The identity authentication information of the remote user.
-
"-"
: means that identity authentication has not been performed (or authentication is not involved).
-
- The identity authentication information of the remote user.
- request:
- The request line, including the request method, resource path, and protocol version.
-
"GET /static/js/chunk-2d0bce05. HTTP/1.1"
: Indicates that a GET request was initiated and the specified static resource file was accessed.
-
- The request line, including the request method, resource path, and protocol version.
- status:
- The response status code indicates the request processing result.
-
"200"
: The request was successful.
-
- The response status code indicates the request processing result.
- body_bytes_sent:
- The number of response volume bytes sent to the client.
-
"10029"
: Indicates that the response body size is 10029 bytes.
-
- The number of response volume bytes sent to the client.
- http_referer:
- The URL from which the request originated, indicating which page the request was initiated.
-
"http://192.168.200.14/monitor/druid"
: Indicates a request initiated from the monitoring page.
-
- The URL from which the request originated, indicating which page the request was initiated.
- http_user_agent:
- The client's user agent information usually contains browser and operating system information.
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0"
- The client's user agent information usually contains browser and operating system information.
- http_x_forwarded_for:
- Used to record the real IP address of the client, usually used in reverse proxy or load balancing scenarios.
-
"-"
: means that the header information has not been passed or is not carried.
-
- Used to record the real IP address of the client, usually used in reverse proxy or load balancing scenarios.
2. Commonly used variables of Json
1. Request related variables
Variable name | meaning | Example Values |
---|---|---|
$request | The complete string of requested (method + URI + protocol) | GET / HTTP/1.1 |
$request_method | Request method | GET, POST, PUT, DELETE |
$request_uri | The original requested URI (including parameters) | /?user=123 |
$uri | Requested URI (excluding parameters) | / |
$args | Requested parameter string | user=123&name=abc |
$query_string | Equivalent to $args | user=123&name=abc |
$status | Response status code | 200, 404, 500 |
$protocol | Request Agreement | HTTP/1.1, HTTP/2.0 |
$host | Request hostname | |
$server_name | Server name | |
$server_port | Server Port | 80, 443 |
2. Time and date related variables
Variable name | meaning | Example Values |
---|---|---|
$time_iso8601 | Timestamp in ISO 8601 format | 2025-03-19T04:03:51+08:00 |
$time_local | Local Timestamp | 19/Mar/2025:04:03:51 +0800 |
$msec | Timestamps accurate to milliseconds (Unix timestamps) | 1614768000.123 |
3. Client-side and user-related variables
Variable name | meaning | Example Values |
---|---|---|
$remote_addr | Client IP address | 192.168.1.1 |
$remote_port | Client port | 52345 |
$remote_user | Authenticated username | admin |
$http_user_agent | User Agent String | Mozilla/5.0 (Windows NT 10.0...) |
$http_referer | Recommendation page | / |
$http_x_forwarded_for | Original client IP address forwarded by proxy server | 192.168.1.1, 172.16.0.1 |
4. Upstream and proxy-related variables
Variable name | meaning | Example Values |
---|---|---|
$upstream_addr | Upstream server address (IP: port) | 127.0.0.1:8080 |
$upstream_status | Status code returned by upstream server | 200, 502 |
$upstream_response_time | Upstream server response time (seconds) | 0.125 |
$upstream_connect_time | The time (seconds) it takes to connect to the upstream server | 0.050 |
$upstream_header_time | Upstream server response header time (seconds) | 0.075 |
5. Request processing and performance-related variables
Variable name | meaning | Example Values |
---|---|---|
$request_time | Total time (seconds) from the request's receipt to the completion of the response | 0.245 |
$body_bytes_sent | Number of response bytes sent to the client (excluding response headers) | 1024 |
$bytes_sent | Total number of bytes sent (including response headers) | 2048 |
$connection | Connected serial number | 12345 |
$connection_requests | Number of requests processed on the current connection | 5 |
6. System and environment variables
Variable name | meaning | Example Values |
---|---|---|
$pid | Worker process ID that handles the request | 1234 |
$pipe | Indicates whether the request is piped or not | p (pipe), . (direct connection) |
Example: JSON log format configuration
log_format json '{"time":"$time_iso8601",' '"request_time":$request_time,' '"upstream_response_time":"$upstream_response_time",' '"remote_addr":"$remote_addr",' '"remote_user":"$remote_user",' '"request":"$request",' '"status":$status,' '"body_bytes_sent":$body_bytes_sent,' '"http_referer":"$http_referer",' '"http_user_agent":"$http_user_agent",' '"http_x_forwarded_for":"$http_x_forwarded_for"}'; access_log /var/log/nginx/ json;
#Output result
{"time":"2025-03-19T04:51:55-04:00","request_time":0.000,"upstream_response_time":"-","remote_addr":"192.168.200.1","remote_user":"-","request":"GET /static/js/chunk-2d0bce05. HTTP/1.1","status":200,"body_bytes_sent":10029,"http_referer":"http://192.168.200.14/monitor/druid","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0","http_x_forwarded_for":"-"}
{"time":"2025-03-19T04:51:56-04:00","request_time":0.010,"upstream_response_time":"0.009","remote_addr":"192.168.200.1","remote_user":"-","request":"GET /prod-api/monitor/server HTTP/1.1","status":200,"body_bytes_sent":93,"http_referer":"http://192.168.200.14/monitor/server","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0","http_x_forwarded_for":"-"}
Field meaning:
-
time:
- Log timestamp, in ISO 8601 format.
-
"2025-03-19T04:51:55-04:00"
: Indicates March 19, 2025, 04:51:55 (-04:00 time zone).
-
request_time:
- The total time required is in seconds.
-
"0.000"
: Indicates that the time from receiving the request to returning the response is 0 seconds, and the response is extremely fast.
-
upstream_response_time:
- The response time of the upstream server (such as Tomcat) is in seconds.
-
"-"
: Indicates that the current request has not passed through the upstream server (such as static resource request).
-
remote_addr:
- Client IP address.
-
"192.168.200.1"
: The request is issued from this IP address.
-
remote_user:
- The identity authentication information of the remote user.
-
"-"
: means that identity authentication has not been performed (or authentication is not involved).
-
request:
The request line, including the request method, resource path, and protocol version.
-
"GET /static/js/chunk-2d0bce05. HTTP/1.1"
-
GET
: HTTP request method. -
/static/js/chunk-2d0bce05.
: The requested resource path. -
HTTP/1.1
: Protocol version.
-
-
status:
- HTTP response status code.
-
200
: Indicates that the request is successful.
-
body_bytes_sent:
- Number of bytes of the response body.
-
10029
: Indicates that the response size is 10029 bytes (approximately 9.8 KB).
-
http_referer:
- The referral page, which page the request was initiated from.
-
"http://192.168.200.14/monitor/druid"
: The user initiates a request from this page.
-
http_user_agent:
- Client's browser and operating system information.
- Browser: Chrome 134.0.0.0
- Operating system: Windows 10 x64
-
http_x_forwarded_for:
- Used to identify the real IP of the client accessed through the proxy server.
-
"-"
: means that the header information is not passed or the header information is not passed.
This is the end of this article about the implementation of Nginx log format configuration. For more related contents of Nginx log configuration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!