简体   繁体   English

从Heroku Rails应用程序重新路由到AWS托管资产

[英]Rerouting to AWS hosted assets from a Heroku Rails app

I have a Heroku hosted Rails app that has reached the 300MB limit for the slug size and can no push to Heroku. 我有一个Heroku托管的Rails应用,该应用的子弹大小已达到300MB的限制,无法推送到Heroku。 To fix this issue, I've setup an AWS S3 account and want to redirect the assets being requested from my Rails app to the new S3 location. 为了解决此问题,我已经设置了一个AWS S3帐户,并希望将从我的Rails应用程序请求的资产重定向到新的S3位置。 The Rails app is basically just serving JSON files that point to the static assets using a relative URL. Rails应用程序基本上只是使用相对URL提供指向静态资产的JSON文件。 An iOS app using the Rails JSON then has a hardcoded domain URL, and appends the path to the resources to that domain and requests assets it needs. 然后,使用Rails JSON的iOS应用程序具有一个硬编码的域URL,并将该资源的路径追加到该域并请求其所需的资产。

I want to update my Heroku app and change my asset locations without requiring an update to the iOS app in order to change the asset domain. 我想更新我的Heroku应用程序并更改资产位置,而无需更新iOS应用程序即可更改资产域。 So, I need to redirect static asset requests from the Rails app to the AWS server. 因此,我需要将静态资产请求从Rails应用重定向到AWS服务器。

In my git repo, I've ignored the assets in the public folder that I've moved to the AWS server. 在我的git仓库中,我忽略了移到AWS服务器的公用文件夹中的资产。 The asset files are present on my local machine, but are not part of the git repo when uploaded to Heroku. 资产文件存在于我的本地计算机上,但是当上传到Heroku时不属于git repo的一部分。

So far I've tried changing the config.action_controller.asset_host which does not seem to work since these are static assets and are being returned by the web server before Rails gets it. 到目前为止,我已经尝试更改config.action_controller.asset_host ,该值似乎不起作用,因为它们是静态资产,并且在Rails获取之前由Web服务器返回。

I tried using routes rules to redirect to a different domain, but these routes never seem to be captured. 我尝试使用路由规则重定向到其他域,但是这些路由似乎从未被捕获。 The static files appear to be returned before the Rails app has a chance to handle the request. 静态文件似乎在Rails应用程序有机会处理请求之前被返回。

I've tried using the rack-rewrite gem to try and redirect my assets to a different domain with the following in `initializers/rack_rewrite.rb: 我已经尝试过使用rack-rewrite gem来尝试将我的资产重定向到`initializers / rack_rewrite.rb中的以下内容:

require 'rack/rewrite'

AppNamespace::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  r301 %r{/images(.*)}, 'http://my-subdomain.amazonaws.com/images$1'
end

This doesn't seem to work either and it always just returns the static files. 这似乎也不起作用,它总是只返回静态文件。

So far I've been trying for hours to figure out the proper way to handle this situation. 到目前为止,我已经尝试了几个小时,以找出解决这种情况的正确方法。 I'm not a Rails developer by trade, I build iOS apps, so please let me know if I'm going about this the wrong way or completely missed the "right" way of doing this. 我不是行业上的Rails开发人员,而是构建iOS应用程序,所以请让我知道我是错误的方法还是完全错过了“正确”的方法。

I solved this using routes and the 'rails_serve_static_assets'. 我使用路线和“ rails_serve_static_assets”解决了这个问题。 I had to move the local images out of the public folder in order to avoid Nginx from returning the images before hitting the Rails app. 为了避免Nginx在点击Rails应用之前返回图像,我不得不将本地图像移出公共文件夹。 The route ended up like so: 路线最终如下所示:

match '/images/(*path)', :to => redirect { |params, request|  
  "#{Rails.configuration.assets.aws_url}#{request.path}"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM