Home >Backend Development >PHP Tutorial >A brief discussion on the difference between apache and nginx rewrite_PHP tutorial

A brief discussion on the difference between apache and nginx rewrite_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:13:07745browse

1. Nginx Rewrite rule related instructions

The instructions related to Nginx Rewrite rules include if, rewrite, set, return, break, etc., among which rewrite is the most critical instruction. A simple Nginx Rewrite rule syntax is as follows:

rewrite ^/b/(.*).html /play.php?video=$1 break;

If you add an if statement, the example is as follows:
if (!-f $request_filename)

{ rewrite ^/img/(.*)$ /site/$host/images/$1 last; }

2. Comparison of Rewrite rule examples between Nginx and Apache

There is not much difference between simple Nginx and Apache rewrite rules, and they are basically fully 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 not difficult to find that it is quite simple to change Apache's Rewrite rules to Nginx's Rewrite rules. After changing the rules, use the "nginx -t" command to check and find that the nginx.conf configuration file has syntax errors, then you can try to add conditions quotation marks. For example, the following Nginx Rewrite rule will report a syntax error:

rewrite ^/([0-9]{5}).html$ /x.jsp?id=$1 last; add quotation marks and it will be correct:

rewrite “^/([0-9]{5}).html$” /x.jsp?id=$1 last;

There are subtle differences between the Rewrite rules of Apache and Nginx when URL jumps:

Apache Rewrite rules:

RewriteRule ^/html/tagindex/([a-zA-Z]+)/.*$ /$1/ [R=301,L]
Nginx Rewrite Rule:

rewrite ^/html/tagindex/([a-zA-Z]+)/.*$ http://$host/$1/ permanent;


In the above example, we noticed that "http://$host" was added to the replacement string of the Nginx Rewrite rule, which is required in Nginx.

In addition, the Rewrite rules of Apache and Nginx are also different 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;
The correspondence between some instructions and tags with the same or similar functions between Apache and Nginx Rewrite rules :
Apache's RewriteCond instruction corresponds to Nginx's if instruction;
Apache's RewriteRule instruction corresponds to Nginx's rewrite instruction;
Apache's [R] tag corresponds to Nginx's redirect tag;
Apache's [P] The tag corresponds to Nginx's last tag;
Apache's [R, L] tag corresponds to Nginx's redirect tag;
Apache's [P, L] tag corresponds to Nginx's last tag;
Apache's [PT, L ] tag corresponds to Nginx’s last tag;

Specified domain names are allowed to access this site, other domain names will jump to 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 instruction does not support nesting, nor does it support multi-condition matching such as AND and OR. Compared with Apache's RewriteCond seems a little more troublesome, but we can implement this example through the Nginx configuration writing method on the next page:
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;

}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326583.htmlTechArticle1. Nginx Rewrite rule related instructions Nginx Rewrite rule related instructions include if, rewrite, set, return, break, etc. Among them, rewrite is the most critical instruction. A simple Nginx Rewrite rule...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn