shallow丿ove
nginx用户认证
-
vi /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; serever_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 #测试配置并重新加载
[root@localhost default]# cd /usr/local/nginx/conf/vhost
[root@localhost vhost]# vi test.com.conf
1 server
2 {
3 listen 80;
4 server_name test.com;
5 index index.html index.htm index.php;
6 root /data/wwwroot/test.com;
7
8 location /
9 {
10 auth_basic "Auth";
11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;
12 }
13 }
[root@localhost vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd user
New password:
Re-type new password:
Adding password for user user
[root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd
user:$apr1$NVdIvb1f$14A08nppYo/PwUYjQRD6O.
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x 127.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.4.7</center>
</body>
</html>
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
[root@localhost vhost]# ls /data/wwwroot/test.com
ls: cannot access /data/wwwroot/test.com: No such file or directory
[root@localhost vhost]# mkdir /data/wwwroot/test.com
[root@localhost vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com
test.com
若在某一目录下实现用户认证,如:test.com/admin/,则需将根目录/改为/admin/
[root@localhost vhost]# vi test.com.conf
1 server
2 {
3 listen 80;
4 server_name test.com;
5 index index.html index.htm index.php;
6 root /data/wwwroot/test.com;
7
8 location /admin/
9 {
10 auth_basic "Auth";
11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;
12 }
13 }
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com
test.com
[root@localhost vhost]# mkdir /data/wwwroot/test.com/admin/
[root@localhost vhost]# echo "hello test.com admin dir" > /data/wwwroot/test.com/admin/index.html
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com/admin/
hello test.com admin dir
以上为针对目录,若要针对当个文件,则需要匹配
[root@localhost vhost]# vi test.com.conf
1 server
2 {
3 listen 80;
4 server_name test.com;
5 index index.html index.htm index.php;
6 root /data/wwwroot/test.com;
7
8 location ~(.*)admin.php$
9 {
10 auth_basic "Auth";
11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;
12 }
13 }
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# vi /data/wwwroot/test.com/admin.php
1 <?php
2 echo "hello admin.php";
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/admin/
hello test.com admin dir
[root@localhost vhost]# curl -x 127.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.4.7</center>
</body>
</html>
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com/admin.php
<?php
echo "hello admin.php";
本文详细介绍如何在Nginx中配置HTTP基本认证,包括设置认证目录、创建用户密码文件及针对特定路径或文件的认证方法。
460

被折叠的 条评论
为什么被折叠?



