如何将请求从apache转发到s3

时间:2017-02-11 00:35:29

标签: apache amazon-s3 amazon-ec2

我在fasthost DNS提供商上有一个名为mywebsite.com的托管网站。对mywebsite.com的任何请求都会转发到正在运行我的Web应用程序的aws EC2实例上运行。在EC2实例中,我已经设置了Apache http服务器,它借助apache httpd.conf文件中的以下虚拟主机设置获取所有请求并转发到我的Web应用程序

<VirtualHost *:80>
     ProxyPreserveHost On
     ProxyRequests Off
     ServerName mywebsite.com
     ProxyPass / http://127.0.0.1:8080/
     ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost> 

到目前为止,这工作正常。现在我有静态图像,我已经在aws S3存储桶上托管了。可公开访问的网址为https://s3.eu-west-2.amazonaws.com/static/images/some-img.png

我不希望出现S3域名,并希望屏蔽s3.eu-west-2.amazonaws.com服务器名称,而不是https://s3.eu-west-2.amazonaws.com/static/images/some-img.png我有类似的东西 https://mywebsite.com/static/images/some-img.png 我尝试在httpd.conf中添加虚拟主机条目,以便任何带有上下文/static的请求都转发到S3实例。但它似乎没有起作用:

<VirtualHost *:80>
     ProxyPreserveHost On
     ProxyRequests Off
     ServerName mywebsite.com
     ProxyPass /static https://s3.eu-west-2.amazonaws.com/
     ProxyPassReverse /static https://s3.eu-west-2.amazonaws.com/
</VirtualHost> 

知道这是否可能?

更新 我将VirtualHost修改为

<VirtualHost *:80>
     ErrorLog logs/mywebsite.com-error_log
     ProxyPreserveHost Off
     ProxyRequests Off
     ServerName mywebsite.com
     ProxyPass /static https://s3.eu-west-2.amazonaws.com/mywebsite-static-repo
     ProxyPassReverse /static https://s3.eu-west-2.amazonaws.com/mywebsite-static-repo

</VirtualHost> 

其中mywebsite-static-repo是存储桶的名称。 mywebsite.com-error_log出现以下错误:

File does not exist: /var/www/html/mywebsite-static-repo
[warn] proxy: No protocol handler was valid for the URL /static/mywebsite-static-repo/images/some-img.png. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.

2 个答案:

答案 0 :(得分:0)

我在us-west-2区域中设置了存储桶,无论我做什么,都无法使ProxyPass正常工作。作为最后的选择,我删除了存储桶,并在默认区域(us-east-1)中重新创建了存储桶。然后终于ProxyPass工作了。设置方法如下(是的,我知道存储桶名称在其中两次):

SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
ProxyPass        /static/ https://<bucket_name>.s3.amazonaws.com/<bucket_name>/
ProxyPassReverse /static/ https://<bucket_name>.s3.amazonaws.com/<bucket_name>/

因此,似乎S3不能接受默认区域以外的其他区域中的代理请求(除非您可以发送一些我不知道的标头)。

直到铲斗移动后,我才尝试使用RewriteRule with the proxy flag

RewriteRule "/static/(.*)$" "https://<bucket_name>.s3.amazonaws.com/$1" [P]

答案 1 :(得分:0)

我遇到了同样的问题,可以通过设置适当的Host标头来解决该问题

要了解实际情况,我尝试代理httpbin.org

<VirtualHost *:80>
     ProxyPreserveHost On
     ProxyRequests Off
     ServerName www.example.org

    <Location /get>
         ProxyPass http://httpbin.org/get
         ProxyPassReverse  http://httpbin.org/get
         RequestHeader set "Host" "httpbin.org"
         RequestHeader unset Cookie
    </Location>

</VirtualHost> 

检查是否卷曲

curl -s“ http://www.example.org/get?foo=bar

{
  "args": {
    "foo": "bar"
  },
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.64.1",
    "X-Forwarded-Host": "httpbin.org",
    "X-Forwarded-Server": "www.example.org"
  },
  "origin": "---",
  "url": "https://httpbin.org/get?foo=bar"
}

**奖励:我发现为了安全起见,我也需要删除Cookie标头。

然后我用s3桶进行了测试---请避免在斜杠后面加上斜线(如果不打算在s3网址中删除静态字符)

 <VirtualHost *:80>
     ProxyPreserveHost On
     ProxyRequests Off
     ServerName www.example.org

     <Location /static>
       ProxyPass  http://BUCKET.s3.amazonaws.com/static
       ProxyPassReverse  http://BUCKET.s3.amazonaws.com/static
       RequestHeader set "Host" "BUCKET.s3.amazonaws.com"
       RequestHeader unset Cookie
     </Location>

</VirtualHost>