Skip to content

Commit bc11f26

Browse files
committed
ops/network-service: Nginx logging
1 parent be458b4 commit bc11f26

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

docs/ops/network-service/nginx.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ server {
190190
GET /path/to/something?query=string HTTP/1.1
191191
```
192192

193-
这里的 `/path/to/something?query=string` 就是 Request URI。在 Nginx 中,由 [`$request_uri`](https://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri) 变量表示。同时,Nginx 会对用户提供的 Request URI 进行归一化(处理 `%xx` 编码、`..` 等),然后将归一化后的路径存储在 [`$uri` 变量](https://nginx.org/en/docs/http/ngx_http_core_module.html#var_uri)。`location` 块的匹配也是基于归一化后的 `$uri` 变量进行的。
193+
这里的 `/path/to/something?query=string` 就是 Request URI。在 Nginx 中,由 [`$request_uri`](https://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri) 变量表示。同时,Nginx 会对用户提供的 Request URI 进行归一化(处理 `%xx` 编码、`..` 等),然后将归一化后的路径存储在 [`$uri`](https://nginx.org/en/docs/http/ngx_http_core_module.html#var_uri) 变量中。`location` 块的匹配也是基于归一化后的 `$uri` 变量进行的。
194194

195195
一个 `location` 块的基本结构如下:
196196

@@ -993,7 +993,7 @@ if ($http_user_agent ~* "^Mozilla") {
993993

994994
!!! warning "谨慎在 `location` 中使用 `if`"
995995

996-
曾有一篇[官方博客文章 "If is evil"](https://github.com/nginxinc/nginx-wiki/blob/master/source/start/topics/depth/ifisevil.rst)讨论了在 `location` 块中使用 `if` 指令可能带来的问题。简单来讲,以下的使用场景是安全的:
996+
曾有一篇[官方博客文章 "If is evil"](https://github.com/nginxinc/nginx-wiki/blob/master/source/start/topics/depth/ifisevil.rst) 讨论了在 `location` 块中使用 `if` 指令可能带来的问题。简单来讲,以下的使用场景是安全的:
997997

998998
- 在 `server` 块中使用 `if` 指令。
999999
- 在 `location` 块中使用 `if` 指令,但只包含 `return` 或者 `rewrite ... last` 指令。
@@ -1008,6 +1008,79 @@ if ($http_user_agent ~* "^Mozilla") {
10081008

10091009
## 日志 {#logging}
10101010

1011+
Nginx 对日志提供了完善的支持,其中核心模块提供了 [`error_log`](https://nginx.org/en/docs/ngx_core_module.html#error_log),HTTP 模块提供了 [`access_log`](https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log) 指令,分别用于配置错误日志和访问日志。默认的配置一般如下:
1012+
1013+
```nginx
1014+
error_log /var/log/nginx/error.log;
1015+
1016+
http {
1017+
access_log /var/log/nginx/access.log;
1018+
# ...
1019+
}
1020+
```
1021+
1022+
!!! warning "不要写 `error_log off`"
1023+
1024+
`access_log` 支持 `off` 参数,表示关闭访问日志,但是 `error_log` 不支持 `off` 参数。如果写了 `error_log off`,Nginx 不会报错,看起来也能正常运行,但是实际上错误日志会被写入到**名为 `off` 的文件**中(默认情况下,路径会是 `/usr/share/nginx/off`)。很多时候要等待 `off` 这个文件变得非常大,才会发现问题所在。
1025+
1026+
如果需要关闭错误日志,可以将 `error_log` 输出到 `/dev/null`:
1027+
1028+
```nginx
1029+
error_log /dev/null;
1030+
```
1031+
1032+
访问日志默认的格式是 `combined`,类似如下:
1033+
1034+
```combined
1035+
123.45.67.8 - - [12/Mar/2023:00:15:32 +0800] "GET /path/to/a/file HTTP/1.1" 200 3009 "-" ""
1036+
```
1037+
1038+
当然,其支持使用 [`log_format`](https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format) 自定义日志格式,以下给出 combined 的定义,以及输出 JSON 格式日志的示例:
1039+
1040+
```nginx
1041+
log_format combined '$remote_addr - $remote_user [$time_local] '
1042+
'"$request" $status $body_bytes_sent '
1043+
'"$http_referer" "$http_user_agent"';
1044+
1045+
log_format ngx_json escape=json '{'
1046+
'"timestamp":$msec,'
1047+
'"clientip":"$remote_addr",'
1048+
'"serverip":"$server_addr",'
1049+
'"method":"$request_method",'
1050+
'"scheme":"$scheme",'
1051+
'"url":"$request_uri",'
1052+
'"status":$status,'
1053+
'"size":$body_bytes_sent,'
1054+
'"resp_time":$request_time,'
1055+
'"http_host":"$host",'
1056+
'"referer":"$http_referer",'
1057+
'"user_agent":"$http_user_agent",'
1058+
'"request_id":"$request_id",'
1059+
'"proto":"$server_protocol"'
1060+
'}';
1061+
1062+
access_log /var/log/nginx/access_json.log ngx_json;
1063+
```
1064+
1065+
`escape=json` 参数会假设变量会出现在 JSON 字符串中,并由此进行 JSON 转义。
1066+
1067+
访问日志也支持条件输出,例如下面这个将 HTTP 403 响应分开记录到单独日志文件的示例:
1068+
1069+
```nginx
1070+
map $status $log_403 {
1071+
403 1;
1072+
default 0;
1073+
}
1074+
1075+
map $log_403 $log_normal {
1076+
0 1;
1077+
default 0;
1078+
}
1079+
1080+
access_log /var/log/nginx/access_403.log combined if=$log_403;
1081+
access_log /var/log/nginx/access.log combined if=$log_normal;
1082+
```
1083+
10111084
## Lua {#lua}
10121085

10131086
## 示例介绍 {#examples}

0 commit comments

Comments
 (0)