dqnqpqv3841 2019-03-06 11:12
浏览 53

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

Explanation:

I'm trying to configure my NGINX project as an extensionless (PHP) project. It seems to work fine, except when I will be using basic authentication with an exception for a specific folder.

My project has an API folder which requires the extensionless settings. I'm retrieving my API files in a PHP file_get_contents function (it's a very basic API and meant for local use only). The project has a basic authentication, but (because of file_get_contents) I want to create an exception for the API folder. Therefore I have added the following lines to the API folder location:

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

However, due to this location, the files are being downloaded in that folder instead of executed. If I remove this location, the file will be executed fine (except for the file_get_contents function, which returns a 401.

Problem

file_get_contents returns a 401 because of the authentication. file_get_contents should return a 200 for all files in the /api/ folder.

Desired result:

The API folder can be accessed without the .htpasswd configuration (authentication). The rest of the project requires a .htpasswd. The API folder itself may not be accessed by any external IP. I believe this is the solution for my problem.

Code

I am using DirectAdmin to customize the NGINX configuration. This is my full (customized) NGINX config:

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP