2

I'm trying to configure two applications to be served up by nginx. The first seems to be working fine and I duplicated the config for the second, however I'm having some problems.

I'm running a Sinatra server backed with Unicorn using sockets to pass the upstream information. I can reach the root domain fine (api.richardson.co.nz) and everything seems to be working ok, however as soon as I try to access a path off the root domain (api.richardson.co.nz/games) I get a "not found" error.

Here is my nginx conf file:

worker_processes 1;
user nginx web;
pid /tmp/nginx.pid;
error_log /var/www/knowyourgenre.com/shared/log/nginx.error.log;

events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # "on" if nginx worker_processes > 1
  use epoll; # enable for Linux 2.6+
}

http {
  include mime.types;
  default_type application/octet-stream;
  access_log /tmp/nginx.access.log combined;
  sendfile on;
  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
  tcp_nodelay off; # on may be better for some Comet/long-poll stuff
  gzip on;
  gzip_http_version 1.0;
  gzip_proxied any;
  gzip_min_length 500;
  gzip_disable "MSIE [1-6]\.";
  gzip_types text/plain text/html text/xml text/css
             text/comma-separated-values
             text/javascript application/x-javascript
             application/atom+xml;

  upstream app_server {
    server unix:/var/www/knowyourgenre.com/current/unicorn.sock fail_timeout=0;
    server localhost:8080 fail_timeout=0;
  }

  upstream api {
    server unix:/var/www/api/unicorn.sock fail_timeout=0;
    server localhost:8081 fail_timeout=0;
  }

  server {
    listen 80; # for Linux
    client_max_body_size 4G;
    server_name .knowyourgenre.com;
    keepalive_timeout 5;
    root /var/www/knowyourgenre.com/current/public/;
    try_files $uri/index.html $uri.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /path/to/app/current/public;
    }
  }

  server {
    listen 80; # for Linux
    client_max_body_size 4G;
    server_name .richardson.co.nz;
    keepalive_timeout 5;
    root /var/www/api;
    try_files $uri/index.html $uri.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://api;
    }

    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /path/to/app/current/public;
    }
  }

}

Would an error in the upstream server cause a "not found" to be returned? I'm pretty sure everything is working within the Sinatra server but I may have missed something.

Samuel
  • 177
  • 2
  • 6

1 Answers1

1

Add a slash to the end of the root value.

root /var/www/api/;

Also, I recommend wrapping 'try_files' in a location.

location / {
    try_files $uri/index.html $uri.html $uri @app;
}
h0tw1r3
  • 2,746
  • 18
  • 17
  • Hmm, no luck. I replaced the config, restarted nginx with a hup and I'm still getting the same error. I'm starting to think it might be a problem with the API rather then nginx. – Samuel Jul 30 '11 at 12:22
  • This was an error with the Unicorn server rather then Nginx. Thanks for posting a reply though! – Samuel Jul 31 '11 at 03:11
  • Hi Samuel, How did you fix this? – Pravin Mishra Jul 11 '13 at 05:12