nginx rewrite rewrite rules and anti hotlinking configuration method tutorial details
- 2020-05-12 06:51:14
- OfStack
nginx rewrite rewrite rule and anti-hotlinking configuration method, rewrite rule format flag markup in several forms, hotlinking return 403 error, allow the domain name directly after the domain name in line 2.
The nginx rewrite rewrite rule and anti-hotlinking configuration method are as follows:
nginx rewite rules, the official document: http: / / wiki nginx. org/NginxHttpRewriteModule
nginx rewrite rule format: rewrite regex replacement flag
The flag tag comes in four formats:
The copyright of last is equivalent to L in Apache
The break wok terminates Rewirte and no longer matches
The redirect wok returns the temporary redirected HTTP state 302, equivalent to R in Apache
The permanent WSD returns permanent redirected HTTP status 301, equivalent to R=301 in Apache
It can be placed in the server, location and if modules.
Matching judgment:
~ for case-sensitive matching; ! ~ does not match for case sensitivity
~* is case-insensitive matching; ! ~ does not match for case insensitivity
For example, set nginx to redirect users using ie to/nginx-ie:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
Note, commonly used nginx Rewrite rule configuration code.
1. Only use one website, for example, the main website is www.xfcodes.com.
if ($host != 'www.xfcodes.com' ) {
rewrite ^/(.*)$ http://www.xfcodes.com/$1 permanent;
}
When you visit xfcodes.com, it automatically jumps to www.xfcodes.com.
2. Prevent hotlinking
location ~* .(gif|jpg|png|swf|flv)$ {
valid_referers none blocked xfcodes.com dgjs123.com;
if ($invalid_referer) {
return 403;
}
}
When hotlinking returns a 403 error, the allowed domain name can be followed directly by the domain name in line 2.
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;
}
}
This code is currently in use on the code collection.
4. bo-blog is nginx rewrite 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;
}