Location - or how to setup sites in subfolders

Daniel L. Miller dmiller at amfes.com
Sun Apr 29 20:06:03 UTC 2012


I'm trying to give nginx a try as an alternative to my current server 
solutions.  Named-based virtual hosting SEEMS straightforward enough - 
but I'm having a devil of a time trying to get subfolder based sites to 
work.  In other words -

egroupware.mydomain.com
roundcubemail.mydomain.com
ldap-account-manager.mydomain.com

for some examples, work just fine.  But trying to get -

www.mydomain.com/egroupware
www.mydomain.com/roundcubemail
www.mydomain.com/ldap-account-manager

to work is an exercise in extreme frustration.  I'm sure it doesn't HAVE 
to be - but Google thus far has let me down in retrieving the 
information I need.  What I've done thus far, for egroupware for example:

I have a site file with:
server {
     server_name www.amfeslan.local egroupware.amfeslan.local;
     root /opt/egroupware;
     index index.php;

     client_max_body_size 8M;
     rewrite ^/egroupware/(.*)$ /$1 last;
     try_files $uri $uri/ /index.php$args;

     include global/php.conf;
}

my global/php.conf has:
location ~ \.php$ {
         # There's gotta be a better way to do this - try and find it.
         # Since nginx's "best" match is this one - need to do rewrites
         # here to get subfolders to work.
         set $php_root $document_root;
         if ($request_uri ~* /egroupware) {
             set $php_root /opt/egroupware;
         }

         # Zero-day exploit defense.
         try_files $uri =404;

         fastcgi_split_path_info ^(.+\.php)(/.+)$;

         include fastcgi_params;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
         fastcgi_pass php;
}

Note that I tried to add the "if" construct in php.conf based on a 
previous post - and I gotta believe there's a better way to do this.  
With or without the if construct, the above site works - but only for 
egroupware.  Revising the site file to:
server {
     server_name www.amfeslan.local egroupware.amfeslan.local;
     root /var/www;
     index index.html;

     client_max_body_size 8M;

     location / {
        try_files $uri $uri/ /index.php?$args;
     }

     location ^~ /egroupware {
        root /opt/egroupware;
        index index.php;
        rewrite ^/egroupware/(.*)$ /$1 last;
        try_files $uri $uri/ /index.php$args;
     }

     include global/php.conf;
}

Doesn't work right.  Can some kind soul help me not only fix this - but 
get a better understanding of how to build these generic constructs?  
Based on experience with other servers, I can't believe folder-based 
controls in nginx can be as difficult as I seem to be making it!
-- 
Daniel



More information about the nginx mailing list