nginx 配置
server {
listen 8080 default_server ;
root /opt/website/write ;
rewrite_log on ;
location /location1/ {
rewrite /location1/(.*) /test/$1 last ;
return 200 "location1\r\n" ;
}
location /location2/ {
rewrite /location2/(.*) /test/$1 break ;
return 200 "location2\r\n" ;
}
location /test/ {
return 200 "test\r\n" ;
}
location / {
return 200 "null\r\n" ;
}
}
- 创建目录和index.html
# mkdir -p /opt/website/write/{location1,location2,test}
# ls /opt/website/write/|awk '{print $1".index" > "/opt/website/write/"$1"/index.html"}'
- 测试:
[root@centos-81 ~]# curl -s 192.168.1.81:8080/location2/
test
[root@centos-81 ~]# curl -s 192.168.1.81:8080/location2/index.html
test.index
疑问:
对第一个请求结果有疑问,照着官方文档描述,break 不会跳出location层级,应该直接寻找 test/目录下的index.html,理论上返回的是 test.index。不知道是啥原因返回了 test
在第二个请求中也证明了是寻找 /test/目录下的文件,而不是跳出location。