1. Nginx Rewrite rules related directives
Nginx rewrite rules related directives have if, rewrite, set, return, break, etc., where rewrite is the most critical instruction. A simple
The Nginx rewrite rule syntax is as follows:
The code is as follows |
Copy Code |
Rewrite ^/b/(. *). Html/play.php?video=$1 break; |
If you add an if statement, the example is as follows:
The code is as follows |
Copy Code |
if (!-f $request _filename) {Rewrite ^/img/(. *) $/site/$host/images/$1 last; } |
2. Comparison of Nginx and Apache rewrite rule examples
Simple Nginx and Apache rewrite rules are not very different and can be completely compatible.
Apache Rewrite Rules:
The code is as follows |
Copy Code |
Rewriterule ^/(mianshi|xianjing)/$/zl/index.php?name=$1 [L] Rewriterule ^/ceshi/$/zl/ceshi.php [L] Rewriterule ^/(Mianshi) _ ([a-za-z]+)/$/zl/index.php?name=$1_$2 [L] Rewriterule ^/PINGCE ([0-9]*)/$ /zl/pingce.php?id=$1 [L] |
Nginx Rewrite Rules:
The code is as follows |
Copy Code |
Rewrite ^/(mianshi|xianjing)/$/zl/index.php?name=$1 last; Rewrite ^/ceshi/$/zl/ceshi.php last; Rewrite ^/(Mianshi) _ ([a-za-z]+)/$/zl/index.php?name=$1_$2 last; Rewrite ^/PINGCE ([0-9]*)/$/zl/pingce.php?id=$1 last; |
It's easy to see that Apache's rewrite rules change to Nginx rewrite rules are simple, and if you change the rules, use the "nginx-t" command to check for discovery
Nginx.conf the configuration file has syntax errors, you can try to enclose the condition in quotation marks. For example, the Nginx rewrite rule will report syntax errors:
The code is as follows |
Copy Code |
Rewrite ^/([0-9]{5}). html$/x.jsp?id=$1 last; add Quotes right: Rewrite "^/([0-9]{5}). html$"/x.jsp?id=$1 last; |
The rewrite rules for Apache and Nginx have subtle differences in URL jumps:
Apache Rewrite Rules:
The code is as follows |
Copy Code |
Rewriterule ^/html/tagindex/([a-za-z]+)/.*$/$1/[r=301,l] Nginx Rewrite Rules: Rewrite ^/html/tagindex/([a-za-z]+)/.*$ http://$host/$1/permanent; |
In the above example, we notice that the "http://$host" is added to the permutation string of the Nginx Rewrite rule, which is required in Nginx.
In addition, the rewrite rules for Apache and Nginx also differ in terms of variable names, such as:
Apache Rewrite Rules:
The code is as follows |
Copy Code |
Rewriterule ^/user/login/$/user/login.php?login=1&forward=http://%{http_host} [L]
|
Nginx Rewrite Rules:
Rewrite ^/user/login/$/user/login.php?login=1&forward=http://$host last;
Apache has some of the same or similar instructions, tag correspondences as Nginx Rewrite rules:
The rewritecond instruction of Apache corresponds to the if instruction of nginx;
Apache's Rewriterule instruction corresponds to Nginx's rewrite instruction;
The [R] tag of Apache corresponds to the redirect mark of the Nginx;
The [P] tag of Apache corresponds to the last tag of the Nginx;
The [r,l] tag of Apache corresponds to the redirect tag of the Nginx;
The [p,l] tag of Apache corresponds to the last mark of the Nginx;
The [pt,l] tag of Apache corresponds to the last mark of the Nginx;
Allow the designated domain name access to the site, the other domain name jump to http://www.aaa.com:
Apache Rewrite Rules:
The code is as follows |
Copy Code |
Rewritecond%{http_host} ^ (. *?). domain.com$ Rewritecond%{http_host}!^qita.domain.com$ rewritecond%{document_root}/market/%1/index.htm-f Rewriterule ^/wu/$/market/%1/index.htm [L] |
Nginx's if directive does not support nesting, and does not support multiple conditional matches such as and, or, but it is a bit more cumbersome than Apache Rewritecond, but I
You can implement this example by following the Nginx configuration of the next page:
Nginx Rewrite Rules:
code is as follows |
copy code |
if ($host ~* ^ (. *?). domain.com$) { set $var _wupin_city $1; Set $var _wupin 1 '; } If ($host ~* ^qita.domain.com$) { Set $var _wupin 0 '; } If (!-f $document _root/market/$var _ wupin_city/index.htm) { Set $var _wupin 0 '; } if ($var _wupin ~ ' 1 ') { rewrite ^/wu/$/market/$var _wupin_city/ index.htm last; } |