禁止conf后缀的文件
location ~* \.conf$ {
return 404;
}
禁止htaccess
location ~//.ht {
deny all;
#或 return 404;
}
禁止目录
location ~ ^/(cron|templates)/ {
deny all;
break;
}
禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;
location ~ ^/data {
deny all;
}
禁止单个目录
不能禁止.log.txt能请求
location /searchword/cron/ {
deny all;
}
禁止单个文件
location ~ /data/sql/data.sql {
deny all;
}
给favicon.ico和robots.txt设置过期时间;
favicon.ico为99天,robots.txt为7天,并且不记录404错误日志
location ~(favicon.ico) {
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}
设定某个文件的过期时间;这里为600秒,并不记录访问日志
location ^~ /html/scripts/loadhead_1.js {
access_log off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}
文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://www.i-miqi.net/d.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存
location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.i-miqi.net *.i-miqi.com localhost 1.1.1.1;
if ($invalid_referer) {
rewrite ^/ http://www.i-miqi.net/d.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
只允许固定ip访问网站,并加上密码
root /opt/htdocs/www;
allow 208.97.167.194;
allow 222.33.1.2;
allow 231.152.49.4;
deny all;
auth_basic “C1G_ADMIN”;
auth_basic_user_file htpasswd;
将多级目录转成文件形式,seo友好
/i-123-456-789.html 指向/i/123/456/789.html
rewrite ^/i-([0-9]+)-([0-9]+)-([0-9]+)/.html$ /i/$1/$2/show_$3.html last;
将根目录下某个文件夹指向2级目录
如/a/ 指向 /ab/df/
如果你将last改成permanent,那么浏览器地址栏显是/location/a/
rewrite ^/([0-9a-z]+)show$ /$1show/ permanent;
rewrite ^/([0-9a-z]+)show/(.*)$ /ab/$1/$2 last;
文件和目录不存在的时候重定向:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}
域名跳转
server
{
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.i-miqi.net/;
access_log off;
}
多域名转向
server_name www.aaa.com www.aaa.net;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ “aaa/.net”) {
rewrite ^(.*) http://www.i-miqi.net$1 permanent;
}
三级域名跳转
if ($http_host ~* “^(.*)/.i/.aaa/.com$”) {
rewrite ^(.*) http://www.i-miqi.net$1;
break;
}
域名镜像
server
{
listen 80;
server_name mirror.aaa.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/(.*) http://www.i-miqi.net/$1 last;
access_log off;
}
某个子目录作镜像
location ^~ /php {
rewrite ^.+ http://www.i-miqi.net/ last;
break;
}