Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a drupal app and 2 other app written in php, i want use a rewrite uri for drupal and place drupal in virtual root / and the other by their names ... and use the server cache for the img of each app.

ex :

  • xxx.com or xxx.com/ --> /var/www/xxx.com/drupal/
  • xxx.com/app1 or xxx.com/app1/ ----> /var/www/xxx.com/app1/
  • xxx.com/app2 or xxx.com/app2/ ----> /var/www/xxx.com/app2/

I have tested : alias, root in each location, conditional $request_uri, separate config ... and always one error : img path, php path, 404 for one of all app or drupal

so I'm really lost ...

here a test (maybe some good things) :

    # enforce www
if ($host !~* ^(www)) {
    rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;

location / {
    root   /var/www/gplaza.cl/Drupal;
    index  index.php index.html;

    if (!-f $request_filename) {
        rewrite  ^(.*)$  /index.php?q=$1  last;

    if (!-d $request_filename) {
        rewrite  ^(.*)$  /index.php?q=$1  last;

location /app1/ {
    alias /var/www/gplaza.cl/app1/;   
    index index.php;

location /app2/ {
    alias /var/www/gplaza.cl/app2/;   
    index index.php;

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /var/www/xxx.com$fastcgi_script_name;

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {

    set $static_content /var/www/xxx.com/Drupal;

    if ($request_uri ~ ^/app1) {
        set $static_content /var/www/xxx.com/app1;
    if ($request_uri ~ ^/app2) {
        set $static_content /var/www/gplaza.cl/app2;

    expires     30d;
    access_log  off;
    root $static_content;
thank a lot if anybody can help me :)
share|improve this question

1 Answer 1

up vote 1 down vote accepted

The location directives are checked in a predefined order (http://wiki.nginx.org/HttpCoreModule#location). In your case, the regular expression for PHP files is anc cache management are consideres first, then the / location and (if / should not match) the /app1/ and /app2/ directives. As root will always match, /app1/ is never checked.

The solution is simpley: Put the /app1/ and /app2/ before the /-location directive and reload the nginx configuration.


share|improve this answer
thx some much !!!! –  Gilles Grandguillaume Jul 29 '13 at 20:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.