blog.zhuohua.store's Archiver

admin 发表于 2020-1-19 09:56

Nginx用户验证

笺注:这是在 [url=http://blog.zhuohua.store/viewthread.php?tid=79&extra=page%3D1]LNMP一键安装包(lnmp_CentOS6.9)[/url] 的基础上进行的。


Nginx的版本:
[root@localhost ~]# nginx -v
nginx version: nginx/[color=Purple]1.10.0[/color]

[root@localhost ~]# nginx -V
nginx version: nginx/[color=Purple]1.10.0[/color]
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module



先找到Nginx的主配置文件:
[root@localhost ~]# find / -name "nginx.conf"
[color=Purple]/usr/local/nginx/conf/nginx.conf[/color]


查看Nginx的主配置文件:
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
[attach]15674[/attach]


默认站点的根目录:
[attach]15675[/attach]


默认情况下,可以通过访问服务器IP地址的方式访问如下页面:
[attach]15676[/attach]


还可以访问默认站点的根目录里的文件、子目录等等:
http://192.168.168.130[color=Blue]/phpinfo.php[/color]
[attach]15677[/attach]


http://192.168.168.130[color=Blue]/phpmyadmin/[/color]
[attach]15678[/attach]


http://192.168.168.130[color=Blue]/nginx_status[/color]
[attach]15679[/attach]



#########

给默认站点的根目录 [color=Blue]/home/wwwroot/default/[/color] 配置用户验证

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
加入以下代码:(只会针对本目录的默认首页、与其子目录的所有文件进行用户验证,但只需要验证一次即可;本目录里其他文件等却可以直接访问)
location [color=DarkRed]/[/color]
{
auth_basic   "Auth";
auth_basic_user_file /usr/local/nginx/conf/[color=Blue]zhuohua_http[/color];
}
[attach]15680[/attach]



创建认证数据文件:
[root@localhost ~]# htpasswd
[color=Red]-bash: htpasswd: command not found[/color]

[root@localhost ~]# yum -y install httpd

[root@localhost ~]# htpasswd -bc /usr/local/nginx/conf/[color=Blue]zhuohua_http[/color] [color=DarkRed]zhuohua[/color] [color=Blue]111[/color]
[color=Purple]Adding password for user zhuohua[/color]

注释:
[color=Blue]/usr/local/nginx/conf/zhuohua_http[/color] 为自定义的认证数据文件
[color=DarkRed]zhuohua[/color] 为用户名
[color=Blue]111[/color] 为用户密码

备注:
这命令也可以给用户更改密码。
认证数据文件可以在别的服务器上创建,再拿过来用的。


自定义的认证数据文件:(密码会加密)
[attach]15681[/attach]


重启Nginx,使更改生效:
[attach]15682[/attach]


客户端远程测试:
http://192.168.168.130[color=Blue]/phpinfo.php[/color]
[attach]15683[/attach]


http://192.168.168.130[color=Blue]/nginx_status[/color]
[attach]15684[/attach]



当客户端访问默认站点根目录就必须要输入对应的用户名和密码了,如下图:
http://192.168.168.130/
[attach]15685[/attach]

[attach]15686[/attach]


当客户端访问默认站点根目录下的子目录里的所有文件都必须要输入对应的用户名和密码,如下图:
http://192.168.168.130[color=Blue]/phpmyadmin/[/color]
[attach]15687[/attach]

[attach]15688[/attach]

备注:
假如客户端在访问站点根目录时,已经输入对应的用户名和密码,并登录成功,那在访问子目录时就不用再次输入对应的用户名和密码,可以直接访问;

假如客户端在访问子目录时已经输入对应的用户名和密码,并登录成功,那在访问站点根目录时就不用再次输入对应的用户名和密码,可以直接访问;













#########

给 [color=Blue]/nginx_status [/color]单独配置用户验证

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
加入以下代码:(这与上面默认站点根目录的验证是分开的)
auth_basic   "Auth";
auth_basic_user_file /usr/local/nginx/conf/[color=Blue]zhuohua_status[/color];
[attach]15689[/attach]

备注: [color=Blue]/nginx_status[/color] 比较特殊,无需有真实目录或文件。


创建认证数据文件:
[root@localhost ~]# htpasswd -bc /usr/local/nginx/conf/[color=Blue]zhuohua_status[/color] [color=DarkRed]jacky[/color] [color=Blue]886[/color]
[color=Purple]Adding password for user jacky[/color]


重启Nginx,使更改生效:
[attach]15690[/attach]


重新访问 [color=Blue]/nginx_status [/color]就必须要输入对应的用户名和密码了,如下图:
http://192.168.168.130[color=Blue]/nginx_status[/color]
[attach]15691[/attach]

[attach]15692[/attach]














#########

给虚拟主机的根目录配置用户验证

查找虚拟主机的配置文件:
[root@localhost ~]# find / -name "blog.zhuohua.store.conf"
[color=Purple]/usr/local/nginx/conf/vhost/blog.zhuohua.store.conf[/color]

编辑虚拟主机的配置文件:
[root@localhost ~]# vi /usr/local/nginx/conf/vhost/blog.zhuohua.store.conf
加入以下代码:
location [color=DarkRed]/[/color]
{
auth_basic   "Welcome to zhuohua";
auth_basic_user_file /usr/local/nginx/conf/[color=Blue]zhuohua_auth1[/color];
}
[attach]15693[/attach]


创建认证数据文件:
[root@localhost ~]# htpasswd -bc /usr/local/nginx/conf/[color=Blue]zhuohua_auth1[/color] [color=DarkRed]zhuohua1[/color] [color=Blue]1234[/color]
[color=Purple]Adding password for user zhuohua1[/color]


重启Nginx,使更改生效:
[attach]15694[/attach]


创建测试的网页文件:
echo 'This is root.' > /home/wwwroot/blog.zhuohua.store/index.html
echo 'This is 1.html' > /home/wwwroot/blog.zhuohua.store/1.html


测试:(虚拟主机的根目录的所有文件都需要验证的,但只需要验证一次即可)

http://blog.zhuohua.store
[attach]15695[/attach]

http://blog.zhuohua.store/1.html
[attach]15696[/attach]


[attach]15697[/attach]

[attach]15698[/attach]














#########

给虚拟主机的子目录配置用户验证

编辑虚拟主机的配置文件:
[root@localhost ~]# vi /usr/local/nginx/conf/vhost/blog.zhuohua.store.conf
加入以下代码:
location [color=DarkRed]/dir1[/color]
{
auth_basic   "Auth for zhuohua";
auth_basic_user_file /usr/local/nginx/conf/[color=Blue]zhuohua_auth2[/color];
}
[attach]15699[/attach]


创建认证数据文件:
[root@localhost ~]# htpasswd -bc /usr/local/nginx/conf/[color=Blue]zhuohua_auth2[/color] [color=DarkRed]zhuohua2[/color] [color=Blue]12345[/color]
[color=Purple]Adding password for user zhuohua2[/color]


重启Nginx,使更改生效:
[attach]15700[/attach]


创建测试的目录和网页文件:
mkdir -p /home/wwwroot/blog.zhuohua.store/dir1
echo 'This is dir1' > /home/wwwroot/blog.zhuohua.store/dir1/index.html
echo 'This is dir1/2.html' > /home/wwwroot/blog.zhuohua.store/dir1/2.html


测试:(虚拟主机的子目录的所有文件都需要验证的,但只需要验证一次即可)

http://blog.zhuohua.store/dir1/
[attach]15701[/attach]

http://blog.zhuohua.store/dir1/2.html
[attach]15702[/attach]


[attach]15703[/attach]

[attach]15704[/attach]





相关文章:
[url=http://blog.zhuohua.store/viewthread.php?tid=292&page=1&extra=#pid295]Nginx基于域名的虚拟主机/域名重定向/访问控制/防盗链/SSL[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=293&page=1&extra=#pid296]Nginx的并发连接数[/url]

[url=http://blog.zhuohua.store/viewthread.php?tid=371&page=1&extra=#pid653]Windows2012R2_UPUPW_Nginx_域名重定向+用户验证+访问控制+SSL[/url]

[url=http://blog.zhuohua.store/viewthread.php?tid=63&page=1&extra=#pid64]CentOS6_Nginx反向代理+负载均衡(轮询)[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=405&page=1&extra=#pid833]CentOS8_Nginx基于域名的虚拟主机+代理虚拟主机[/url]

[url=http://blog.zhuohua.store/viewthread.php?tid=274&page=1&extra=#pid277]Apache2.2基于域名的虚拟主机+用户授权限制+客户端地址限制[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=312&page=1&extra=#pid315]Apache2.4基于域名的虚拟主机+用户授权限制+客户端地址限制[/url]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.