web优化之nginx优化<六>

        

   这里我们接着web之nginx优化<->继续分析nginx优化之资源防盗链,方爬虫,控制目录权限、CDN加速重要性及方法。


12.资源防盗链

例如:资源在优酷视频,但显示在我自己的博客上,会增加优酷的带宽,但是访问量在我的博客;会节省我的资源。

  1)根据http referer 实现防盗链

      通过referer可以检测目标访问的来源网页,如果是资源文件,可以跟踪到显示它的网页地址。


wKioL1YrVzTiFOxKABJUiM8Pje0879.bmp


  2)其他的防盗链方案:

    ・cookie防盗链;

    ・加密URL路径,定时失效;

    ・lighttpd有类似的插件mod_secdownload,先在服务端配置此模块,设置一个固定用于加密的字符串,比如Yvonne,然后设置一个url前缀,比如/mp4/,再设置一个过期时间,比如1小时,然后在通过一段PHP代码,利用加密字符串和系统时间等通过md5算法加密生成一个加密字符串,最终获取到的文件的url链接中会带有一个时间戳和一个加密的MD5数值,当访问时系统会对这两个数据进行验证,如果时间不在预期的时间段内(如1小时)则失效;如果时间戳符合条件,但是加密的字符串不符合条件也失效,从而达到防盗链的效果。

  3)实现防盗链:

location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
 valid_referers none blocked *.etiantian.org etiantian.org; 
if ($invalid_referer) { 
rewrite ^/ 
http://www.etiantian.org/img/nolink.jpg; 
} 
}


server {
listen 80 default_server;
server_name _;
return 501;
}
    server {
        listen       80;
        root   html/www;
        server_name  
www.etiantian.org;
        location / {
            index  index.html index.htm;
        }
location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
 valid_referers none blocked *.etiantian.org etiantian.org; 
if ($invalid_referer) { 
rewrite ^/ 
http://bbs.etiantian.org/img/nolink.jpg; 
} 
}
    access_log  logs/www-access.log  main;
    }
    server {
        listen       80;
        root   html/dao;
        server_name  
www.daolian.com;
        location / {
            index  index.html index.htm;
        }
access_log  logs/dao-access.log  main;
    }
    server {
        listen       80;
        root   html/bbs;
        server_name  bbs.etiantian.org;
        location / {
            index  index.html index.htm;
        }
access_log  logs/bbs-access.log  main;
    }
}

    在上面这段防盗链设置中,分别针对不同文件类型和不同的目录进行了设置,读者可以根据自己的需求进行类似的设定。

・"jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar"表示对以jpg、gif、png、swf、flv、wma、wmv、asf、mp3、mmf、zip、rar为后缀的文件实行防盗链处理;

・"*.etiantian.org etiantian.org"表示这个请求可以正常访问上面指定的文件资源;

・if{}中的内容的意思是:如果地址不是上面指定的地址就跳转到通过rewrite指定的地址,也可以直接通过return返回403错误;

・return 403 为自定义的http返回状态码;

・以下内容表示显示一张防盗链图片:

rewrite ^/ 
http://bbs.etiantian.org/img/nolink.jpg;

・access_log off:不记录访问日志,减轻压力;

・expires 3d 所有文件3天的浏览器缓存;

location /images { 
root /data0/www/www;
  valid_referers none blocked *.etiantian.org etiantian.org; 
if ($invalid_referer){ 
return 403; 
} 
}

阿里门户天猫网站的nginx优雅显示配置案例如下:

error_page   500 501 502 503 504  http://err.tmall.com/error2.html;
error_page 400 403 404 405 408 410 411 412 413 414 415 http://err.tmall.com/error1.html;


13.站点页面权限控制

服务器角色 权限处理 安全系数
动态web集群

目录权限755,文件权限644

所有目录和文件属组和属主都是root

环境nginx+php

文件不能被改;
目录不能被写入;
安全系数10
static图片集群 目录权限是755,文件权限644;
所有目录和文件属组和属主都是root
环境nginx;
文件不能被改;
目录不能被写入;
安全系数10
上传upload集群 目录权限755,文件权限644;
所有目录和文件属组和属主都是root
特别:
用户上传的目录设置为755,用户使用nginx服务的用户
文件不能被改,目录不能被写入;
特别:
用户上传的目录可以允许写入
(前面通过nginx会禁止读文件),
安全系数8
14.防爬虫


1)先来看看何为Robots协议?

    Robots协议,又称机器人协议或爬虫协议,该协议就搜索引擎抓取网站内容的范围作了约定,包括网站是否希望被搜索引擎抓取,哪些内容不允许被抓取,网络爬虫据此“自觉地”抓取或者不抓取该网页内容。自推出以来Robots协议已成为网站保护自有敏感数据和网民隐私的国际惯例。

小case: http://tech.qq.com/a/20131016/017362.htm2

2)防爬虫

if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot") { 
     return 403; 
}
#Only allow these request methods
     if ($request_method !~ ^(GET|HEAD|POST)$ ) {
         return 501;
     }
#Do not accept DELETE, SEARCH and other methods

HTTP协议中GET、POST和HEAD的介绍:
GET: 请求指定的页面信息,并返回实体主体。
HEAD: 只请求页面的首部。
POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体。

15.Linux内核优化:

# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000    65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。
##net.nf_conntrack_max = 25000000
##net.netfilter.nf_conntrack_max = 25000000
##net.netfilter.nf_conntrack_tcp_timeout_established = 180
##net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
#writen by troy#
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time =600
net.ipv4.ip_local_port_range = 4000    65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#for iptables
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120

16.用CDN做网站加速

   1)CDN简介

    ・CDN是一款用于静态页面的分布式缓存。

    ・CDN的价值:

      a.为企业架构省钱;

      b.提升用户访问网站体验;

      c.可以阻挡大部分流量攻击,如DOS;

    ・企业使用CDN的基本要求[待补充]


更多优化见 http://7826443.blog.51cto.com/7816443/1705051

你可能感兴趣的:(Web,服务端,cookie,时间段,优酷视频,访问量)