4

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?.

Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101
Niels Kristian
  • 8,661
  • 11
  • 59
  • 117
  • Do you want the file to be served by NginX, or by unicorn? – Kevin A. Naudé Dec 19 '12 at 13:52
  • Well, I think serving it from nginx is how it's suggested to be done by default here https://github.com/mperham/sidekiq/issues/260 However putting it into public/admin/sidekiq is another solution suggested as a hack, when the the default doesn't work – Niels Kristian Dec 19 '12 at 14:12
  • What is the exact response you get from your browser when you visit ``http://www.myapp.com/admin/sidekiq/stylesheets/application.css``? – Kevin A. Naudé Dec 19 '12 at 14:46

2 Answers2

9

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

Niels Kristian
  • 8,661
  • 11
  • 59
  • 117
  • You most likely want nginx to serve assets directly without requesting them from Rails. – Smar Dec 19 '12 at 15:22
  • Yes, apart from the fact that normally I use asset_sync to serve my assets from cloud front. But in this case it's a backend admin tool, which is used very rarely compared to everything else. And since it's assets that is comming from a gem, I wouldn't know how to (if at all possible) to serve those by cloud front..? – Niels Kristian Dec 19 '12 at 15:28
  • Don’t normal rake assets:precompile pick those too? – Smar Dec 19 '12 at 15:47
1

Sorry, Niels, I think I am going to need more information in order to help you. Could you please enable logging, and then post the logged responses into your question.

To enable logging, please include the following lines in your config, and then either reload the config, or restart NginX.

    log_format my_log_format
        '$host $remote_addr - $remote_user [$time_local]  $status '
        '"$request" $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    error_log  /home/deployer/myapp_error.log;
    access_log /home/deployer/myapp_access.log my_log_format;

I will revise my answer according to what we find in the logs.

Kevin A. Naudé
  • 3,992
  • 19
  • 20