nginx rewrite (nginx URL address rewrite)
Rewrite main function is to implement the URL rewrite, nginx Rewrite rules Pcre,perl compatible Regular expression syntax rules matching, if you need nginx Rewrite function, before compiling nginx, need to compile and install Pcre Library. Through the rewrite rule, you can implement the URL of the specification, and make the URL steering and select configuration according to the variables. If directive
Rule syntax:
[Plain]View PlainCopy
- if ($http _user_agent ~msie) {
- Rewrite ^ (. *) $/msie/$1 break;
- }
- if (!-f$request_filename) {
- Rewrite ^/img/(. *) $/site/$host/images/$1 last;
- }
Rewrite syntax rules:
Variable name:
Variable names can use the "=" or "! =" operator
~ symbol indicates case-sensitive letter matching
The ~* symbol indicates a case-insensitive letter match
!~ and!~ are the opposite of ~!~
-F and!-f to determine if a file exists
-D and!-d to determine if a directory exists
-E and!-e to determine whether a file or directory exists
-X and!-x to determine if a file can be executed
Also supports the $ to $9 positional parameter
return instruction
Example: If you access a URL that ends with a. Sh. Bash, return a status code of 403
[Plain]View PlainCopy
- Location ~. *\. (Sh|bash)? $
- {
- return 403;
- }
Rewrite directive
[Plain]View PlainCopy
- The last parameter of the rewrite directive is the flag flag, and the supported flag flags are mainly in the following categories:
- Last: Equivalent to Apache Reed (L) mark, indicating completion of rewrite;
- break; After the rule match is complete, the match is terminated and no longer matches the following rule
- Redirect: Returns 302 temporary redirect, the browser address will show the URL address after the jump
- Permanent: Return 301 Permanent Redirect, the browser address bar will show the URL address after the jump
- Last and break are used to implement URL rewriting, browser address bar URL address unchanged
Example: the access/b jumps to the/bbs directory:
[Plain]View PlainCopy
- location/b {
- AutoIndex on;
- Alias/usr/local/nginx/html/redhat;
- Rewrite ^/b/?$/bbs permanent;
- }
- Location/bbs {
- AutoIndex on;
- Alias/usr/local/nginx/html/bbs;
- }
Rewrite rule Writing instance
1, rewrite the original directory to access/b to/bbs
Core statement:
[Plain]View PlainCopy
- Rewrite ^/b/?$/bbs permannet;
2, depending on the browser will be different results.
[Plain]View PlainCopy
- if ($http _user_agent ~ Firefox) {
- Rewrite ^ (. *) $/firefox/$1 break;
- }
- if ($http _user_agent ~ MSIE) {
- Rewrite ^ (. *) $/msie/$1 break;
- }
- if ($http _user_agent ~ Chrome) {
- Rewrite ^ (. *) $/chrome/$1 break;
- }
3. Prevent hotlinking: According to referer information to prevent hotlinking, the code is as follows:
[Plain]View PlainCopy
- Location ~*\. (gif|jpg|png|swf|flv) ${
- Valid_referers none blocked www.cheng.com*.test.com;
- if ($invalid _referer)
- Rewrite ^/(. *) http://www.cheng.com/error.html
- }
4. Implementing the Domain name jump:
[Plain]View PlainCopy
- server {
- Listen 80;
- server_name cheng.example.com;
- Write ^ (. *) $ http://zhang.example.com/$1 permanent;
- Location/{
- root HTML;
- Index index.html index.htm;
- }
http://blog.csdn.net/xifeijian/article/details/20955253
On the rules of Nginx rewrite (turn)