Nginx访问控制_登陆权限的控制(http_auth_basic_module)

Nginx提供HTTP的Basic Auth功能,配置了Basic Auth之后,需要输入正确的用户名和密码之后才能正确的访问网站。

我们使用htpasswd来生成密码信息,首先要安装httpd-tools,在httpd-tools中包含了htpasswd命令。需要依赖一个工具,先检查一下系统是否已经安装此工具。

rpm -qf /usr/bin/htpasswd 

如果没有安装此工具,那么可以yum命令安装

yum install -y httpd-tools

接下来我们就可以创建用户和密码了,例如创建一个test_uname的用户,而且指定目录为/etc/nginx,如果没有此目录,那么创建目录,进入/etc/nginx目录下,执行以下命令

htpasswd -c ./nginx_auth_conf test_uname

下一步就是确定两次密码,完成之后cat查看一下

[root@localhost nginx]# cat nginx_auth_conf 
test_uname:zYOFNeL1lXaGk   //test_uname 为用户名,后面是密码

然后下一步就是修改Nginx的配置文件了,需要在访问控制的目录下编辑配置项,一定要制定好密码文件的路径

server {
  listen       80;
  server_name example.com;
  access_log logs/access.log main;
  location / {
        root  /data/www/project;
        auth_basic "auth.......";
        auth_basic_user_file /etc/nginx/nginx_auth_conf;
        index index.php index.html;
  }
}

记得修改完配置一定要检测Nginx配置是否正确,正确后再重新软加载配置文件

[root@~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@~]# /usr/local/nginx/sbin/nginx -s reload

 

OK~

 

posted @ 2018-01-10 11:48  温柔的风  阅读(2266)  评论(0编辑  收藏  举报