PHP

On the difference between apache and rewrite of nginx


1. Instructions related to Nginx Rewrite rules

The Nginx Rewrite rules include if, rewrite, set, return, break, etc. rewrite is the most critical instruction. 1 simple Nginx Rewrite rule syntax is as follows:

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

If you add the if statement, here’s an example: if (!-f $request_filename)

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

2. Comparison of Nginx and Apache Rewrite rule instances

The simple Nginx and Apache rewrite rules are not that different and 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 not difficult to see that Apache’s Rewrite rule is changed to Nginx’s Rewrite rule. If you have changed the rule, you can use the “nginx-t” command to check the nginx.conf configuration file and find a syntax error, then you can try to put the condition in quotation marks. For example, the Nginx Rewrite rule under 1 will report grammatical errors:

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

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

The Apache and Nginx Rewrite rules are slightly different when it comes to URL jumps:

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 the substitution string of the Nginx Rewrite rule added “http://$host”, which is required in Nginx.

In addition, Apache and Nginx’s Rewrite rules also differ in terms of 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 corresponding relationship between some instructions and marks of Apache and Nginx Rewrite rules with the same or similar functions: The RewriteCond directive of Apache corresponds to the if directive of Nginx; The RewriteRule directive of Apache corresponds to the rewrite directive of Nginx; 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] tag of Apache corresponds to the last tag of Nginx; The [PT,L] mark of Apache corresponds to the last mark of Nginx;

Allows you to specify the domain name to access this site, jump to the other domain name 1 law 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 directive does not support nesting, nor does AND, OR and other multi-condition matching. Compared with RewriteCond’s Apache, RewriteCond is more troublesome. However, we can write Nginx configuration on the next page to realize 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;

}