Nginx访问路径添加密码保护

建立口令文件

用openssl命令建立口令php

openssl passwd -apr1

会产生一个hash口令, 而后和用户名一块儿, 以[用户名]:[hash口令]的格式写入文本文件便可html

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

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

若是有htpasswd命令, 也能够直接用下面的命令建立, 这个命令来自于 apache2-utilsapache

htpasswd -c site_passwd mycat

 

Nginx配置文件修改

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

    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后访问就会弹出口令输入了.spa

sudo systemctl reload nginxcode

相关文章
相关标签/搜索