1. commands related to Nginx Rewrite Rules
Commands related to Nginx Rewrite Rules include if, rewrite, set, return, and break. rewrite is the most critical command. A simple Nginx Rewrite rule syntax is as follows:
Rewrite ^/B/(. *) \. html/play. php? Video = $1 break;
If the if statement is added, the example is as follows:
If (! -F $ request_filename)
{Rewrite ^/img/(. *) $/site/$ host/images/$1 last ;}
2. Comparison between Nginx and Apache Rewrite rule instances
Simple Nginx and Apache rewrite rules are basically completely compatible.
Apache Rewrite rules:
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:
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 is easy to find that the Rewrite rule of Apache is changed to the Rewrite rule of Nginx. If the Rewrite rule is changed, run the "nginx-t" command to check and find nginx. if the conf configuration file has a syntax error, you can try to quote the condition. For example, the Nginx Rewrite rule will report a syntax error:
Rewrite ^/(%0-9%%5%%%%.html $/x. jsp? Id = $1 last; correct with quotation marks:
Rewrite "^/(%0-9%%5%%%%.html $"/x. jsp? Id = $1 last;
The Rewrite rules of Apache and Nginx are slightly different in URL redirection:
Apache Rewrite rules:
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 noticed that "http: // $ host" is added to the replacement string of the Nginx Rewrite rule, which is required in Nginx.
In addition, the Rewrite rules of Apache and Nginx differ in variable names, for example:
Apache Rewrite rules:
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;
Commands and tags with the same or similar functions of Apache and Nginx Rewrite rules are as follows:
Apache's RewriteCond command corresponds to Nginx's if command;
Apache's RewriteRule command corresponds to Nginx's rewrite command;
The [R] Mark of Apache corresponds to the redirect mark of Nginx;
The [P] Mark of Apache corresponds to the last mark of Nginx;
The [R, L] Mark of Apache corresponds to the redirect mark of Nginx;
The [P, L] Mark of Apache corresponds to the last mark of Nginx;
The [PT, L] Mark of Apache corresponds to the last mark of Nginx;
Allow the specified domain name to access this site, other domain names will jump to the http://www.aaa.com:
Apache Rewrite rules:
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 command does not support nesting, AND does not support AND, OR, AND other multi-condition matching. Compared with Apache's RewriteCond, it is more troublesome. However, we can use the Nginx configuration method on the next page to implement this example:
Nginx Rewrite rules:
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;
}