Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 432
Niels Kristian Avatar asked Dec 27 '22 12:12

Niels Kristian


2 Answers

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

like image 199
Niels Kristian Avatar answered Jan 02 '23 05:01

Niels Kristian


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.

like image 33
Kevin A. Naudé Avatar answered Jan 02 '23 05:01

Kevin A. Naudé