Nginx关于Rewrite执行顺序详解.docx
,client: 127.0.0.1, server: localhost, request: "GET /aaa.htmlHTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost,request: "GET /aaa.html HTTP/1.1", host: "localhost:9090"”体现了重新匹配location的流程。
,client: 127.0.0.1, server: localhost, request: "GET /aaa.htmlHTTP/1.1", host: "localhost:9090"
, client: 127.0.0.1, server: localhost, request:"GET /aaa.html HTTP/1.1", host: "localhost:9090"
, client:127.0.0.1, server: localhost, request: "GET /aaa.html HTTP/1.1",host: "localhost:9090"
??????? }??
??? ??? location? /ddd.html {
??????????? rewrite "^/ddd\.html$" /eee.html;
??????? }
简单伪代码描述下rewrite执行过程:
?
boolean match_finish = false;
int match_count = 0;
while(!match_finish && match_count < 10) {
??? ??? match_count ++;
??? (1)按编辑顺序执行server级的rewrite指令;
??? (2)按重写后的URI匹配location;
??? (3)
??? ??? String uri_before_location = uri;
??? ??? 按编辑顺序执行location级的rewrite指令;
??? ??? String uri_after_location = rewrite(uri);
??? ??? if(uri_before_location != uri_after_location) {
??? ??? ??? match_finish = false;??? ??? ???
??? ??? } else {
??? ??? ??? match_finish = true;
??? ??? }
??? ??? if(location rewrite has last flag) {
??? ??? ??? continue;//表示不执行后面的rewrite,直接进入下一次迭代
??? ??? }
??? ??? if(location rewrite has break flag) {
??? ??? ??? break;//表示不执行后面的rewrite,并退出循环迭代
??? ??? }
}
if(match_count <= 10) {
??? return HTTP_200;
} else {
??? return HTTP_500;
}