How to serve webpacked assets of Rails app in Elastic Beanstalk container?
I'm using Rails5 app and deployed it to EB container successfully.
But webpacked assets -- served in public/packs
directly, return 404 in production environment.
In current situation, I set RAILS_SKIP_ASSET_COMPILATION = false
so I precompile assets before deploying the app everytime.
I used to use heroku as a production environment and everything went ok at that time.
here is my 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
what I tried...
I tried to change public_output_path from packs to assets. but same error persists...
Solution 1:
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;
}
Then run sudo /etc/init.d/nginx restart
.
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.
See this post by Maria Luisa Carrion D. to see how to automate the nginx config.