Nginx Rewrite rule related instructions
Nginx Rewrite rule related instructions 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;
Comparison of Rewrite rule examples between Nginx and Apache
There is not much difference between simple Nginx and Apache rewrite rules , basically fully compatible. For example:
Apache Rewrite rule:
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;
As can be seen from the above example, Apache’s Rewrite rules are changed to Nginx’s Rewrite rules. It is actually very simple: Apache’s The RewriteRule directive is replaced with Nginx's rewrite directive, Apache's [L] tag is replaced with Nginx's last tag, and the content in the middle remains unchanged.
If you change Apache's Rewrite rules to Nginx's Rewrite rules and use the nginx -t command to check and find that the nginx.conf configuration file has syntax errors, you can try adding quotes to the conditions. 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;
The Rewrite rules of Apache and Nginx have subtle differences 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. This is required in Nginx.
In addition, the Rewrite rules of Apache and Nginx are also different in variable names, for example:
Apache Rewrite rule:
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;
Some functions of Apache and Nginx Rewrite rules Correspondence between the same or similar instructions and tags:
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] mark corresponds to Nginx’s last mark;
Apache’s [R, L] mark corresponds to Nginx’s redirect mark;
Apache’s [P, L] mark corresponds to Nginx’s last mark;
Apache’s [PT, L] mark corresponds to Nginx last tag;
Allow specified domain names 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 multiple condition matching such as AND and OR. Compared with Apache’s RewriteCond, it is more troublesome. However, we can achieve this through the Nginx configuration writing method on the next page. Example:
Nginx Rewrite rule:
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;
}
syntax of rewrite
syntax: rewrite regex replacement flag
Default: none
Scope: server, location, if
This directive changes URI in accordance with the regular expression and the replacement string. Directives are carried out in order of appearance in the configuration file.
This directive changes URI in accordance with the regular expression and the replacement string. Change the URI, or modify the string. The instructions are executed according to the order in the configuration file.
Be aware that the rewrite regex only matches the relative path instead of the absolute URL. If you want to match the hostname, you should use an if condition, like so:
Note that the rewrite regex only works for relative paths. If you want to pair hostnames you should use an if statement.
rewrite will only rewrite the path part and will not change the user's input parameters. Therefore, in the if rule here, you do not need to care about the parameters entered by the user in the browser. They will be added automatically after rewrite. Therefore, we just add Got one? No. and a small parameter we want later, ***https=1.
nginx rewrite rule reference:
Example of combining QeePHP
Convert multiple directories into parameters
abc.domian.com/sort/2 => abc.domian.com/index .php?act=sort&name=abc&id=2
Directory swap
/123456/xxxx -> /xxxx?id=123456
For example, the following setting nginx redirects to the /nginx-ie directory when the user uses ie:
The directory automatically adds "/"
htaccess prohibited
Ban multiple directories
Prohibit files starting with /data
You can ban /data/ Requests such as .log.txt under multi-level directories;
Single directory is prohibited
Cannot prohibit .log.txt requests
Disallow individual files
to favicon .ico and robots.txt set the expiration time;
here is 99 days for favicon.ico, 7 days for robots.txt and no 404 error log is recorded
Set a file The expiration time; here is 600 seconds, and no access log is recorded
File anti-hotlinking and set expiration time
The return 412 here is a custom http status code, the default is 403, which is convenient for finding the correct hotlinking request
"rewrite ^/ http://leech.divmy.com/leech.gif;" Display an anti-hotlink picture
"access_log off;" Do not record access logs, reduce stress
"expires 3d" Browser cache of all files for 3 days
Only allow fixed IP to access the website and add password
Convert files in multi-level directories into one file , enhance seo effect
/job-123-456-789.html points to /job/123/456/789.html
Point a folder in the root directory to the second-level directory
such as /shanghaijob/ points to / area/shanghai/
If you change last to permanent, the browser address bar displays /location/shanghai/
A problem with the above example is that it will not match when accessing /shanghai
In this way, /shanghai can also be accessed, but the relative links in the page cannot Use,
For example, the real address of ./list_1.html is /area/shanghia/list_1.html, which will become /list_1.html, making it inaccessible.
Then it won’t work if I add automatic jump
(-d $request_filename) It has a condition that it must be a real directory, but my rewrite is not, so it has no effect
It will be easier once you know the reason, let me jump manually
Redirect when files and directories do not exist:
Domain name jump
Multiple domain name redirection
Third-level domain name jump
Domain name mirror
Mirror a subdirectory
discuz ucenter home (uchome) rewrite
discuz 7 rewrite
to discuz Configure the domain name separately for the section
discuz ucenter avatar rewrite optimization
jspace rewrite
In addition, there is a tool that can directly convert apache rules into nginx rules
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/
Reference :
http://wiki.nginx.org/NginxChsHttpRewriteModule
http://blog.csdn.net/cnbird2008/archive/2009/08/04/4409620.aspx
http://www.divmy. com/
The above has introduced some things about Nginx configuration file and ReWriteUrl~, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.