需求:访问 xxx.com/* 重写到 xxx.com/testdir/*
测试
if ($request_uri !~* "/testdir/?") {
rewrite ^(.*) /testdir$1 last;
}
- xxx.com 访问提示"File not Found"
- xxx.com/x.php 重写到 xxx.com/testdir/x.php 访问正常,测试 xxx.com/index.php 也正常
开启重写日志进一步分析,但没看懂。
"/testdir/?" does not match "/", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
"^(.*)" matches "/", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
rewritten data: "/testdir/", args: "", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
"/testdir/?" does not match "/", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
"^(.*)" matches "/testdir/index.php", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
rewritten data: "/testdir/testdir/index.php", args: "", client: 1.1.1.1, server: xxx.com, request: "GET / HTTP/2.0", host: "xxx.com"
换成以下规则测试 OK
if ($document_uri !~* "/testdir/?") {
rewrite ^(.*) /testdir$1 last;
}