【问题标题】:NGINX - Why can I bypass password authentication?NGINX - 为什么我可以绕过密码验证?
【发布时间】:2016-09-30 17:07:16
【问题描述】:

我有一个 NGINX 服务器,我使用 Apache 实用程序的密码要求 (.htpasswd)。它大部分工作正常。以下这项工作正常:

example.com/admin
example.com/admin/
example.com/admin/index

但是... 当我输入example.com/admin/index.php 并且根本不输入任何密码并按“中止”时,服务器显示的是 index.php(没有任何 CSS 或 JS 文件)。我认为我的 PHP-FPM 是问题所在。请看:

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

location /admin {
    auth_basic "Restricted";
    auth_basic_user_file /admin/.htpasswd;
}

location ~ \.php$ {
    fastcgi_pass            127.0.0.1:9000;
    include                 fastcgi_params;
    fastcgi_param           SCRIPT_FILENAME $document_root$
}

【问题讨论】:

  • 参见this,但基本上您的身份验证适用于location /admin.php 文件由location ~ \.php$ 处理。
  • 我明白了。但是我该如何解决呢?我不知道。

标签: .htaccess nginx .htpasswd apache-utils


【解决方案1】:

只需查看您问题中的最后两个位置:

location ^~ /admin {
    auth_basic "Restricted";
    auth_basic_user_file /admin/.htpasswd;

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        include       fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

location ~ \.php$ {
    fastcgi_pass  127.0.0.1:9000;
    include       fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

添加^~ 修饰符使location ^~ /admin 块优先于其他正则表达式块(特别是现有的location ~ \.php$ 块)。所以认证规则统一应用于任何以/admin开头的URI。详情请见this document

为避免破坏 PHP,location ~ \.php$ 块在 location ^~ /admin 块中重复,以处理以 /admin 开头并以 .php 结尾的 URI。

【讨论】:

  • 不错。该错误现已修复。但现在他总是说403 Forbidden。我检查了目录权限,但没有问题。
  • 403 可能是由于 URI 是一个裸目录(如 /admin/admin/)并且不存在 indextry_files 规则来解决它。您可能希望考虑将 indextry_files 添加到 location ^~ /admin 块中以获得所需的行为。
  • 我试过了,但仍然是 403。即使我将 URI 指向文件。你有什么想法吗?
  • auth_basic 以前工作过吗?这可能是一个无关的问题。顺便说一句,auth_basic_user_file 采用路径名而不是 URI。 /admin/.htpasswd 看起来有点像 URI。
猜你喜欢
  • 2018-05-12
  • 2011-10-04
  • 1970-01-01
  • 2016-05-21
  • 2015-02-09
  • 2013-03-15
  • 2011-08-02
  • 2012-10-22
  • 1970-01-01
相关资源
最近更新 更多