Search code examples
ruby-on-railsnginxunicornassets

Why can't nginx find my assets?


I'm on rails 3.2 and my production setup is using nginx and unicorn.

I have a problem with some assets that a ruby gem called sidekiq uses. However those assets are not being served properly when I request them. My nginx config looks like this:

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

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/deployer/apps/myapp/current/public;

  if (-f $document_root/system/maintenance.html) {
    return 503;my
  }
  error_page 503 @maintenance;
  location @maintenance {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

  location ~ ^/assets/ {
    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;

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
  error_page 503 @maintenance;
  location @maintenance {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }
}

From my browser I can see that it's
e.g. requesting http://www.myapp.com/admin/sidekiq/stylesheets/application.css.

If I ssh into the server and write:

ls /home/deployer/apps/myapp/current/public/admin/sidekiq/stylesheets
application.css  bootstrap.css

You can see that it's actually there. So why isn't it being served?.


Solution

  • Solved it, It was all due to a wrong setup in my production.rb in rails, which made the default behavior fail, so the hack of putting the assets into /public manually isn't necessary anyways.

    I had:

    config.action_dispatch.x_sendfile_header = "X-Sendfile"
    

    Which instead for nginx should be:

    config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
    

    Thanks for all the help :-)