分享

Nginx访问控制配置方法 | 蓝风博客

 corefashion 2014-08-21

lnmp

1.Nginx 身份证密码验证

1
2
3
4
5
6
7
#cd /usr/local/nginx/conf
#mkdir htpasswd
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/home lenfeng
#添加用户名为lenfeng
New password: (此处输入你的密码)
Re-type new password: (再次输入你的密码)
Adding password for user

http://it./home/data/index.html(目录存在/www/wwwroot/home/data/目录下)

将下段配置放到虚拟主机目录,当访问http://it./home/即提示要密验证:

1
2
3
4
5
location ~ ^/(home)/ {
root /www/wwwroot/count;
auth_basic "LT-COUNT-Home";
auth_basic_user_file /usr/local/nginx/conf/htpasswd/home;
}

 

2.禁止Nginx 对某类型的文件访问

如,在Nginx下禁止对*.txt文件的访问,配置方法如下:

1
2
3
4
5
6
7
location ~* \.(txt|doc)$ {
if (-f $request_filename) {
root /data/www/wwwroot/lenfeng/test;
#rewrite …..可以重定向到某个URL
break;
}
}

第二种方法:

1
2
3
4
location ~* \.(txt|doc)${
root /data/www/wwwroot/lenfeng/test;
deny all;
}

第三种方法:禁止访问某个目录

1
2
3
location ~ ^/(WEB-INF)/ {
deny all;
}

 

3.用ngx_http_access_module限制ip对nginx的访问

1
2
3
4
5
6
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}

 

4.限制Nginx 下载速度和并发速率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
limit_zone lenfeng $binary_remote_addr 10m;
server
{
listen 80;
server_name down.lenfeng.com;
index index.html index.htm index.php;
root /data/www/wwwroot/down;
#Zone limit
location / {
limit_conn lenfeng 1;
limit_rate 20k;
}
..........
}
//只允许客户端一个线程,每个线程20k.

【注】limit_zone lenfeng $binary_remote_addr 10m; 这个可以定义在主的

 

5.Nginx 跟Apache一样有目录列表权限

1
2
3
location / {
autoindex on;
}

 

6.对上文件大小限制

主配置文件里加入如下,具体大小根据你自己的需做对应的调整。

1
client_max_body_size 10m;

更多

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多