send_timeout 10:指定响应客户端的超时时间,这个超时仅限于两个阅读活动之间的时间,如果这个时间后客户端没有任何活动,Nginx将会关闭连接。
9、控制并发连接
你可以使用NginxHttpLimitZone模块限制指定会话,或某个IP的并发连接数,编辑nginx.conf:
### Directive describes the zone, in which the session states are stored i.e. store in slimits.
### 1m can handle 32000 sessions with 32 bytes/session, set to 5m x 32000 session ###limit_zone slimits $binary_remote_addr 5m;### Control maximum number of simultaneous connections for one session i.e. ###
### restricts the amount of connections from a single ip address ###
limit_conn slimits 5;
上述设置可以限制远程客户端每IP地址不能超过5个同时打开的连接。
10、只允许访问指定的域名
如果有机器人程序在随机扫描所有域,那就阻止它访问,你必须配置只允许虚拟域或反向代理请求。
## Only requests to our Host are allowed i.e. nixcraft.in, images.nixcraft.in and www.nixcraft.in
if ($host !~ ^(nixcraft.in|www.nixcraft.in|images.nixcraft.in)$ ) {
return 444; }
##
11、限制可用的方法
GET和POST是互联网上最常用的方法,RFC 2616定义了Web服务器可用的方法,如果一个Web服务器不要求实现所有方法,那些方法就应该被禁止掉,下面的代码将过滤所有方法,只允许GET,HEAD和POST方法:
## Only allow these request methods ##
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444; }
## Do not accept DELETE, SEARCH and other methods ##
关于HTTP方法的更多信息:
GET方法用于请求文档,如http://www.cyberciti.biz/index.php。
HEAD方法与GET相同,但服务器不会在响应中只返回消息主体。
POST方法功能就多了,如通过表单存储或更新数据,订购一个产品,发送电子邮件等,通常使用服务器端脚本(如PHP,Perl,Python等)处理,如果你要上传文件或在服务器上处理表单就必须用它。
12a、如何阻止某些用户代理(User-Agents)?
你可以轻松阻止用户代理,如扫描器,机器人和垃圾邮件,它们可能会滥用你的服务器。
## Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
return 403; }
##
阻止msnbot和scrapbot机器人:
## Block some robots ##
if ($http_user_agent ~* msnbot|scrapbot) {
return 403; }
12b、如何阻止被提名的垃圾邮件
被提名的垃圾邮件都很危险,它们可能会损害你的SEO排名,可以使用下面的代码阻止访问垃圾邮件发送者:
## Deny certain Referers ###
if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) )
{
# return 404;
return 403; }
##
13、如何停止图片热链
图片或HTML热链是指有人在他们的网站上引用了你网站的图片,你必须为其它网站的流量支付贷款费用,有点象是网站劫持,通常这种情况发生在博
客和论坛中,我强烈建议你在服务器级停止并阻止图片热链。
# Stop deep linking or hot linking
location /images/ { valid_referers none blocked www.example.com example.com;
if ($invalid_referer) {
return 403; }
}
例子:重写并显示禁令图片:
valid_referers blocked www.example.com example.com;
if ($invalid_referer) { rewrite ^/images/uploads.*\.(gif|jpg|jpeg|png)$
http://www.examples.com/banned.jpg last
}
另外,请参考“How-to:使用Nginx映射阻止图片热链”(http://nginx.org/pipermail/nginx/2007-June/001082.html)。
14、目录限制
你可以为特定目录设置访问控制,所有网页目录都应配置为按需访问。
通过IP地址限制访问,你可以限制访问/docs/目录的IP地址:
location /docs/ { ## block one workstation deny 192.168.1.1;
## allow anyone in 192.168.1.0/24 allow 192.168.1.0/24;
## drop rest of the world deny all;
}
通过密码保护目录,首先创建一个密码文件,再添加一个用户vivek:
# mkdir /usr/local/nginx/conf/.htpasswd/
# htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd vivek
编辑nginx.conf添加需要保护的目录:
### Password Protect /personal-images/ and /delta/ directories ###
location ~ /(personal-images/.*|delta/.*) { auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd/passwd;
}
您看到此篇文章时的感受是: