amy_888 发表于 2016-12-29 06:44:46

nginx 伪静态、裸域名跳转、禁止某个文件或目录被访问、禁止爬虫拔取资源配置

  裸域名跳转配置:
  在server配置中添加:

if ($host != 'www.3gi.cn'){
rewrite    ^/(.*)$ http://www.3gi.cn/$1 permanent;
}
  所有访问该server的主机名不等于指定的www.3gi.cn(当然包括直接3gi.cn),都会跳转到www.3gi.cn去。
  伪静态配置,由于项目wordpress需要伪静态配置,配置如下:
  在需要伪静态的server中添加:

try_files $uri $uri/ /index.php?q=$uri&$args;
  禁止用户访问某个文件或目录使用deny命令

location /doc/ {
deny all;
}

location ~ \.(doc|txt)$ {
root /data/www/htdoc/designer/
deny all;
}
  第一个location配置表示所有doc目录下的访问都应该被禁止。第二个location表示/data/www/htdoc/designer/下面的所有doc |txt文件禁止访问
  许多网站都需要添加防止蜘蛛爬虫拔取自己的资源,下面的配置就是防止资源被拔取,假如我们将我们的静态资源放置到/data/www/resource/static/目录下,并且该资源只有局域网的localhost、 abc.com  、 xxx.com 几台机器访问,那么我们可以做如下处理:

location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blockedlocalhost *.abc.com*.xxx.com;#判断主机是否不是localhot 、abc.com 、xxx.com中的一台
if ($invalid_referer) {
rewrite ^/ http://leech.divmy.com/leech.gif;
return 412;
break;
}
access_log   off; #关闭日志记录功能,确保不会被拔取资源,负责你一直看不到日志,也不知道怎么处理
root /data/www/resource/static; #定义静态资源的根目录
expires 3d;
break;
}
   域名重定向:

    server
{
listen       80;
server_namejump.88dgw.com;
index index.html index.htm index.php;
root/opt/lampp/htdocs/www;
rewrite ^/ http://www.88dgw.com/;
access_logoff;
}
 
页: [1]
查看完整版本: nginx 伪静态、裸域名跳转、禁止某个文件或目录被访问、禁止爬虫拔取资源配置