Skip to content

Commit f1bb24f

Browse files
committed
ops/network-service: Note about if and existence check
1 parent 991b1e9 commit f1bb24f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

docs/ops/network-service/nginx.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ autoindex on;
882882

883883
location @dir_check {
884884
internal;
885-
if (-d $request_filename) {
885+
if (-d $request_filename) { # 判断目录是否存在
886886
rewrite ^(.*)$ /_dir_handler/$1/ last;
887887
}
888888
return 404;
@@ -894,6 +894,8 @@ autoindex on;
894894
}
895895
```
896896

897+
有关 `internal` 与 `if` 等相关的介绍,可参考下文的 [Rewrite](#rewrite) 部分。
898+
897899
## 速率、请求与连接数限制 {#limiting}
898900

899901
Nginx 提供了多种不同维度的限制功能,帮助减轻恶意流量对服务的影响,保护后端稳定运行,将更多资源分配给正常用户。以下介绍 Nginx 自带的限制功能,如果需要更复杂的规则,可以参考 [Lua](#lua) 部分。
@@ -1027,6 +1029,14 @@ if ($http_user_agent ~* "^Mozilla") {
10271029
}
10281030
```
10291031

1032+
`if` 中的条件也可以使用类似 Shell 的语法做文件存在性判断,例如 `-d` 判断目录是否存在,`-f` 判断文件是否存在,`-x` 判断是否有可执行权限等:
1033+
1034+
```nginx
1035+
if (-x $request_filename) {
1036+
return 403; # 拒绝访问可执行文件
1037+
}
1038+
```
1039+
10301040
!!! tip "同时符合多个条件的判断"
10311041

10321042
Nginx 的 `if` 指令不支持 `&&` 或者 `||` 这样的逻辑运算,并且不支持 `if` 嵌套。如果需要同时满足多个条件,可以添加一个变量来实现:

0 commit comments

Comments
 (0)