您的位置:首页 > 运维架构 > Nginx

实战Nginx(3)-访问控制与用户认证模块及nginx内置状态页介绍

2014-12-27 20:53 591 查看
一.访问控制模块详解
Nginx的访问控制模块是ngx_http_access_module,实际上deny和allow指令属于ngx_http_access_module.我们想控制某个uri或者一个路径不让人访问,就需要依赖此模块.
1.模块安装:
编译安装nginx时不需要指定访问控制模块参数,这个模块已经内置在了nginx中,除非你安装中使用了--without-http_access_module。

2.模块指令:
允许:allow
语法:
Syntax:allow address | CIDR | unix: | all;
默认值:
Default:—
配置段:
Context:http, server, location, limit_except
允许某个ip或者一个ip段访问.如果指定unix:,那将允许socket的访问.注意:unix在1.5.1中新加入的功能,如果你的版本比这个低,请不要使用这个方法。
禁止:deny
语法:
Syntax:deny address | CIDR | unix: | all;
默认值:
Default:—
配置段:
Context:http, server, location, limit_except
禁止某个ip或者一个ip段访问.如果指定unix:,那将禁止socket的访问.注意同上。

3.官网实例:
location / {
deny  192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny  all;
}


解释:
从上到下的顺序,类似iptables。匹配到了便跳出。如上的例子先禁止了192.168.1.1,接下来允许了3个网段,其中包含了一个ipv6,最后未匹配的IP全部禁止访问.

二.用户认证模块详解
nginxd的用户认证模块是ngx_http_auth_basic_module,我们需要使用用户认证模块来控制用户访问特定页面.
1.模块安装
编译安装nginx时不需要指定访问控制模块参数,这个模块已经内置在了nginx中。

2.模块指令:
认证协议:
语法:
Syntax:auth_basic string | off;
默认值:
Default:auth_basic off;
配置段:
Context:http, server, location, limit_except
认证文件:
语法:
Syntax:auth_basic_user_file file;
默认值:
Default:—
配置段:
Context:http, server, location, limit_except


3.官网实例:
location / {
auth_basic           "closed site";
auth_basic_user_file conf/htpasswd;
}


解释:
采用基本认证模块,认证名称是closed site,认证文件存放在是nginx配置文件路径下的conf/htpasswd。

三.对nginx的内置状态页的访问进行访问控制和用户认证。
1.nginx内置状态页介绍
nginx和php-fpm一样内建了一个状态页,对于想了解nginx的状态以及监控nginx非常有帮助。

2.启用nginx status配置并只允许172.16.0.0/16的网段访问
在虚拟主机里面加上location或者你希望能访问到的主机里面。
[root@www ~]# vim /etc/nginx/extra/nginx-vhost.conf
server {
listen   *:80 default_server;
server_name www.stu31.com;
index index.html index.htm ;
root  /www/vhosts/www1;
access_log  /var/log/nginx/www.stu31.com.log main ;
location /status {
stub_status on;
allow 172.16.0.0/16;
deny all;
}
}


2..重启nginx
语法检查:
[root@www ~]# service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启nginx
[root@www ~]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]


3.访问status页面:
[root@www ~]# curl http://172.16.31.40/status Active connections: 1
server accepts handled requests
6 6 5
Reading: 0 Writing: 1 Waiting: 0


4.nginx status详解
active connections – 活跃的连接数量
server accepts handled requests — 总共处理了6个连接 , 成功创建6次握手, 总共处理了5个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.


5.虚拟开启用户认证
[root@www ~]# vim /etc/nginx/extra/nginx-vhost.conf
server {
listen   *:80 default_server;
server_name www.stu31.com;
index index.html index.htm ;
root  /www/vhosts/www1;
access_log  /var/log/nginx/www.stu31.com.log main ;
location /status {
stub_status on;
auth_basic "Nginx-status";
auth_basic_user_file /etc/nginx/.htpasswd;
allow 172.16.0.0/16;
deny all;
}
}


6.创建用户认证文件
[root@www ~]# htpasswd -c -m /etc/nginx/.htpasswd status
New password:
Re-type new password:
Adding password for user status
[root@www ~]# ll -a /etc/nginx/.htpasswd
-rw-r--r-- 1 root root 45 Dec 27 12:33 /etc/nginx/.htpasswd
7.重启nginx服务
[root@www ~]# service nginx restart


8.输入用户名密码访问nginx状态页:
[root@www ~]# curl -u status:status http://172.16.31.40/status Active connections: 1
server accepts handled requests
4 4 4
Reading: 0 Writing: 1 Waiting: 0
访问成功。

至此,nginx的访问控制模块与用户认证模块及开启nginx内置状态页面的知识就介绍完毕了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐