×
新网 > 虚机资讯 > 正文

12.8 Nginx用户认证

Nginx用户认证目录概要 vim /usr/local/nginx/conf/vhost/test.com.conf//写入如下内容 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com;

Nginx用户认证目录概要 vim /usr/local/nginx/conf/vhost/test.com.conf//写入如下内容

server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } yum install -y httpd htpasswd -c /usr/local/nginx/conf/htpasswd aming -t && -s reload //测试配置并重新加载 mkdir /data/wwwroot/test.com echo “test.com”>/data/wwwroot/test.com/index.html curl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证 curl -uaming:passwd 访问状态码变为200 编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗 针对目录的用户认证

1 (67).jpg

location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } Nginx用户认证 首先切换到usr/local/nginx/conf/vhost/目录下

[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost/ [root@hanfeng vhost]# 新建新建一个虚拟主机test.com.conf,并编辑

[root@hanfeng vhost]# ls aaa.com.conf [root@hanfeng vhost]# vim test.com.conf 添加以下内容 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / //表示全站,都需要进行用户认证 #location /admin // 这个地方只要加上” /admin ” 就变成 针对这个站点的“admin” 这个目录需要用户认证 #location ~ admin.php //如果把这行这样写,就会变成,匹配 “ admin.php ”这个页面的时候才需要用户认证 { auth_basic "Auth"; //定义用户认证的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用户名密码文件 } } 保存退出 在配置完成后,需要生成密码文件 在生成密码文件,需要用到Apache生成密码文件的工具“ htpasswd ” 若本机已经安装过Apache,可以直接使用命令htpasswd进行生成

/usr/local/apache2.4/bin/htpasswd 若是本机未安装Apache,可直接 yum install -y httpd 进行安装,因为yum安装的,所以工具存放在/usr/bin/下,可以直接使用htpasswd

yum install -y httpd 这里由于未安装过Apache,所以先yum安装

[root@hanfeng vhost]# yum install -y httpd 在yum安装后,可以直接使用htpasswd命令 htpasswd指定文件,生成用户

[root@hanfeng vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd hanfeng New password: //密码hanfeng Re-type new password: Adding password for user hanfeng [root@hanfeng vhost]# 使用cat 命令查看/usr/local/nginx/conf/htpasswd 文件,会看到生成了一行字符串

[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/ [root@hanfeng vhost]# 关于htpasswd -c 命令 第一次创建的时候因为没有htpasswd这个文件,需要-c创建,第二使用的时候因为已经有这个htpasswd文件了,将不再需要-c 选项,如果还继续使用-c 这个选项,将会重置 htpasswd里的东西 再来htpasswd指定文件,生成另一个用户

[root@hanfeng vhost]# htpasswd /usr/local/nginx/conf/htpasswd gurui New password: Re-type new password: Adding password for user gurui [root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/ gurui:$apr1$mqc2Dgwa$qVvurqGN6gj8hX3tEpQ6j/ [root@hanfeng vhost]# 检查配置nginx文件是否存在语法错误

[root@hanfeng vhost]# /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@hanfeng vhost]# 重新加载配置文件 在重新加载的时候,若配置文件中存在错误,配置文件将不会生效; 如果是直接使用restart,如果配置有错,将会直接影响到网站的运行

[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload [root@hanfeng vhost]# 测试

[root@hanfeng vhost]# curl -x127.0.0.1:80 test.com <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hanfeng vhost]# curl -x127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 16:52:23 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@hanfeng vhost]# 会提示错误码401,就是需要用户,所以用curl指定用户 这时指定用户和密码再来访问,会提示404,这是因为去访问index.html,但是还未创建

[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hanfeng vhost]# 创建目录,然后新建index.html

[root@hanfeng vhost]# mkdir /data/wwwroot/test.com [root@hanfeng vhost]# echo “test.com”>/data/wwwroot/test.com/index.html [root@hanfeng vhost]# 这时再来访问,会看到显示正常

[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com “test.com” [root@hanfeng vhost]# 这里的用户认证是针对整站 针对某一个目录下,才需要认证 比如访问admin的时候,才需要认证 首先访问admin尝试下

[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/ <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]# 然后在/usr/local/nginx/conf/vhost/test.com.conf配置文件中定义,只需要在location / 后加上admin/ 目录即可

[root@hf-01 vhost]# vim test.com.conf 在location / 后加上admin/ 目录 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } 保存退出 检查配置文件是否存在语法错误

[root@hf-01 vhost]# /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@hf-01 vhost]# 重新加载配置文件

[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@hf-01 vhost]# 这时候再来访问test.com,就不需要指定用户名和密码了

[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com “test.com” [root@hf-01 vhost]#

6.访问test.com/admin/目录

[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]# 这时创建一个测试页面 先新建目录

[root@hf-01 vhost]# mkdir /data/wwwroot/test.com/admin [root@hf-01 vhost]# 然后在admin目录下新建index.html

[root@hf-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/adm in/index.html [root@hf-01 vhost]# 这时再来访问 test.com/admin/ 会显示401,但是指定用户名和密码后就会正常显示

[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/ test.com admin dir [root@hf-01 vhost]# 针对URL 比如针对admin.php 首先在配置文件/usr/local/nginx/conf/vhost/test.com.conf下定义,在 location 后加~ admin.php即可

[root@hf-01 vhost]# vim test.com.conf 在 location 后加~ admin.php即可 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location ~ admin.php { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } 保存退出 检查配置文件是否存在语法错误

[root@hf-01 vhost]# /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@hf-01 vhost]# 重新加载配置文件

[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@hf-01 vhost]# 这时候就可以直接访问 test.com/admin/,不需要指定用户名和密码了,但是在访问admin.php的时候,则会显示401——>状态码为401说明需要验证

[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ test.com admin dir [root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]#

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading