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

    1、设置访问认证的作用:

      在实际的工作中,有时候我们会接到给网站加密的任务,就是需要有用户名和密码才能访问网站的内容,这个一般会是在企业的内部web服务上面来实现,其实也很简单就两个参数

      语法:

    location / {
        auth_basic        "closed site";
        auth_basic_user_file  conf/htpasswd;
    }
    
    • auth_basic:语法:auth_basic string|off; 默认值:auth_basic off; 使用位置:http、server、location、limit_except
    • auth_basic_user_file:语法:auth_basic_user_file file;默认值:-;使用位置:http、server、location、limit_except (auth_basic_user_file参数后接认证密码文件)

    2、设置实例:

      以www.brian.com虚拟主机为例,修改brian.conf配置文件:(添加红色标记位置)

    [root@Nginx www_date]# cat brian.conf 
        server {
            listen       80;
            server_name  www.brian.com;
            location / {
                root   html/brian;
                index  index.html index.htm;
                auth_basic    "brian training";                   # 设置密码提示
                auth_basic_user_file  /opt/nginx/conf/htpasswd;   # 密码文件路径
            }
            access_log logs/brian.log main gzip buffer=128k flush=5s; 
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
     }

      生成认证密码文件:

    [root@Nginx www_date]# yum -y install httpd          # htpasswd是Apache的命令 要安装httpd
    [root@Nginx www_date]# htpasswd -bc /opt/nginx/conf/htpasswd brian brianzjz123      # 生成密码文件
    Adding password for user brian
    [root@Nginx www_date]# chmod 400 /opt/nginx/conf/htpasswd                 # 为了安全设置权限
    [root@Nginx www_date]# chown nginx /opt/nginx/conf/htpasswd 
    [root@Nginx www_date]# cat /opt/nginx/conf/htpasswd              # 查看密码文件
    brian:$apr1$DMJXj4Qp$IrP.gx0wTjV6m.OBgEnNm.
    

      检查语法:

    [root@Nginx conf]# ../sbin/nginx -t
    nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
    nginx: configuration file /opt/nginx//conf/nginx.conf test is successful
    

      平滑重启:

    [root@Nginx conf]# ../sbin/nginx -s reload
    

      windows浏览器测试:

      输入用户名密码:USER:brian Passwd:brianzjz123

  • 相关阅读:
    在UltraEdit中如何像NotePad++一样实现双击单词在全文中高亮
    记人生第一次做面试官的经历
    error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug
    压缩感知中的数学知识:稀疏、范数、符号arg min
    Tensorflow timeline trace
    tensorflow serving
    日志分析工具ELK(一)
    Zabbix3.0安装部署最佳实践
    防cc攻击利器之Httpgrard
    反向代理负载均衡之haproxy
  • 原文地址:https://www.cnblogs.com/brianzhu/p/8625502.html
Copyright © 2011-2022 走看看