从其他服务器提供静态文件,同时保持进程不可见

Sil*_*hus 0 php apache .htaccess amazon-s3

我要实现的目标是从S3存储桶为多个网站提供文件,而不显示项目的真实路径。文件存储在S3存储桶中,例如:bucket / website

例:

domain: domain.com
static: static.domain.com
Run Code Online (Sandbox Code Playgroud)

我不想为每个网站都创建S3存储桶,所以我想将所有文件存储在存储桶文件夹中,并使用脚本从那里提供服务。

我目前有:

<?php

    $path = "domain";
    $file = "filename";

    // Save a copy of the file
    file_put_contents($file, fopen($path . $file, 'r'));

    // Set the content type and output the contents
    header('Content-Type: ' . mime_content_type($file));
    readfile($file);

    // Delete the file
    unlink($file);

?>
Run Code Online (Sandbox Code Playgroud)

但这不是那么干净,因为我保存了文件然后将其输出。由于网站可能具有相同名称的文件,因此保存文件可能会导致混乱。我也缺少.htaccess文件来进行正确的重写,因此看来您是从static.domain.com/file获得文件的,而不是从static.domain.com/script获得的文件。

关于如何实现这一点的任何建议都很棒!提前谢谢了!

dec*_*eze 5

配置Apache以将请求代理到S3。就像是:

RewriteRule /path/to/static/files/(.*) http://my.bucket.s3.amazon.com/$1 [P]
Run Code Online (Sandbox Code Playgroud)

这意味着对您的Web服务器的请求/path/to/static/files/foo.jpg将导致您的Web服务器从中获取文件,my.bucket.s3.amazon.com/foo.jpg并像直接来自您的Web服务器一样来提供文件。

但是,请注意,这在卸载流量方面会否定S3的功能。它只是减轻了存储的负担,这很好,但是您的Web服务器仍然需要处理每个单个请求,并且由于必须从另一台服务器获取它,因此实际上在带宽方面会产生一些开销。您至少应该考虑在服务器上缓存副本,和/或在所有这些之前使用CDN。