Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm having quite a bit of trouble configuring nginx to serve my Rails app's static files; I mean all the files in the app's public/ folder, not just the files in Rails' asset pipeline. It seems I'm able to get things mostly working but I can't figure out how to put everything together. My app is installed in a subdir on the server (/dashboard) and not the web root.

Here are excerpts from my various config files:

config.ru:

map Dashboard::Application.config.relative_url_root || "/" do
  run Rails.application
end

environments/production.rb:

config.relative_url_root = "/dashboard"
config.assets.prefix = "/dashboard/assets/"

nginx.conf:

location /dashboard/ {

    root /srv/XXX/dashboard/public;

    location /dashboard/assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }

    try_files $uri @unicorn;

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

location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn_server;
}

upstream unicorn_server {
    server unix:/srv/XXX/dashboard/tmp/sockets/unicorn.sock fail_timeout=0;
}

With this configuration I'm able to serve the Rails app itself using unicorn and the files inside the public/assets/ folder using nginx, but the rest of the files in the public/ folder are routed to Rails and end up returning a 404 status code. I know this is because the server is looking for (for example) /dashboard/robots.txt which doesn't exist inside the public/ folder. I believe I need to use alias to manipulate the paths however I haven't been able to put everything together to get Rails to correctly serve the app and nginx to serve the static files.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.