Nginx: location 404 with try_files under a different root directory

Andru asked:

I’m in the process of moving a wiki I administer ( to a new server running Nginx. I’m accustomed to Apache; this is my first time working with Nginx and I’m having trouble adapting the rewrites and aliases I had set up in Apache. The directory layout is as follows

/html               (server root)
    /html/w         (mediawiki)
    /html/community (vanilla)
    /html/blog      (wordpress)
/sso                (Zend Framework SSO solution)

As you can see, the sso solution has it’s own publicly accessible application root directory, which I’m attempting to set as the root for the location directive, but it seems like the following try_files still attempts to fall back to the server root.

Here’s my config:

server {

  root /var/www/v-practicalplants/html;
  access_log /var/log/nginx/;
  error_log /var/log/nginx/;

  include /etc/nginx/biowikifarm_listen80_443.conf;
  include /etc/nginx/biowikifarm_generics.conf;
  include /etc/nginx/biowikifarm_generic_php.conf;

  disable_symlinks off;

  location = / {
    # Redirect domain-only access to wiki:
    rewrite "^[/]?$" /wiki/ permanent;

  location /wiki {
    #rewrite "^/wiki[/]?([^?]*)(?:\?(.*))?" /w/index.php?title=$1&$args last;
    try_files $uri $uri/ /w/index.php?title=$uri&$args;
  location /w/media {
    location ~ ^/w/media/thumb/(archive/)?[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ {
      try_files $uri @thumb_w;
  location @thumb_w {
    rewrite ^/w/media/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /w/thumb.php?f=$1&width=$2;
    rewrite ^/w/media/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /w/thumb.php?f=$1&width=$2&archived=1;
    include /etc/nginx/fastcgi_params; fastcgi_param SCRIPT_FILENAME  $document_root/w/thumb.php; fastcgi_pass php5-fpm-sock;

  location /sso {
    root /var/www/v-practicalplants/sso/public;
    try_files $uri $uri/ /index.php

This config gives me a 404 if I navigate to /sso

Is there something obviously wrong here?

My answer:

You need to use alias instead of root here.

Your existing configuration tries to load /var/www/v-practicalplants/sso/public/sso/index.php, which is probably not what you want. Regardless of which location it’s in, root always specifies the document root, i.e. the location of /.

Use alias instead to specify the location of the location.

location /sso/ {
    alias /var/www/v-practicalplants/sso/public;

View the full question and any other answers on Server Fault.

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