zoukankan      html  css  js  c++  java
  • Nginx 访问认证

    简介

    在实际工作中,企业中有些网站,要求使用账号和密码才能访问,如网站后台、phpMyAdmin 、Wiki 平台 等

    模块ngx_http_auth_basic_module 允许使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问

    模块ngx_http_auth_basic_module 下有两条指令 auth_basic 和 auth_basic_user_file


    语法

    auth_basic string | off;
    

    开启使用“HTTP基本认证”协议的用户名密码验证。参数 off 可以取消继承自上一个配置等级 auth_basic 指令的影响,默认参数为 off。

    auth_basic_user_file file;
    

    指定保存用户名和密码的文件,密码是加密的,格式如下:

    # comment
    name1:password1
    name2:password2:comment
    name3:password3
    

    可以用Apache发行包中的htpasswd命令来创建。


    实战

    配置虚拟主机:

    server {
            listen       80;
            server_name  www.abc.com;
            location / {
                root   html/blog;
                index  index.html index.htm;
                auth_basic            "xxxxxxxxxx";                     # 设置用于认证的提示字符串
                auth_basic_user_file  /usr/local/nginx/conf/htpasswd;   # 设置认证的密码文件
            }
        }
    

    生成认证文件:

    yum install -y httpd                                   # 要用到 http 的工具htpasswd 来产生账号和密码,所以要先安装 http
    htpasswd -bc /usr/local/nginx/conf/htpasswd abc 1234   # 设置认证的账号密码,会保存在密码文件中,第一次使用要用 -bc 参数
    htpasswd -b /usr/local/nginx/conf/htpasswd def 5678    # 以后使用无需加 -c 参数
    chmod 400 /usr/local/nginx/conf/htpasswd
    chown nginx /usr/local/nginx/conf/htpasswd
    /usr/local/nginx/sbin/nginx -t
    /usr/local/nginx/sbin/nginx -s reload
    

    访问网站提示输入用户名和密码:
    认证


    参考资料

    http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_auth_basic_module.html

  • 相关阅读:
    TSQL存储过程:获取父级类别图片
    ASP.NET小收集<7>:JS创建FORM并提交
    JS收集<4>:限制输入搜索串
    js编码风格
    学习日志0504
    记于20120508日晚
    NHibernate中的Session yangan
    SQL Server2005排名函数 yangan
    让IE8兼容网页,简单开启兼容模式 yangan
    Log4Net跟我一起一步步 yangan
  • 原文地址:https://www.cnblogs.com/wushuaishuai/p/9361811.html
Copyright © 2011-2022 走看看