Nginx rewrite and anti-leech configuration, nginxrewrite

Source: Internet
Author: User

Nginx rewrite and anti-leech configuration, nginxrewrite
Nginx rewrite Rules and anti-leech Configuration

Time: 15:16:58 Source: Network
Guide: nginx rewrite Rules and anti-leech configuration methods. In the rewrite rule format, the flag is in several forms. During leeching, error 403 is returned. The allowed domain names follow the domain names in the second line. Nginx rewrite Rules and anti-leech Configuration

Nginx rewite rules, official documentation: http://wiki.nginx.org/NginxHttpRewriteModule

Nginx rewrite rule format: rewrite regex replacement flag

There are four flag formats:
 

Last-equivalent to L in Apache
Break-Abort Rewirte and do not continue matching
Redirect-returns the HTTP status 302 of the temporary redirection, which is equivalent to the R
Permanent-returns the 301 HTTP status for permanent redirection, which is equivalent to R = 301 in Apache
 

It can be placed in the server, location, and if modules.

Matching judgment:
~ It is case-sensitive ;!~ Case-insensitive
~ * Case-insensitive match ;!~ Case-insensitive

For example, set nginx to redirect to the/nginx-ie directory when you use ie:
 

If ($ http_user_agent ~ MSIE ){
Rewrite ^ (. *) $/msie/$1 break;
}

Appendix, common nginx Rewrite rule configuration code.

1. Use only one web site. For example, set the main web site to www.xfcodes.com.
 

If ($ host! = 'Www .xfcodes.com '){
Rewrite ^/(. *) $ http://www.xfcodes.com/#1 permanent;
}
 

When you access xfcodes.com, it will automatically jump to www.xfcodes.com.

2. Anti-leech
 

Location ~ *. (Gif & #124; jpg & #124; png & #124; swf & #124; flv) $ {
Valid_referers none blocked xfcodes.com dgjs123.com;
If ($ invalid_referer ){
Return 403;
}
}
 

The error 403 is returned during leeching. The allowed domain name can be directly followed by the domain name in the second line.

3. Rewrite of WordPress
 

Location /{
Index index.html index. php;
If (-f $ request_filename/index.html ){
Rewrite (. *) $1/index.html break;
}
If (-f $ request_filename/index. php ){
Rewrite (. *) $1/index. php;
}
If (! -F $ request_filename ){
Rewrite (. *)/index. php;
}
}
 

Currently, this code is used in the Code favorites.

4. bo-blog nginx rewrite rules under nginx
 

If (! -E $ request_filename ){
Rewrite ^/post/([0-9] + )/? ([0-9] + )? /? ([0-9] + )? /? $/Read. php? Entryid = $1 & page = $2 & part = $3 last;
Rewrite ^/page/([0-9] +)/([0-9] + )/? $/Index. php? Mode = $1 & page = $2 last;
Rewrite ^/starred/([0-9] + )/? ([0-9] + )? /? $/Star. php? Mode = $1 & page = $2 last;
Rewrite ^/category/([^/] + )/? ([0-9] + )? /? ([0-9] + )? /? $/Index. php? Go = category _ $1 & mode = $2 & page = $3 last;
Rewrite ^/archiver/([0-9] +)/([0-9] + )/? ([0-9] + )? /? ([0-9] + )? /? $/Index. php? Go = archive & cm = $1 & cy = $2 & mode = $3 & page = $4 last;
Rewrite ^/date/([0-9] +)/([0-9] +)/([0-9] + )/? ([0-9] + )? /? ([0-9] + )? /? $/Index. php? Go = showday _ $1-$2-$3 & mode = $4 & page = $5 last;
Rewrite ^/user/([0-9] + )/? $/View. php? Go = user _ $1 last;
Rewrite ^/tags/([^/] + )/? ([0-9] + )? /? ([0-9] + )? /? $/Tag. php? Tag = $1 & mode = $2 & page = $3 last;
Rewrite ^/component/id/([0-9] + )/? $/Page. php? Pageid = $1 last;
Rewrite ^/component/([^/] + )/? $/Page. php? Pagealias = $1 last;

# Force redirection for old rules
Rewrite ^/read \. php/([0-9] +) \. htm $ http: // $ host/post/$1/permanent;
Rewrite ^/post/([0-9] +) \. htm $ http: // $ host/post/$1/permanent;
Rewrite ^/post/([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/post/$1/$2/permanent;
Rewrite ^/post/([0-9] +) \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/post/$1/$2/$3/permanent;
Rewrite ^/index \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/page/$1/$2/permanent;
Rewrite ^/star \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/starred/$1/$2/permanent;
Rewrite ^/category \ _ ([0-9] +) \. htm $ http: // $ host/category/$1/permanent;
Rewrite ^/category \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/category/$1/$2/$3/permanent;
Rewrite ^/archive \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/archiver/$1/$2/permanent;
Rewrite ^/archive \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/archiver/$1/$2/$3/$4/permanent;
Rewrite ^/showday \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/date/$1/$2/$3/permanent;
Rewrite ^/showday \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] +) \ _ ([0-9] + )\. htm $ http: // $ host/date/$1/$2/$3/$4/$5/permanent;

# Filename alias
Rewrite ^/([a-zA-Z0-9 _-] + )/? ([0-9] + )? /? ([0-9] + )? /? $/Read. php? Blogalias = $1 & page = $2 & part = $3 last;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.