首页 > 解决方案 > 在 Apache 代理后面为 S3 静态网站 JS 获取 404

问题描述

我有一个 Apache HTTP 服务器作为 AWS S3 静态网站前面的代理。所以http://example.com/testsite转到 S3 站点,但应该继续被视为从http://example.com/testsite访问。

初始index.html访问导致 200 响应,但是,所有运行的 JavaScript 文件都返回 404。它们位于 S3 根目录中,与 相同index.html,但最终以http://example.com/而不是http://example.com/testsite,因此我得到 404。

我希望有人可以帮助我处理我的 Apache 配置片段(请参见下文),以便我能够获得正确的配置。

RedirectMatch permanent ^/$ /doorman/
Redirect /js/ /frontman/js/
...
...
<Location /testsite >
  ProxyPreserveHost On
  AuthType openid-connect
  Require valid-user
  ProxyPass <%= @s3_website %>/
  ProxyPassReverse <%= @s3_website %>/
</Location>

标签: apacheamazon-s3reverse-proxyhttpd.conf

解决方案


我通过使用以下配置解决了这个问题:

``` ProxyPreserveHost Off AuthType openid-connect 需要有效用户 ProxyPass <%= @s3_website %> ProxyPassReverse <%= @s3_website %>

ProxyPreserveHost Off AuthType openid-connect 需要有效用户 ProxyPass <%= @s3_website %>/assets/$1 ProxyPassReverse <%= @s3_website %>/assets/$1

ProxyPreserveHost Off AuthType openid-connect 需要有效用户 ProxyPass <%= @s3_website %> ProxyPassReverse <%= @s3_website %> ```

关键部分是:

“ProxyPreserveHost Off” - 我的 S3 存储桶名称与代理不同,因此需要设置为“Off”。

“LocationMatch” - 需要这样代理才能匹配,并将 URL 请求映射到 S3 存储桶位置/对象。


推荐阅读