nginx中的限制
1.限制请求数:
每次修改完配置文件,都必须刷新nginx
nginx -s reload
vim /etc/local/lnmp/nginx/conf/nginx.conf
#gzip on;
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
location / {
root html;
index index.html index.htm;
}
location /download/ {
limit_conn addr 1; # 限制连接数为1
#limit_rate 50k; # 限制速率50K
limit_req zone=one burst=5; # 只响应一个组的请求
}
验证:主机中进行压力测试:
同一个请求10次:
所有的请求都得到了正确的回应(返回值为200)
10个请求10次:
请求中只有一个得到了正确的返回值(200)
速率限制最大为50KB
2.访问限制:
vim /usr/local/lnmp/nginx/conf/nginx.conf
location /admin/ {
#allow 172.25.14.0/24;
deny all;
拒绝所有访问:
报403的错误,访问被拒绝
写入allow策略时,会只允许策略中所含的ip通过。
3.浏览器访问还有中文的界面
vim /usr/local/lnmp/nginx/conf/nginx.conf
server {
listen 80;
server_name _;
# return 500;
charset koi8-r;
4.日志备份
vim /usr/local/lnmp/nginx/conf/nginx.conf
location ~ .*\.(gif|jpg|png|js|css)$ {
expires 30d; # 可以缓存的时间
access_log off;
}
日志备份中
vim /opt/nginx_log.sh
cd /usr/local/lnmp/nginx/logs/ && mv access.log_$(date +%F -d -1day)
/usr/local/lnmp/nginx/sbin/nginx -s reload # 刷新nginx,mv之后access.log会消失,刷新之后会重新生成新的文件。
crontab -e
00 00 * * * /opt/nginx_log.sh
chmod +x /opt/nginx_log.sh
5.防盗链
模拟盗链
在server6中安装httpd,在发布文件中写
vim index.html
<html>
<body>
<img src="http://www.westos.org/images/iso7.gif">
</body>
</html>
浏览器中访问172.25.11.6/image/iso7.gif
或者访问server6的域名也可以出现如下界面
书写nginx的配置文件:
vim /usr/local/lnmp/nginx/conf/nginx.conf
location / {
root /www1;
index index.html;
}
location ~ \.(gif|jpg|png)$ {
root /www1;
valid_referers none blocked www.westos.org;
if ($invalid_referer) {
# return 403; # 当访问的域名不是www.westos.org时,返回403错误
rewrite ^/ http://bbs.westos.org/daolian.jpg; # 将盗链的域名重新定向到自己想要返回的文件上。
}
}
}
server {
listen 80;
server_name bbs.westos.org;
# rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
location / {
root /www2;
index index.html;
}
}
浏览器中再次输入:daolian.westos.org (注意清理浏览器缓存)