nginx-设置目录访问权限及特定文件查看

cjh| 阅读:1437 发表时间:2018-03-22 22:34:21 linux

环境:centos

1.创建用户

htpasswd -c -d /usr/local/nginx/passwd.db username

或添加用户

htpasswd -d /usr/local/nginx/passwd.db username


如果命令不存在,需要安装apache(httpd)

yum install httpd

配置nginx(这里设置成访问网站根目录所有文件夹都需要验证)

 location / {
        root   /www/work;
        auth_basic "提示语";
        auth_basic_user_file /usr/local/nginx/passwd.db;
        index  index.html index.htm index.php;
    }

nginx 平滑重启

service nginx reload 

关于配置出错的问题及查找方法



这行命令会权限设置有问题

1.查看错误日志(error.log/位置在配置文件中找到 或locate/find 直接查找)


显然权限问题

修改密码验证文件的权限(外部访问用户读取和执行权限 5即是005)

chmod 5 /usr/local/nginx/passwd.db 


不需要重启,问题已解决.


续:制作简易的特定(html)文件查看

 list 输出 x-mind 导出的html

在根目录创建 index.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>X-mind图</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<ol>
    <?php
    $list_str =shell_exec("ls -Ft");//使文件夹加/,可执行文件加*,时间逆序
    $list = explode("\n",$list_str) ;//切割成数组
    unset($list[count($list)-1]);//去掉最后一个\n
    foreach ($list as $k=>$v){
        //(输出html)
        if(strpos($v,'html')){
            echo "<li><a href='{$v}'>{$v}</a></li>";
        }
    }
    ?>
</ol>
<style>
    a:hover, a:visited, a:link, a:active {
        text-decoration: none;
        color: #000;
    }
    li{
        line-height: 40px;
    }
</style>
</body>
</html>

效果:

 点击进入效果