【问题标题】:how to forward request from apache to s3如何将请求从apache转发到s3
【发布时间】:2017-06-29 11:29:03
【问题描述】:

我在 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.

【问题讨论】:

  • 我看错了你的问题,所以我删除了我的答案。您应该考虑使用您的自定义域设置 Cloudfront 并将其连接到您的 S3 服务器,以避免 EC2 上传入连接等的重定向开销,从长远来看,CF 还为您提供大量其他好处。
  • 这绝对是可能的,但您希望将ProxyPreserveHost 设置为off 并在ProxyPass 指令中包含存储桶名称,例如...aws.com/bucket/https://bucket.s3....。请不要说“它似乎不起作用”。描述行为。
  • @Michael-sqlbot 我已根据您的建议更新了问题。
  • @tintin 可能是你没有启用 mod_proxy_http 或 mod_ssl 吗?

标签: apache amazon-s3 amazon-ec2


【解决方案1】:

我遇到了同样的问题,通过设置正确的主机头解决了问题

为了了解实际发生的情况,我尝试代理 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 检查它

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 url 中删除静态)

 <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> 

【讨论】:

    【解决方案2】:

    我在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]
    

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 2014-02-18
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2011-06-20
      • 2012-04-23
      相关资源
      最近更新 更多