Nginx location redirecting improperly

Kevin Brown asked:

My nginx.conf:

http {
  include mime.types;

  default_type application/octet-stream;

  # click tracking!
  access_log /var/log/nginx/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].";

  include /etc/nginx/sites-enabled/*;



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

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

  server {

    listen 80;
    server_name hmaapp101;

    # Application root, as defined previously
    #root /var/www/nvh/public;
    #try_files $uri/index.html $uri.html $uri;
    location / {
        root /var/www/nvh/public;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unix:///tmp/nvhbase.sock;
    }

    location ^~ /tracker/ {
        root /var/www/tracker/public;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unix:///tmp/tracker.sock:/tracker/;
    }
  }
}

When I go to http://myapp/tracker I get redirected to myapp/tracker/tracker/users/sign_in instead of myapp/tracker/users/sign_in.

I have tried a million variations, but either get a 500 too many redirects, or this.

I changed a few things fiddling around, thought I changed it back, but now I’ve broken something…shoulda backed up.

Please assist. Is this bad practice to run two rails apps like this?

My answer:


You need to set root in the server block, not in the location block. This is one of the most common nginx mistakes.

In the location where you need to override the document root, use alias instead of root so that the path is translated properly.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.