{"msg":"操作成功","code":200,"data":{"createBy":"admin","createTime":"2025-07-16 13:07:05","updateBy":"admin","updateTime":"2025-08-11 13:07:05","remark":null,"id":119,"articleTitle":"Nginx（十）日志格式","articleUrl":"nginx_log_format","articleThumbnail":"https://www.asumimoe.com/imgfiles/20250618/4b57c2e46f8949e5a30d4e2e9e8a8fd5.png","articleFlag":"0","draftStatus":"1","reprintStatement":"0","articleSummary":"Nginx 日志格式非常灵活，允许您自定义记录的字段。以下是 Nginx 日志格式的全部可用字段及其详细说明","articleContent":"## Nginx 日志格式完整字段解析\n\nNginx 日志格式非常灵活，允许您自定义记录的字段。以下是 Nginx 日志格式的全部可用字段及其详细说明：\n\n### 默认日志格式字段（combined 格式）\n\n```nginx\nlog_format combined '$remote_addr - $remote_user [$time_local] '\n                   '\"$request\" $status $body_bytes_sent '\n                   '\"$http_referer\" \"$http_user_agent\"';\n```\n\n#### 字段解释：\n\n1. **`$remote_addr`**  \n   - 客户端 IP 地址\n   - 示例：`192.168.1.100`\n\n2. **`$remote_user`**  \n   - HTTP 基本认证的用户名（如果启用）\n   - 未认证时为 `-`\n   - 示例：`admin` 或 `-`\n\n3. **`$time_local`**  \n   - 本地服务器时间\n   - 格式：`[05/Aug/2025:15:30:22 +0800]`\n   - 包含日期、时间和时区\n\n4. **`$request`**  \n   - 完整的 HTTP 请求行\n   - 格式：`\"GET /index.html HTTP/1.1\"`\n   - 包含方法、URI 和协议版本\n\n5. **`$status`**  \n   - HTTP 响应状态码\n   - 示例：`200`, `404`, `500`\n\n6. **`$body_bytes_sent`**  \n   - 发送给客户端的响应体字节数（不包括响应头）\n   - 示例：`1024`\n\n7. **`$http_referer`**  \n   - 请求来源 URL（Referer 头）\n   - 示例：`\"https://www.google.com/\"` 或 `\"-\"`\n\n8. **`$http_user_agent`**  \n   - 客户端浏览器/代理信息\n   - 示例：`\"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\"`\n\n## 完整可用字段列表（按类别）\n\n### 请求相关字段\n\n| 变量名            | 描述                               | 示例值                     |\n| ----------------- | ---------------------------------- | -------------------------- |\n| `$request`        | 完整的原始请求行                   | `GET /index.html HTTP/1.1` |\n| `$request_method` | HTTP 请求方法                      | `GET`, `POST`              |\n| `$request_uri`    | 完整的请求 URI（带参数）           | `/search?q=nginx`          |\n| `$uri`            | 当前请求的 URI（不含参数）         | `/search`                  |\n| `$args`           | 请求中的查询字符串                 | `q=nginx&page=1`           |\n| `$query_string`   | 同 `$args`                         | `q=nginx&page=1`           |\n| `$is_args`        | 如果请求有参数则为 \"?\"，否则为空   | `?` 或 空                  |\n| `$request_length` | 请求长度（包括请求行、头部和正文） | `256`                      |\n\n### 响应相关字段\n\n| 变量名                   | 描述                             | 示例值                    |\n| ------------------------ | -------------------------------- | ------------------------- |\n| `$status`                | HTTP 响应状态码                  | `200`, `404`              |\n| `$body_bytes_sent`       | 发送给客户端的正文字节数         | `1024`                    |\n| `$bytes_sent`            | 发送给客户端的字节总数（包括头） | `1256`                    |\n| `$sent_http_<header>`    | 发送到客户端的响应头             | `$sent_http_content_type` |\n| `$sent_trailer_<header>` | 响应结束时分块发送的尾部头       |                           |\n\n### 连接信息\n\n| 变量名                 | 描述                     | 示例值          |\n| ---------------------- | ------------------------ | --------------- |\n| `$remote_addr`         | 客户端 IP 地址           | `192.168.1.100` |\n| `$remote_port`         | 客户端端口号             | `54321`         |\n| `$server_addr`         | 服务器接收请求的 IP 地址 | `10.0.0.5`      |\n| `$server_port`         | 服务器接收请求的端口     | `80`, `443`     |\n| `$server_protocol`     | 请求协议版本             | `HTTP/1.1`      |\n| `$connection`          | 连接序列号               | `12345`         |\n| `$connection_requests` | 当前连接已处理的请求数   | `3`             |\n| `$nginx_version`       | Nginx 版本               | `1.24.0`        |\n\n### 时间相关字段\n\n| 变量名                    | 描述                         | 示例值                         |\n| ------------------------- | ---------------------------- | ------------------------------ |\n| `$time_local`             | 本地时间（通用日志格式）     | `[05/Aug/2025:15:30:22 +0800]` |\n| `$time_iso8601`           | ISO 8601 格式时间            | `2025-08-05T15:30:22+08:00`    |\n| `$msec`                   | 当前时间（秒.毫秒）          | `1688650222.123`               |\n| `$request_time`           | 请求处理时间（秒，毫秒精度） | `0.005`                        |\n| `$upstream_response_time` | 上游服务器响应时间           | `0.003`, `0.002, 0.001`        |\n\n### HTTP 头部字段\n\n| 变量名                  | 描述                             | 示例值                    |\n| ----------------------- | -------------------------------- | ------------------------- |\n| `$http_<header>`        | 请求头（小写，下划线代替连字符） | `$http_user_agent`        |\n| `$sent_http_<header>`   | 响应头（小写，下划线代替连字符） | `$sent_http_content_type` |\n| `$http_user_agent`      | 用户代理头                       | `Mozilla/5.0...`          |\n| `$http_referer`         | Referer 头                       | `https://example.com/`    |\n| `$http_host`            | Host 头                          | `www.example.com`         |\n| `$http_cookie`          | Cookie 头                        | `sessionid=abc123`        |\n| `$http_x_forwarded_for` | X-Forwarded-For 头               | `192.168.1.1, 10.0.0.1`   |\n\n### SSL/TLS 相关字段\n\n| 变量名                | 描述                | 示例值                        |\n| --------------------- | ------------------- | ----------------------------- |\n| `$ssl_protocol`       | SSL/TLS 协议版本    | `TLSv1.3`                     |\n| `$ssl_cipher`         | 协商的加密套件      | `ECDHE-RSA-AES256-GCM-SHA384` |\n| `$ssl_client_verify`  | 客户端证书验证结果  | `SUCCESS`, `FAILED`           |\n| `$ssl_client_s_dn`    | 客户端证书主题 DN   | `/CN=client1`                 |\n| `$ssl_client_i_dn`    | 客户端证书颁发者 DN | `/C=US/O=Let's Encrypt`       |\n| `$ssl_session_id`     | SSL 会话 ID         | `a1b2c3d4e5f6...`             |\n| `$ssl_session_reused` | SSL 会话是否重用    | `r` (重用) 或 `.` (新会话)    |\n\n### 反向代理相关字段\n\n| 变量名                    | 描述                 | 示例值                        |\n| ------------------------- | -------------------- | ----------------------------- |\n| `$upstream_addr`          | 上游服务器地址       | `10.0.0.10:8080`              |\n| `$upstream_status`        | 上游服务器响应状态码 | `200`, `502`                  |\n| `$upstream_response_time` | 上游服务器响应时间   | `0.003`                       |\n| `$upstream_http_<header>` | 上游服务器响应头     | `$upstream_http_content_type` |\n| `$upstream_cache_status`  | 缓存状态             | `HIT`, `MISS`, `BYPASS`       |\n| `$proxy_protocol_addr`    | PROXY 协议客户端地址 | `192.168.1.100`               |\n\n### 其他实用字段\n\n| 变量名                | 描述                     | 示例值                         |\n| --------------------- | ------------------------ | ------------------------------ |\n| `$host`               | 请求中的 Host 头         | `www.example.com`              |\n| `$scheme`             | 请求协议                 | `http`, `https`                |\n| `$limit_rate`         | 速率限制设置             | `10240`                        |\n| `$request_id`         | 唯一请求 ID              | `c29tZXJlcXVlc3RpZA==`         |\n| `$pipe`               | 请求是否为流水线         | `p` (流水线) 或 `.` (非流水线) |\n| `$gzip_ratio`         | gzip 压缩率              | `4.56`                         |\n| `$geoip_country_code` | GeoIP 国家代码（需模块） | `US`, `CN`                     |\n\n## 完整日志格式配置示例\n\n### 扩展日志格式（包含更多详细信息）\n\n```nginx\nlog_format extended '$remote_addr - $remote_user [$time_iso8601] '\n                    '\"$request_method $scheme://$host$request_uri $server_protocol\" '\n                    '$status $body_bytes_sent \"$http_referer\" '\n                    '\"$http_user_agent\" \"$http_x_forwarded_for\" '\n                    'rt=$request_time uct=\"$upstream_connect_time\" '\n                    'urt=\"$upstream_response_time\" us=\"$upstream_status\" '\n                    'cs=$upstream_cache_status ssl=\"$ssl_protocol/$ssl_cipher\" '\n                    'ciphers=\"$ssl_ciphers\" '\n                    'v=\"$server_name\" pid=$pid';\n```\n\n### JSON 格式日志（适合 ELK 等系统）\n\n```nginx\nlog_format json_combined escape=json\n    '{'\n        '\"time_local\":\"$time_local\",'\n        '\"remote_addr\":\"$remote_addr\",'\n        '\"remote_user\":\"$remote_user\",'\n        '\"request\":\"$request\",'\n        '\"status\": \"$status\",'\n        '\"body_bytes_sent\":\"$body_bytes_sent\",'\n        '\"request_time\":\"$request_time\",'\n        '\"http_referrer\":\"$http_referer\",'\n        '\"http_user_agent\":\"$http_user_agent\",'\n        '\"http_x_forwarded_for\":\"$http_x_forwarded_for\",'\n        '\"upstream_addr\":\"$upstream_addr\",'\n        '\"upstream_status\":\"$upstream_status\",'\n        '\"upstream_response_time\":\"$upstream_response_time\",'\n        '\"upstream_cache_status\":\"$upstream_cache_status\",'\n        '\"server_name\":\"$server_name\",'\n        '\"server_port\":\"$server_port\",'\n        '\"scheme\":\"$scheme\"'\n    '}';\n```\n\n## 日志配置最佳实践\n\n1. **为不同服务配置不同日志格式**\n\n   ```nginx\n   http {\n       log_format main_ext '$remote_addr - $remote_user [$time_local] '\n                          '\"$request\" $status $body_bytes_sent '\n                          '\"$http_referer\" \"$http_user_agent\" '\n                          'rt=$request_time uct=\"$upstream_connect_time\"';\n       \n       log_format api_json escape=json '{...}';\n       \n       server {\n           # 常规网站使用扩展格式\n           access_log /var/log/nginx/access.log main_ext;\n           \n           location /api/ {\n               # API 使用 JSON 格式\n               access_log /var/log/nginx/api_access.log api_json;\n           }\n       }\n   }\n   ```\n\n2. **包含重要安全字段**\n\n   ```nginx\n   log_format security '$remote_addr - $remote_user [$time_iso8601] '\n                      '\"$request\" $status $body_bytes_sent '\n                      '\"$http_referer\" \"$http_user_agent\" '\n                      'xff=\"$http_x_forwarded_for\" '\n                      'ssl=\"$ssl_protocol/$ssl_cipher\" '\n                      'country=\"$geoip_country_code\"';\n   ```\n\n3. **性能分析日志**\n\n   ```nginx\n   log_format perf '$remote_addr - $remote_user [$time_local] '\n                  '\"$request\" $status $body_bytes_sent '\n                  'rt=$request_time urt=\"$upstream_response_time\" '\n                  'cpu=$request_cpu_time mem=$request_memory';\n   ```","categoryId":12,"viewCount":113,"categoryName":"Nginx","author":"球接子","authorAvatar":null,"tagIds":[13,14],"tagNames":["Nginx","中间件"]}}