Nginx rewrite rewrite rules and anti-theft chain configuration method Tutorial Detailed _nginx

Source: Internet
Author: User

Introduction: Nginx rewrite rewrite rules and anti-theft chain configuration method, rewrite rule format flag tags in several forms, hotlinking return 403 error, allow the domain name directly following the second row of the domain name.

Nginx rewrite rewrite rules and anti-theft chain configuration methods are as follows:

Nginx Rewite Rules, official documents: Http://wiki.nginx.org/NginxHttpRewriteModule

Nginx rewrite rule format: rewrite regex replacement flag

Flag tags are available in four different formats:

last– is equivalent to L in Apache.
break– abort Rewirte, do not continue to match
redirect– returns the temporarily redirected HTTP status 302, equivalent to the R in Apache
permanent– returns HTTP status 301 for permanent redirection, equivalent to r=301 in Apache

Can be placed in the server, location, and if modules.

Match judgment:

~ to match case sensitivity;!~ does not match case sensitivity
~* matches case-insensitive;!~ does not match case insensitive

For example, the setting Nginx is redirected to the/nginx-ie directory using the user's use of IE:

if ($http _user_agent ~ msie) {
rewrite ^ (. *) $/msie/$1 break;
}

Attached, common nginx Rewrite rule configuration code.

1, only use a Web site, such as the main website set to www.xfcodes.com.

if ($host!= ' www.xfcodes.com ') {
rewrite ^/(. *) $ http://www.xfcodes.com/$1 permanent;
}

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

2, anti-theft chain

Location ~*. (gif|jpg|png|swf|flv) $ {
Valid_referers none blocked xfcodes.com dgjs123.com;
if ($invalid _referer) {return
403
}}

The hotlinking returns 403 errors, allowing the domain name to be followed directly by the domain name of the second row.

3, the 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 collection.

4.bo-blog under Nginx nginx rewrite rules

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; 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; }

The above is a small set to introduce the Nginx rewrite rewrite rules and anti-theft chain configuration method Tutorial detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.