四、Nginx Redirect
将所有linuxtone.org与netseek.linuxtone.org域名全部自跳转到
server
{
listen 80;
server_name linuxtone.org netseek.linuxtone.org;
index index.html index.php;
root /data/www/wwwroot;
if ($host !~ "^www\.linxtone\.org$") {
rewrite ^(.*) [ redirect;
}
........................
}
五、Nginx 目录自动加斜线:
if (-d $request_filename){
rewrite ^/(.*)([^/])$
http://$host/$1$2/ permanent;
}
六、Nginx Location
1.基本语法:[和上面rewrite正则匹配语法基本一致]
location [=|~|~*|^~] /uri/ { … }
* ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
示例1:
location = / {
# matches the query / only. # 只匹配 / 查询。
}
匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配
示例2:
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
}
# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
示例3:
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
}
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。
七、Nginx expires
1.根据文件类型expires
# Add expires header for static content
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /data/www/wwwroot/bbs;
expires 1d;
break;
}
}
2、根据判断某个目录
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /data/www/wwwroot/down;
expires 30d;
}
八、Nginx 防盗链
1.针对不同的文件类型
#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.linuxtone.org linuxtone.org
http://localhost baidu.com;
if ($invalid_referer) {
rewrite ^/
;
# return 403;
}
}
2.针对不同的目录
location /img/ {
root /data/www/wwwroot/bbs/img/;
valid_referers none blocked server_names *.linuxtone.org
http://localhost baidu.com;
if ($invalid_referer) {
rewrite ^/
;
#return 403;
}
}
3.同实现防盗链和expires的方法
#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.linuxtone.org linuxtone.org
http://localhost ;
if ($invalid_referer) {
rewrite ^/
;
}
access_log off;
root /data/www/wwwroot/bbs;
expires 1d;
break;
}