17

This is an AWS question, I'm using the Ruby 2.2 (Puma) platform.

My compiled assets (in /public/assets) are served as expected. The other assets in /public are not being served (404).

Where do I configure this? Is this an nginx problem? or a puma problem?

Or is this just an AWS image issue?

Here's a live example (robots.txt should be served from the root): http://staging.us-west-2.elasticbeanstalk.com/public/robots.txt

It's also worth mentioning that the default Passenger platform image works out of the box.

chrisp
  • 2,181
  • 4
  • 27
  • 35
  • 2
    Can you give us the names/locations of the other assets in your `public` dir? – Richard Peck Jan 23 '16 at 12:56
  • 1
    You mean like 422.html, 500.html, favicon.ico? – chrisp Jan 23 '16 at 21:52
  • 1
    Yeah, you're saying that you have assets in the public dir which won't show, yet some will. To make a valid comparison, I need to know what you have which won't show – Richard Peck Jan 23 '16 at 22:22
  • The ones that are compiled (/public/assets) are served as expected. The others, listed, do not. – chrisp Jan 23 '16 at 22:29
  • Did you get this figured out? I'm having the same issue. This forum thread is relevant but it's unresolved: https://forums.aws.amazon.com/thread.jspa?messageID=648423 – thebenedict Apr 13 '16 at 13:35
  • 2
    I resolved it by fixing the nginx configuration that is supplied by AWS. (AWS's default config is *wrong* -- objectively, it is wrong.) There is a thread on AWS's community support forums that talks about this. It's been a few months so I don't have a direct link for you. Some people, like the bottom answer, choose to let rails serve the assets. I don't like this. So I let nginx, or [your web server], do the static assets serving. SO answer: http://stackoverflow.com/questions/20781682/rails-nginx-puma-static-assets-not-being-served-by-nginx-from-the-tutorial – chrisp Apr 14 '16 at 03:19
  • Very helpful, thanks. Pasted my working config below. – thebenedict Apr 14 '16 at 19:29

3 Answers3

7

So, i'm using the exact same environment and I found the solution with a little google fu:

With rails 4+, in the file:

/config/environments/production.rb

you should find the following lines near the top of the file

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

That's all fine and dandy since we're using passenger(nginx or apache), but Puma doesn't handle this for us :)

To resolve this...

In your AWS console, go to your elastic beanstalk dashboard for the project in question, and click 'Configuration' on the left-hand menu.

Now click the little gear icon in the box titled 'Software Configuration'

Now you should see a table under 'Environment Properties', enter 'RAILS_SERVE_STATIC_FILES' into a new field under 'Property Name', then type 'true' (without the quotes) into the value field, hit apply.

Viola! Now your project is serving static files :)

DivXZero
  • 601
  • 1
  • 7
  • 17
  • 8
    The application server shouldn't serve static assets in production. It's inefficient for requests for static files to hit the Rails app. http://guides.rubyonrails.org/configuring.html – thebenedict Apr 13 '16 at 13:47
7

In case it helps anyone, or someone knows how to improve it, here's the nginx config that finally got it working for me. In /.ebextensions/01_files.config:

files:
    "/etc/nginx/conf.d/webapp_healthd.conf" :
        mode: "000755"
        owner: root
        group: root
        content: |
            upstream my_app {
              server unix:///var/run/puma/my_app.sock;
            }

            log_format healthd '$msec"$uri"'
                            '$status"$request_time"$upstream_response_time"'
                            '$http_x_forwarded_for';

            server {
              listen 80;
              server_name _ localhost; # need to listen to localhost for worker tier
              root /var/app/current/public;

              if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
                set $year $1;
                set $month $2;
                set $day $3;
                set $hour $4;
              }

              access_log  /var/log/nginx/access.log  main;
              access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

              try_files $uri/index.html $uri @my_app;

              location @my_app {
                proxy_pass http://my_app; # match the name of upstream directive which is defined above
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              }

              location /assets {
                alias /var/app/current/public/assets;
                gzip_static on;
                gzip on;
                expires max;
                add_header Cache-Control public;
              }
            }
    "/opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/usr/bin/env bash
            rm /etc/nginx/conf.d/webapp_healthd.conf.bak
            rm /etc/nginx/conf.d/custom.conf            
            service nginx restart
thebenedict
  • 2,539
  • 3
  • 20
  • 29
  • I was reluctant to go this route, in case EB makes some changes to this file. But there were several other modifications I needed to make to the EB nginx config, so I finally went with this approach. – Eric Walker Jan 21 '17 at 02:00
  • is this still working for you? im in the same situation, tweaking nginx configuration doesn't seem to be working. its a little unclear which nginx files are actually being used for what. i modified the one you did above -via ssh to verfiy itll work, after restarting nginx, no success yet – Ricky Brown Sep 12 '17 at 04:18
  • @RickyBrown Yes, still working. I don't have a suggestion for you unfortunately. – thebenedict Sep 18 '17 at 12:49
-3

I needed to simply run bundle exec rake assets:precompile

Arthur
  • 1,441
  • 1
  • 13
  • 17