如何使用Apache在EC2服务器上托管我的网站,但是在同一子域下的S3上托管我的图像?

时间:2022-01-21 00:30:11

So I have my website hosted on an EC2 server running apache. All works fine and well.

所以我的网站托管在运行apache的EC2服务器上。一切都很好。

The issue I'm having is that I want my images hosted on S3 (and possibly cached on CloudFront), not on my EC2 server, but want both accessible under the same domain/subdomain.

我遇到的问题是我希望我的图像托管在S3上(并且可能在CloudFront上缓存),而不是在我的EC2服务器上,但希望两者都可以在同一个域/子域下访问。

For example say my website is www.helloworld.com. I want my images to be accessible at www.helloworld.com/images/foobar.png. I want foobar.png to be hosted on S3 and not each one of my EC2 servers though. How can I go about doing this?

比如说我的网站是www.helloworld.com。我想在www.helloworld.com/images/foobar.png*问我的图片。我希望foobar.png可以托管在S3而不是我的每台EC2服务器上。我该怎么做呢?

Is there and configuration I can do in the AWS console or are there any rewrite rules I can use in Apache? I know I could always just set up a controller that will download the images from S3 and forward them to the user, but this seems wrong to me.

是否有我可以在AWS控制台中执行的配置,或者我可以在Apache中使用任何重写规则吗?我知道我总是可以设置一个控制器,它将从S3下载图像并将它们转发给用户,但这对我来说似乎不对。

2 个解决方案

#1


1  

For that you need to put a load-balancer/proxy in front to intercept all requests and dispatch accordingly. That may not be a bad idea anyway (for availability reasons), but a separate host name for images should not be a problem, either (it's not like users will have to see or type those URL).

为此,您需要在前面放置一个负载均衡器/代理来拦截所有请求并相应地分派。这可能不是一个坏主意(出于可用性原因),但图像的单独主机名也不应该是一个问题(它不像用户必须查看或键入这些URL)。

#2


0  

Using CloudFront, you can have CloudFront receive the initial requests, then based on the path, forward the request to either your EC2 instance or to your S3 bucket.

使用CloudFront,您可以让CloudFront接收初始请求,然后根据路径将请求转发到您的EC2实例或S3存储桶。

For example, you could setup the following:

例如,您可以设置以下内容:

  • A CloudFront behaviour that upon a path under /images/ would use an S3 origin, and
  • 在/ images /下的路径上使用S3原点的CloudFront行为,和
  • A default (for everything else) would go to your EC2 instance origin.
  • 默认(对于其他所有内容)将转到您的EC2实例源。

#1


1  

For that you need to put a load-balancer/proxy in front to intercept all requests and dispatch accordingly. That may not be a bad idea anyway (for availability reasons), but a separate host name for images should not be a problem, either (it's not like users will have to see or type those URL).

为此,您需要在前面放置一个负载均衡器/代理来拦截所有请求并相应地分派。这可能不是一个坏主意(出于可用性原因),但图像的单独主机名也不应该是一个问题(它不像用户必须查看或键入这些URL)。

#2


0  

Using CloudFront, you can have CloudFront receive the initial requests, then based on the path, forward the request to either your EC2 instance or to your S3 bucket.

使用CloudFront,您可以让CloudFront接收初始请求,然后根据路径将请求转发到您的EC2实例或S3存储桶。

For example, you could setup the following:

例如,您可以设置以下内容:

  • A CloudFront behaviour that upon a path under /images/ would use an S3 origin, and
  • 在/ images /下的路径上使用S3原点的CloudFront行为,和
  • A default (for everything else) would go to your EC2 instance origin.
  • 默认(对于其他所有内容)将转到您的EC2实例源。