Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I serve assets in /public that are not part of the asset pipeline with puma/nginx?

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.

like image 906
chrisp Avatar asked Jan 23 '16 12:01

chrisp


2 Answers

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 :)

like image 125
DivXZero Avatar answered Oct 19 '22 02:10

DivXZero


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
like image 41
thebenedict Avatar answered Oct 19 '22 02:10

thebenedict