Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx doesn't serve static assets in Rails 3

Stackoverflowers. I have a problem with my Rails nginx configuration. I'm running a Rails 3.0.12 app, and I'm quite new to nginx.

I can't seem to get nginx to serve static assets. For every request in /public folder I get 404. I'm posting the nginx configuration I got so far. Maybe I missed something

nginx.conf:

user rails;
worker_processes  1;
daemon off;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections  2048;
}

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  access_log  /var/log/nginx/access.log;

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;

  keepalive_timeout  65;

  gzip  on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  server_names_hash_bucket_size 64;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

sites-enabled/project.conf:

upstream project {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  # for UNIX domain socket setups:
  server unix:/tmp/project.socket fail_timeout=0;
}

server {
  listen 80;
  root /srv/www/project/current/public;
  passenger_enabled on;
  server_name dev.project.eu;
  server_name *.dev.project.eu;

  location / {
      #all requests are sent to the UNIX socket
      proxy_pass  http://project;
      proxy_redirect     off;

      proxy_set_header   Host             $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

      client_max_body_size       10m;
      client_body_buffer_size    128k;

      proxy_connect_timeout      90;
      proxy_send_timeout         90;
      proxy_read_timeout         90;

      proxy_buffer_size          4k;
      proxy_buffers              4 32k;
      proxy_busy_buffers_size    64k;
      proxy_temp_file_write_size 64k;
      root /srv/wwww/project/current/public;
    }

}

I've tried removing the location / block from project.conf, but it didn't do anything, the assets are still not visible.

I am also aware of serve_static_assets switch in Rails, but I'd rather have nginx serve those assets, as it should do so.

like image 596
Michał Avatar asked May 02 '12 09:05

Michał


1 Answers

You need to add something like that (documentation on locations):

location / {
  try_files $uri @ruby;
}

location @ruby {
    proxy_pass  http://project;
}
like image 135
Dmitry Verhoturov Avatar answered Oct 23 '22 03:10

Dmitry Verhoturov