zoukankan      html  css  js  c++  java
  • Nginx访问路径添加密码保护

    创建口令文件

    用openssl命令创建口令

    openssl passwd -apr1

    会产生一个hash口令, 然后和用户名一起, 以[用户名]:[hash口令]的格式写入文本文件即可

    例如创建一个名为 site_passwd 的文件, 放到nginx的conf目录的 htpasswd 目录下

    mycat:$apr1$s2XP.Vxi$CWQHb8GDsPThsBR6HD4/F/
    milton:$apr1$BZImjdgT$PJ9Dq08hwi5XrBxIHev/W/

    如果有htpasswd命令, 也可以直接用下面的命令创建, 这个命令来自于 apache2-utils

    htpasswd -c site_passwd mycat

    Nginx配置文件修改

    对于需要口令限制的目录, 创建一个单独的location (如果已经有, 则加在已经有的配置上). 例如这里限制的是demo这个路径下的访问. 对于前缀为/的location里的配置, 需要在这个location里重新填一遍, 配置了/demo后, /的配置在这个路径下不再起作用.

        location / {
            root   /var/wwwroot/site;
            index  index.html index.htm index.php;
        }
    
        location /demo {
            root   /var/wwwroot/site;
            index  index.html index.htm index.php;
            auth_basic "Restricted Content";
            auth_basic_user_file htpasswd/site_passwd;
        }
    

    reload后访问就会弹出口令输入了.

    sudo systemctl reload nginx

  • 相关阅读:
    js倒计时
    web前端基本开发手册
    快速了解CSS3当中的HSLA 颜色值怎么算
    IDEA快捷键
    3 认识的本质及其规律
    2 世界的物质性及其发展规律
    1 绪论
    9 数据库连接池
    8 代码实现事务
    7 IDEA连接数据库
  • 原文地址:https://www.cnblogs.com/milton/p/11040894.html
Copyright © 2011-2022 走看看