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

    报告相同问题?

    悬赏问题

    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体