带有NGINX的Ubuntu 20.04上的Laravel 7 - 存储/受保护/图像目录中的图像在通过URL访问时返回



我刚刚将应用程序从本地环境移动到测试/生产服务器。

一切都适用于 AWS 驱动器,但是当更改为本地驱动器(即存储图像storage/protected/images(时,我会error 404本地驱动器中的所有图像。

所有路由都存在,所有控制器也就位。有趣的是,我也存储在storage/protected/files中的可下载文件工作得很好,我可以毫无问题地下载它们。

这是我的ImageViewController

命名空间 App\Http\Controllers\Client;

use AppExceptionsErrorPageException;
use IlluminateHttpRequest;
use AppHttpControllersController;
use IlluminateSupportFacadesStorage;
class ImageViewController extends Controller
{
public function getMainImage($name)
{
if(file_exists(storage_path().'/app/protected/images/'.$name)){
$image = storage_path().'/app/protected/images/'.$name;
}else{
throw new ErrorPageException(404);
}
return response()->file($image);
}
public function getImage($type, $name)
{
if(file_exists(storage_path().'/app/protected/images/'.$type.'/'.$name)){
$image = storage_path().'/app/protected/images/'.$type.'/'.$name;
}else{
throw new ErrorPageException(404);
}
return response()->file($image);
}
}

我用来访问图像的直接 URL 是example.com/images/image.png

我怀疑这个错误与我的nginx设置有关,除非我错了。

请分享您的想法,如果可以的话,请帮助我。

正如我所怀疑的那样,nginx设置导致了这个问题,所以这是我必须做的来解决这个问题:

server {
listen 80;
server_name server_domain_or_IP;
root /var/www/laravel/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}

来源:如何在 Ubuntu 18.04 上使用 LEMP 安装和配置 Laravel

(英语:Lem(

最新更新