Search code examples
ruby-on-railsnginxcapistranounicornlinode

Rails/Nginx not serving JS and CSS


I deployed a Rails 3.2.8 application via Capistrano, with asset pipeline enabled, to my Linode server.

It is running nginx + unicorn.

When I visit my application, the minimised JS and CSS are not being served, although the assets are present in <RAILS_DIR>/public/assets.

$ tree assets
assets
|-- application-66e477d6fd8cf088e8be44affeead089.css
|-- application-66e477d6fd8cf088e8be44affeead089.css.gz
|-- application-7d3ead38a0b5e276a97d48e52044ac31.js
|-- application-7d3ead38a0b5e276a97d48e52044ac31.js.gz

In my application, I can see those exact files not being found:

error

This is my nginx configuration:

server {
  listen 80 default deferred;
  server_name me.example.com;
  root /home/kennym/apps/app/current/public;

  location ^~ /assets/ {
    add_header Last-Modified "";
    add_header ETag "";
    gzip_static on;
    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;
}

Can you guess what is wrong?


Solution

  • location ^~ /assets/ should be location ~ ^/assets/.

    The former is does not match /assets/, the latter is matches a pattern that starts with /assets/

    Update your nginx config to get caching and pre-gzipped file serving working again.