如何公开不在 public/ - Laravel 中的特定目录?

How to expose a specific directory that is not in public/ - in Laravel?

我在做一个非常古老的 Laravel 4.2 项目。出于 安全性 原因,我将所有文件存储在 /app/files/ 中。我想将我的目录之一设置为 public 访问权限,这个 /app/files/logo_path/.


假设...我有一个文件 cat.png 位于

Ex. /app/files/logo_path/cat.png

我希望我的域 return 像这样 domain.com/logo_path/cat.png

我应该使用 routes.php 还是 Nginx configuration 来解决这个问题?

基本上,我想映射从 /app/files/logo_path/*domain.com/logo_path/*

的所有图像

使用nginx,你可以通过以下配置来完成:

location ^~ /logo_path/ {
    root /app/files; # you should specify full path to /app/files folder here!
}

我假设你在 /app/files/logo_path/ 中除了图像之外没有任何其他文件,这就是为什么我使用 ^~ 位置修饰符来防止任何尝试从这里请求任何 PHP 脚本路径(如果出现这样的请求,它不会被 PHP 处理程序位置拦截并被 PHP-FPM 引擎解释。

此配置不是安全漏洞,因为此位置块将仅用于以 /logo_path/ 前缀开头的请求。如果您想使用不同的前缀,请改用 root directive won't be suitable for you, for that case you would need to use an alias 指令。以下是 nginx 文档的摘录:

For example, with the following configuration

location /i/ {
    root /data/w3;
}

The /data/w3/i/top.gif file will be sent in response to the /i/top.gif request.

...

A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.

nginx配置就是解决这个问题的方法。通过 php 传递这些静态图像对象没有什么好处,除非 您只想将它​​们显示给授权的站点访问者。

nginx 在传送静态对象方面比 php 更快。它 mmaps the objects' files to RAM and then sends 它们到浏览器。快!