west
west

Reputation: 35

nginx won't serve all my assets (Ruby on Rails 4)

I have a Ruby on Rails app running under unicorn/nginx. The problem is that nginx won't serve all my assets. The CSS & JS files seem to be loaded but the images aren't served.

Here is my nginx conf file :

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

server {
  listen 80 default deferred;
  server_name exemple.com;

  root /home/aiccrr/aiccrr/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;
  }

  location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

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

unicorn.rb file :

root = "/home/aiccrr/aiccrr"

working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.aiccrr.sock"
worker_processes 2
timeout 30

I did rake assets:precompile at least 10 times today and added this line to the production.rb :

config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif]

I'm running out of ideas. Do you have any idea please?

Thank you in advance.

Upvotes: 1

Views: 2649

Answers (1)

CuriousMind
CuriousMind

Reputation: 34145

Try the following:

Disable Rails asset server

# config/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = false

try following changes to your `nginx.conf

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

server {
  listen 80 default deferred;
  server_name exemple.com;

  location / {
    root /home/aiccrr/aiccrr/public;

    location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
      expires max;
      gzip_static on;
      add_header Cache-Control public;
      break;
    }

    # If the file exists as a static file serve it directly without
    # running all the other rewrite tests on it
    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      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 1G;
  keepalive_timeout 10;
}

If it still doesn't work, post back with result of Log files & also URL to example nginx where you can replicate this issue. Thanks

Upvotes: 0

Related Questions