nginx 对某个目录或整个网站进行登录认证的方法

来源:互联网 发布:小学生沉迷网络的案例 编辑:程序博客网 时间:2024/06/05 16:33

1.在linux上面安装httpd:yum install httpd 命令来安装。

2.使用 htpasswd -c -d /home/java-web/my-shell/password hupei  然后输入密码,生成密码文件。(附:密码文件是以username:password这样的形式展现的)

3.在nginx中配置静态服务器:

   server
    {
        listen  5555; //访问的端口
        server_name  139.196.137.221; //访问ip
        index index.html index.htm;
        root /home/static/; //静态资源所在本机的路径
        access_log on;
        autoindex on;
        location /hupei/ { //需要登录认证的目录
             auth_basic 'please enter username and password'; //弹出窗口的提示信息
             auth_basic_user_file /home/java-web/my-shell/password; //密码文件 该路径就是第二部生成的密码文件路径
        }
  }

4.重启nginx:service nginx restart。然后再访问会弹出如下窗口,证明已经达到我们需要的效果。

  

1 0
原创粉丝点击