如何在 Elastic Beanstalk 容器中提供 Rails 应用程序的 webpacked 资产? [英] How to serve webpacked assets of Rails app in Elastic Beanstalk container?

查看:22
本文介绍了如何在 Elastic Beanstalk 容器中提供 Rails 应用程序的 webpacked 资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rails5 应用程序并成功将其部署到 EB 容器.

I'm using Rails5 app and deployed it to EB container successfully.

但是 webpacked assets -- 直接在 public/packs 中提供,在生产环境中返回 404.

But webpacked assets -- served in public/packs directly, return 404 in production environment.

在当前情况下,我设置了 RAILS_SKIP_ASSET_COMPILATION = false 所以我在每次部署应用程序之前预编译资产.

In current situation, I set RAILS_SKIP_ASSET_COMPILATION = false so I precompile assets before deploying the app everytime.

我曾经使用 heroku 作为生产环境,当时一切正常.

I used to use heroku as a production environment and everything went ok at that time.

这是我的config/webpacker.yml:

source_path: app/frontend/javascripts
  source_entry_path: packs
  public_output_path: packs # public/packs/filename-[hash].js
  cache_path: tmp/cache/webpacker

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  extensions:
    - .js
    - .sass
    - .scss
    - .css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

我尝试了什么...

我尝试将 public_output_path 从包更改为资产.但同样的错误仍然存​​在...

what I tried...

I tried to change public_output_path from packs to assets. but same error persists...

推荐答案

我也遇到了这个问题.我不确定您使用的是 Nginx 还是Passenger.但是,如果是 Nginx,您可能希望向 /etc/nginx/conf.d/webapp_healthd.conf 添加一个位置块,如下所示:

I ran into this problem as well. I'm not sure if you're using Nginx or Passenger. But if it's Nginx you'll probably want to add a location block to /etc/nginx/conf.d/webapp_healthd.conf that looks like this:

location /packs {
  alias /var/app/current/public/packs;
  gzip_static on;
  gzip on;
  expires max;
  add_header Cache-Control public;
}

然后运行sudo/etc/init.d/nginx restart.

这应该足以让它工作.但是您需要使用这些自定义设置在您的项目中创建一个 .ebextensions/ 文件,这样它就不会被 Elastic Beanstalk 默认配置覆盖.

That should be enough to get it working. But you'll want to create a .ebextensions/ file in your project with these custom settings so it doesn't get overwritten by the Elastic Beanstalk default config.

参见 this post 由 Maria Luisa Carrion D. 撰写,以了解如何自动化 nginx 配置.

See this post by Maria Luisa Carrion D. to see how to automate the nginx config.

这篇关于如何在 Elastic Beanstalk 容器中提供 Rails 应用程序的 webpacked 资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆