Christopher Reid
Christopher Reid

Reputation: 4482

Serving static files with NGINX for UNICORN served app

I've just set up a Sinatra app running on Unicorn and served through a socket by NGINX.

I'm trying to get NGINX to serve my static assets with this in my nginx config:

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

server {
  listen 80 default;
  server_name localhost;
  root /home/ubuntu/app;

  location ^~ /public/ {
    root /home/ubuntu/app;
    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;
}

But my unicorn logs are still showing that it is serving the files

[03/Oct/2014 19:25:05] "GET /javascript/map.js HTTP/1.0" 304 - 0.0016
[03/Oct/2014 19:25:05] "GET /javascript/geopo.js HTTP/1.0" 304 - 0.0030
[03/Oct/2014 19:25:05] "GET /images/logo.png HTTP/1.0" 304 - 0.0022

etc.

What am I missing?

Upvotes: 2

Views: 2639

Answers (1)

CDub
CDub

Reputation: 13354

You'll want to explicitly tell the web server to load those files via a location directive.

Here's an example for what I use with Unicorn to have the web server load assets:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  root /home/ubuntu/app/public;
  expires max;
  add_header Cache-Control public;
  log_not_found off;
}

Upvotes: 4

Related Questions