正在下载NGINX无扩展名的PHP文件(带有身份验证)

时间:2019-03-06 11:12:54

标签: php nginx nginx-location

说明:

我正在尝试将NGINX项目配置为无扩展(PHP)项目。似乎工作正常,除非我将使用基本身份验证,但特定文件夹除外。

我的项目有一个需要无扩展名设置的API文件夹。我正在通过PHP file_get_contents函数检索API文件(这是一个非常基本的API,仅供本地使用)。该项目具有基本身份验证,但是(由于file_get_contents)我想为API文件夹创建一个例外。因此,我在API文件夹位置添加了以下几行:

location ~ ^/api/ {    
   auth_basic "off";
   allow 127.0.0.1;
   allow ::1;
   deny all;
}

但是,由于这个位置,文件正在该文件夹中下载而不是执行。如果删除此位置,文件将正常执行(file_get_contents函数除外,该函数返回401。

问题

file_get_contents由于身份验证而返回401。 file_get_contents应该为/ api /文件夹中的所有文件返回200。

所需结果:

无需使用.htpasswd配置(身份验证)就可以访问API文件夹。该项目的其余部分需要一个.htpasswd。可能无法通过任何外部IP访问API文件夹本身。我相信这是解决我问题的方法。

代码

我正在使用DirectAdmin自定义NGINX配置。这是我完整的(定制的)NGINX配置:

location ~ ^/api/ {
    auth_basic "off";
    allow 127.0.0.1;
    allow ::1;
    deny all;
    try_files $uri $uri.html $uri/ @extensionless-php;
}

location / {
    auth_basic $authentication;
    auth_basic_user_file /home/admin/domains/test.testsite.com/.htpasswd;
    try_files $uri $uri.html $uri/ @extensionless-php;
}

location ~ /\.ht {
    deny all;
}

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

geo $authentication {
    default "Authentication required";
    127.0.0.1/8 "off";
    ::1/128 "off";
    my.home.ip "off";
}

0 个答案:

没有答案
相关问题