user468311
user468311

Reputation:

Assets not served with RoR 4, Nginx, Unicorn

I've deployed my RoR 4 app using Capistrano 2, Unicorn, Nginx. The problem is that I get 404 on assets(stylesheets, javascripts).

Here's Nginx access log:

89.0.40.233 - - [16/Mar/2014:08:24:26 +0000] "GET /stylesheets/application.css HTTP/1.1" 404 650 "http://host.cloudapp.net/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36"
89.0.40.233 - - [16/Mar/2014:08:24:26 +0000] "GET /javascripts/application.js HTTP/1.1" 404 650 "http://host.cloudapp.net/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36"

My assets are in the app folder:

azureuser@host:~/apps/testify/current/public$ ls -a assets
.                                                    application-d65a0eaefe6ca2eef9400045f94ab52b.js
..                                                   application-d65a0eaefe6ca2eef9400045f94ab52b.js.gz
application-71e2591e9586afebf3fb4ff70aaae199.css     manifest-a348973e84698f7d898e8021bd6e5388.json
application-71e2591e9586afebf3fb4ff70aaae199.css.gz

My Nginx config:

upstream unicorn {
  server unix:/tmp/unicorn.testify.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  root /home/azureuser/apps/testify/current/public;

  location ^~ /assets/ {
    expires max;
    add_header Cache-Control public;
  }

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

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

Where do I start looking?

Upvotes: 7

Views: 2977

Answers (4)

manroe
manroe

Reputation: 1775

For me, the root path was wrong in my nginx config, and I was experiencing this exact issue. Reference: https://stackoverflow.com/a/25341195/2544629

Upvotes: 1

user2063784
user2063784

Reputation: 56

Adding to the answer of Sergey Moiseev

Try fetching the assets directly with the url /assets/javascripts/application-d65a0eaefe6ca2eef9400045f94ab52b.js.If it doesnt work the your issue is in nginx rather than rails.

Also please check whether you are fetching the file with right fingerprint. In this case check whether its application-d65a0eaefe6ca2eef9400045f94ab52b.js or someother application.js with a different fingerprint. I got a similar issue with multiple servers.

Upvotes: 1

rosenfeld
rosenfeld

Reputation: 1790

I would guess Capistrano is not using "production" as the environment name for Unicorn (-E option). "ps aux|grep unicorn" would probably tell you which environment it's using.

Upvotes: 0

Sergey Moiseev
Sergey Moiseev

Reputation: 2963

As it seems from access.log you just hardcoded application.css/.js in your layout. There is no such files in production public folder because of fingerprint name that asset pipeline gives them (look at your example ls output).

You may read about this here.

Fixing your problem really simple. Replace hardcoded links for application.css/.js with this code:

<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag "application" %>

Upvotes: 10

Related Questions