Nginx's rewrite rule relies on the Pcre library (Perl compatible regular expression). So in the installation must let Nginx support this feature, as well as install Pcre-devel,prce.
Nginx rewrite regular expression matching
Match case
~ For case-sensitive matching
~* for case-insensitive matching
!~ and!~* are case-sensitive but mismatched and case insensitive but mismatched
^~ URI does not use regular matches and can be used to match meta-characters
File and directory matching
-F and!-f to determine if a file exists
-D and!-d to determine if a directory exists
-E and!-e to determine if a file or directory exists
-X and!-x to determine if a file is executable
Flag Flag
Last time this rewrite is complete, restart the next check a[test]->a[test], note if the URI of the rewrite rule and the URI in the location of this paragraph are duplicated, be careful to avoid a dead loop and return a 500 error after 10 cycles.
Break terminates the match and no longer matches the contents of this paragraph. A[test]->b[test] completed!
REDIRECT Returns 302 the temporary redirect Address bar displays the address after the jump.
Permanent returns 301 the Permanent redirect Address bar displays the address after the jump.
Examples of break and last differences:
####
server {
...
Rewrite ^ (/download/.*)/media/(. *) \. *$ $1/mp3/$2.mp3 last;
Rewrite ^ (/download/.*)/audio/(. *) \. *$ $1/mp3/$2.ra last;
return 403; #本段中location中匹配的内容和重写规则中没有重复的
...
}
But if these directives is put inside the "/download/" location, the last flag should is replaced by break, or otherwise Nginx would make cycles and return the error:
location/download/{
Rewrite ^ (/download/.*)/media/(. *) \. *$ $1/mp3/$2.mp3 break;
Rewrite ^ (/download/.*)/audio/(. *) \. *$ $1/mp3/$2.ra break;
return 403;
}
#####
if specified using: The format is the same as the C language,
if (condition) {
Rewrite ...
}
Note that if this does not support nesting, it does not support && and | | Logical judgment
set Use : Set var $avg
rewrite_log: Default off, if on, the notic level is logged to Error.log
Some examples:
- HTTP goto https:rewrite ^ (. *) https://www.wxl.com$1 permanent;
- Domain jump, transfer bbsX.wxl.com to http://www.baidu.com
Location/{
root HTML;
Index index.html index.htm;
if ($host ~ ' ^bbs\d{1}\.wxl\.com ') {
Rewrite ^ (. *) http://www.baidu.com$1 permanent;
}
}
3. file read/write separation: File Upload WebDAV, based on the http1.1 protocol, extended the http1.1, other functions in Get,post,head, while we are experimenting, you can use Apache to open DAV on
Location/{
Proxy_pass Http://read_upstream;
if ($request _method = "PUT") {
Proxy_pass Http://write_upsteam;
}
}
4. The directory is automatically added/, this function is usually done automatically by the browser. If you use index.html in a level two directory, such as/data/bbs/index.html, if the input/data/bbs browser does not jump to the main page index.html
Location ^~/bbs {
if (-D $request _filename) {
Rewrite ^/(. *) ([^/]) $/HTTP $host/$1$2/permanent;
}
}
5. Catalog Merge
If there are many directories in the URI, it will not be conducive to search engines, by overriding the rules can make the URI directory look very small.
Rewrite ^/server-([0-9]+)/-([0-9]+)/-([0-9]+)/-([0-9]+]/\.html$/server/$1/$2/$3/$4.html last;
Break
So the address bar enters Http://www.wxl-dede.com/server-12-12-12-12.html,
The actual access is http://www.wxl-dede.com/server/12/12/12/12.html
6. Disable access to a directory
Location ~ ^/(cron|templates)/{
Deny all; Break
}
7. Access different content for different browsers
if ($http _user_agent ~ Chrome) {
Rewrite ^ (. *) $/data/index.html break;
}
8. Customizing the shelf life time of a file of a certain type
Location ~* \. (js|css|jpg|jpeg|gif|png|swf) $ {
if (-f $request _filename) {
Expires 1h;
Break
}
}
Nginx Anti-theft chain
The concept of hotlinking, its own site does not exist resources, links to other sites of resources. But for other sites will cause additional consumption, unfair.
In the request header of HTTP Refrer the value in the Head field records the source address of the access target, so in nginx you can prevent the anti-theft chain by making a valid URL.
Syntax: Valid_referers None | Blocked | Server_names | String ...;
None Source Header empty case
Blocked firewall has removed HTTP.//or https://
Server_names set up specific hosts, support matches, support regular
Example: resource-based anti-theft chain
Location ~* ^.+\. (JPG|JPEG|GIF|PNG|SWF|RAR|ZIP|CSS|JS) $ {
Valid_referers none blocked *.wxl.com;
if ($invalid _referer) {
return 403;
Rewrite ^/http://img.jjonline.cn/forbid.gif;
Break
}
Jpg,jpeg and other resources to prevent hotlinking.
Of course it can also be based on directory
root/server/file/;
Valid_referers none blocked *.wxl.com;
if ($invalid _referer) {
return 403;
Rewrite ^/http://img.jjonline.cn/forbid.gif;
Break
}
Nginx Rewrite rules