【问题标题】:Nginx Password Protect 403 ForbiddenNginx密码保护403禁止
【发布时间】:2013-04-17 22:21:47
【问题描述】:

我正在使用 nginx 运行 CentOS 6。它目前运行良好,我正在尝试密码保护我的管理目录。我可以成功登录。但是,当我尝试查看目录中的主索引页面 (index.php) 时,我得到了 403 Forbbiden。

2013/04/18 02:10:17 [error] 17166#0: *24 directory index of "/usr/share/ngin/html /somedir/" is forbidden, client: XXX, server: mysite.com, request: "GET /somedir/ HTTP/1.1",  host: "mysite.com"

我对“.htpasswd”文件有双重检查权限。它属于 chmod 640 的“root:root”。我也尝试将 owner ship 设置为“nginx:nginx”,但错误仍然存​​在。

这就是我让 htpasswd 工作的方式:

location ~ ^/([^/]*)/(.*) {
    if (-f $document_root/$1/.htpasswd) {
            error_page 599 = @auth;
            return 599;
    }
}

location @auth {
    auth_basic "Password-protected";
    auth_basic_user_file $document_root/$1/.htpasswd;
}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    虽然这个问题已经很老了,但我必须把我的解决方案放在这里来帮助别人。这个问题就像我在某个地方的痛苦。

    我可能已经阅读(并实施/尝试)了几乎所有可能的在线可用线程(直到日期),但没有一个能完全解决这个“403 Forbidden”nginx 问题:

    我会从头写下步骤:(阻止我的网站访问):

    1> 我们将在 /etc/nginx 中创建一个名为 .htpasswd 的隐藏文件

    sudo sh -c "echo -n 'usernamee:' >> /etc/nginx/.htpasswd"
    

    2> 现在为给定的用户名添加一个加密密码

    sudo sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"
    

    这将要求您输入密码并确认。

    3> 现在我们需要设置 nginx 以在提供任何内容之前检查我们新创建的 .htpasswd。

    location / {
            try_files $uri $uri/ /index.php?$query_string; # as per my configuration
    
            auth_basic "Authorized access only";
            auth_basic_user_file .htpasswd;
    }
    

    4>最后重启服务器生效

    sudo service nginx restart
    

    现在浏览网址:

    请注意:我没有对权限进行任何更改。默认情况下,htpasswd 的文件权限将在创建时设置,如下所示:

    -rw-r--r-- 1 root root 42 Feb 12 12:22 .htpasswd

    【讨论】:

      【解决方案2】:

      仔细阅读错误。您缺少 index.html 或类似内容。

      【讨论】:

        猜你喜欢
        • 2019-06-07
        • 2019-04-17
        • 2019-05-29
        • 1970-01-01
        • 2019-01-28
        • 1970-01-01
        • 2016-04-24
        • 2019-01-23
        相关资源
        最近更新 更多