Nginx
九、Nginx安全
## nginx屏蔽ip 采集和防止采集是一个经久不息的话题,一方面都想搞别人的东西,另一方面不想自己的东西被别人搞走。 本文介绍如何利用nginx屏蔽ip来实现防止采集,当然也可以通过iptable来实现。 ##### 1.查找要屏蔽的ip awk '{print $1}' nginx.access.log |sort |uniq -c|sort -n nginx.access.log 为日志文件, ##### 2. 加入ip文件 在nginx的安装目录下面,新建屏蔽ip文件,命名为blockip.conf,以后新增加屏蔽ip只需编辑这个文件即可。 加入如下内容保存。 ```shell deny 165.91.122.67; ``` ##### 3. 引入加入ip文件 在nginx的配置文件nginx.conf中加入如下配置,可以放到http, server, location, limit_except语句块,需要注意相对路径,本例当中nginx.conf,blocksip.conf在同一个目录中。 ```shell include blockip.conf; ``` ##### 4.重启一下nginx的服务 就可以生效了 ```shell /usr/local/nginx/nginx -s reload ``` ##### 高级用法: 屏蔽ip的配置文件既可以屏蔽单个ip,也可以屏蔽ip段,或者只允许某个ip或者某个ip段访问。 - 单个ip访问 deny IP; - 允许单个ip访问 allow IP; - 屏蔽所有ip访问 deny all; - 允许所有ip访问 allow all; - 屏蔽整个段即从123.0.0.1到123.255.255.254访问的命令 deny 123.0.0.0/8 - 屏蔽IP段即从123.45.0.1到123.45.255.254访问的命令 deny 124.45.0.0/16 - 屏蔽IP段即从123.45.6.1到123.45.6.254访问的命令 deny 123.45.6.0/24 - 如果你想实现这样的应用,除了几个IP外,其他全部拒绝,那需要你在blockip.conf中这样写 allow 1.1.1.1; allow 1.1.1.2; deny all; - 单独网站屏蔽IP的方法,把include blocksip.conf; 放到网址对应的在server{}语句块,所有网站屏蔽IP的方法,把include blocksip.conf; 放到http {}语句块。 ## Nginx 防盗链 - 针对不同的文件类型 ```shell #Preventing hot linking of images and other file types location ~* ^.+.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost baidu.com; if ($invalid_referer) { rewrite ^/ http://www.linuxtone.org/images/default/logo.gif; # return 403; } } ``` - 针对不同的目录 ```shell location /img/ { root /data/www/wwwroot/bbs/img/; valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com; if ($invalid_referer) { rewrite ^/ http://www.linuxtone.org/images/default/logo.gif; #return 403; } } ``` - 同实现防盗链和expires的方法 ```shell #Preventing hot linking of images and other file types location ~* ^.+.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost ; if ($invalid_referer) { rewrite ^/ http://www.linuxtone.org/images/default/logo.gif; } access_log off; root /data/www/wwwroot/bbs; expires 1d; break; } ``` ## Nginx 访问控制 - Nginx 身份证验证 ```shell #cd /usr/local/nginx/conf #mkdir htpasswd /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone #添加用户名为linuxtone New password: (此处输入你的密码) Re-type new password: (再次输入你的密码) Adding password for user http://count.linuxtone.org/tongji/data/index.html(目录存在/data/www/wwwroot/tongji/data/目录 下) 将下段配置放到虚拟主机目录,当访问http://count.linuxtone/tongji/即提示要密验证: location ~ ^/(tongji)/ { root /data/www/wwwroot/count; auth_basic "LT-COUNT-TongJi"; auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji; } ``` - Nginx 禁止访问某类型的文件. ```shell location ~* .(txt|doc)$ { if (-f $request_filename) { root /data/www/wwwroot/linuxtone/test; #rewrite …..可以重定向到某个URL break; } } #方法2 location ~* .(txt|doc)${ root /data/www/wwwroot/linuxtone/test; deny all; } # 实例: 禁止访问某个目录 location ~ ^/(WEB-INF)/ { deny all; } ``` - 使用ngx_http_access_module限制ip访问 ```shell location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; deny all; } ``` - Nginx 下载限制并发和速率 ```shell limit_zone linuxtone $binary_remote_addr 10m; server { listen 80; server_name down.linuxotne.org; index index.html index.htm index.php; root /data/www/wwwroot/down; #Zone limit location / { limit_conn linuxtone 1; limit_rate 20k; } .......... } ``` 只允许客房端一个线程,每个线程20k. 【注】limit_zone linuxtone $binary_remote_addr 10m; 这个可以定义在主的 - Nginx 实现Apache一样目录列表 ```shell location / { autoindex on; } ``` - 上文件大小限制 主配置文件里加入如下,具体大小根据你自己的业务做调整。 ```shell client_max_body_size 10m; ```
顶部
收展
底部
[TOC]
目录
一、 Nginx 基础知识
二、 Nginx 安装及调试
三、nginx php-fpm安装配置
四、常见错误处理
五、Nginx监控
六、Nginx 负载均衡
七、nginx反向代理配置
八、Nginx Rewrite
九、Nginx安全
十、nginx基本配置与参数说明
十一、Nginx Redirect、Location
十二、nginx 高并发配置参数
十三、Nginx 参数优化
十四、Nginx 虚拟主机配置例子
相关推荐
Apache