【CentOS7架构23】,设置nginx用户认证#

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";


转载于:https://my.oschina.net/u/3892756/blog/3069388

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值