13

I'm getting frustrated with my nginx configuration and so I'm asking for help in writing my config file to serve multiple projects from sub-directories in the same root. This isn't virtual hosting as they are all using the same host value. Perhaps an example will clarify my attempt:

  • request 192.168.1.1/ should serve index.php from /var/www/public/
  • request 192.168.1.1/wiki/ should serve index.php from /var/www/wiki/public/
  • request 192.168.1.1/blog/ should serve index.php from /var/www/blog/public/

These projects are using PHP and use fastcgi.

My current configuration is very minimal.

server {
    listen 80 default;
    server_name localhost;

    access_log /var/log/nginx/localhost.access.log;

    root /var/www;
    index index.php index.html;

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www$fastcgi_script_name;
        include fastcgi_params;
    }
}

I've tried various things with alias and rewrite but was not able to get things set correctly for fastcgi. It seems there should be a more eloquent way than writing location blocks and duplicating root, index, SCRIPT_FILENAME, etc.

Any pointers to get me headed in the right direction are appreciated.

2
  • Out of curiosity, what URL would you expect to be able to access a file /var/www/public/wiki/foo.html at?
    – natacado
    Jan 5, 2011 at 5:39
  • That's a good point, natacado. the main public directory will just be a few miscellaneous files and should never really be used anyway. It's an internal set up so I will have control of that. Hopefully we won't have to find out :)
    – Timothy
    Jan 9, 2011 at 9:12

3 Answers 3

17

Since your projects aren't actually in the same root, you must use multiple locations for this.

location /wiki {
    root /var/www/wiki/public;
}

location ~ /wiki/.+\.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME /var/www/wiki/public$fastcgi_script_name;
}

location /blog {
    root /var/www/blog/public;
}

location ~ /blog/.+\.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME /var/www/blog/public$fastcgi_script_name;
}

Also, put fastcgi_index in your fastcgi_params file and include it at server level, that way you keep your php locations as small as possible.

1
  • 1
    This was exactly the type of configuration I was hoping to avoid... duplication. Alas, if this is "proper" than that's what I shall do. Thank you for your help, Martin!
    – Timothy
    Jan 9, 2011 at 9:10
9

Solves by location+alias:


location / {
   root /var/www/public;
   index index.php;
}
location /blog/ {
   alias /var/www/blog/public/;
   index index.php;
}
location /wiki/ {
   alias /var/www/wiki/public/;
   index index.php;
}

location ~ \.php$ {
   #your fastcgi configuration here 
}

0

Here is what i tried, more details at http://programmersjunk.blogspot.com/2013/11/nginx-multiple-sites-in-subdirectories.html

    location /Site1/ {
            root /usr/share/nginx/www/Site1;
           try_files $uri $uri/ /index.php?$query_string;
    }

    # the images need a seperate entry as we dont want to concatenate that with index.php      
    location ~ /Site1/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
            root /usr/share/nginx/www/Site1;
    }
    # pass the PHP scripts to FastCGI server
    location ~ /Site1/.+\.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            allow 127.0.0.1;
    #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #       # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
    }

location /Site3/ {
            root    /usr/share/nginx/www/Site3;
    }

    # pass the PHP scripts to FastCGI server
    location ~ /Site3/.+\.php$ {
            allow 127.0.0.1;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            #we are directly using the $request_filename as its a single php script
            fastcgi_param SCRIPT_FILENAME $request_filename;
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .