Comparison between Nginx Rewrite Rules and apache

Source: Internet
Author: User

Comparison between nginx rewrite Rules and apache

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/(. * tutorial .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;
 
}

Comparison between nginx and apache rewrite rule instances

Simple nginx and apache rewrite rules are basically completely compatible. For example:
 
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;
 
The preceding example shows that the rewrite rule of apache is changed to the rewrite rule of nginx, which is actually very simple: the rewriterule command of apache is changed to the rewrite command of nginx, the [l] Mark of apache is replaced with the last mark of nginx, And the content in the middle remains unchanged.
 
If apache rewrite rules are changed to nginx rewrite Rules, use the nginx-t command to check that the nginx. conf configuration file has a syntax error, you can try to add quotation marks to the conditions. For example, the nginx rewrite rule will report a syntax error:
 
Rewrite ^/(%0-9%%5%%%%.html $/x. jsp tutorial? Id = $1 last;
 
The quotation marks are correct:
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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.