AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 450154
Accepted
carrot
carrot
Asked: 2012-11-20 07:33:33 +0800 CST2012-11-20 07:33:33 +0800 CST 2012-11-20 07:33:33 +0800 CST

nginx:禁止访问文件夹,某些子文件夹除外

  • 772

如何拒绝对文件夹的访问,但将其中的某些子文件夹从“拒绝”中排除?

我试过这样的事情(按此顺序):

#这个子文件夹不应该被拒绝并且里面的php脚本应该是可执行的

位置 ~ /data/public { 允许所有;}

#this 文件夹包含许多子文件夹,应该拒绝公共访问

位置〜/数据{全部拒绝;返回 404;}

...这不能正常工作。/data/public 文件夹中的文件是可以访问的(/data 中的所有其他文件都应该被拒绝),但是 /data/public 文件夹中的 PHP 文件不再执行(如果我不添加这些限制,PHP文件是可执行的)。

怎么了?怎么可能是对的?我认为有更好的方法来做到这一点。

如果有人可以帮助我,那就太好了:)。


谢谢,但是 PHP 文件仍然没有在 /data/public/ 文件夹中执行,就像一个简单的

<? echo "test"; ?>

它为您提供此文件作为下载(没有上面的“拒绝”配置,php 文件运行良好)。

我的 PHP 配置:

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

/data/ 之外的所有其他目录中的 PHP 文件正在工作......(其他子文件夹也是如此)。

php
  • 2 2 个回答
  • 34360 Views

2 个回答

  • Voted
  1. Best Answer
    Jap Mul
    2012-11-21T00:01:39+08:002012-11-21T00:01:39+08:00

    php 文件没有被处理的原因是当它到达/data/public/它停在那里的位置并且不知道如何处理 php 文件。

    尝试将您的 php 位置放在另一个名为 php.conf 的文件中,并将该文件包含在您的服务器块和/data/public/块中。所以你的配置看起来像

    server {
        location ^~ /data/public/ {
            allow all;
            try_files $uri $uri/ /index.php?args;
            # include to avoid writing it twice..
            include php.conf
        }
    
        location ^~ /data/ { 
            deny all; 
        }
    
        # .....
        # Some other config blocks
        # .....
    
        # Put this line instead of the php config block to avoid writing the php part twice
        include php.conf
    }
    

    该php.conf文件(在您的情况下)将如下所示:

    location ~ \.php$ {
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
         fastcgi_index index.php;
         include fastcgi_params;
    }
    
    • 5
  2. Vern Burton
    2012-11-20T12:02:08+08:002012-11-20T12:02:08+08:00

    位置规则应如下所示。

    location ^~ /data/                { deny all; }
    

    或者

    location ^~ /data/public/               { allow all; }
    

    nginx定位规则如下

    To find a location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the most specific one is searched. Then regular expressions are checked, in the order of their appearance in a configuration file. A search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then a configuration of the most specific prefix location is used.
    

    nginx访问规则如下

    "Access rules are checked according to the order of their declaration. The first rule that matches a particular address or set of addresses is the one that is obeyed."
    

    所以一个工作配置应该看起来像

    location ^~ /data/public/               { allow all; }
    location ^~ /data/ { deny all; }
    

    通过使用 deny all,它应该返回 403 Forbidden 而不是 404。

    这应该允许访问和处理公共目录,并阻止任何其他内容。我在为 Magento 配置 nginx 时遇到了同样的问题,但我通过 ^~ 技巧解决了这个问题。

    • 0

相关问题

  • 用户特定的 Php.ini 当 php 作为模块运行时?

  • 使 php mail() 函数在 ubuntu-server 上工作的步骤是什么?

  • Web 服务器和数据库服务器位于完全不同的位置

  • PHP 作为 CGI 还是 Apache 模块?

  • 通过 VPN 连接什么是远程服务器 IP?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve