I recently started using Nginx server which seems to provide the best security options and also the highest performance for the websites. As I migrated from XAMPP which is one of the simplest ones to setup, I found it hard to setup multiple sites in subdirectories using Nginx. As I struggled to find out the correct configuration for setting it up, I thought it deserved a blog post and here it is.
I was trying to setup three websites under the root folder (in my case /usr/share/nginx/www) in an Ubuntu server and all of them are PHP based site. One is PHPMyAdmin, the other is an yii based PHP website and another website with a single PHP file(an API for a mobile app). You can start with the default config file in /etc/nginx/sites-available folder and use fast-cgi configuration for the PHP files. A typical configuration for a PHP site would look like this,
server { listen 80 default; server_name localhost; access_log /var/log/nginx/access.log; root /usr/share/nginx/www; index index.php index.html; location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }If we have to setup multiple sites as sub-directories we need to define multiple location elements in the same config file as below.
For my yii Site1,
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; }For the phpmyadmin Site
location /phpmyadmin/ { root /usr/share/nginx/www/phpmyadmin; try_files $uri $uri/ /index.php?$query_string; } # the image url for this site were including the folder so we can ignore that and provide root location ~ /phpmyadmin/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ { root /usr/share/nginx/www; } # pass the PHP scripts to FastCGI server location ~ /phpmyadmin/.+\.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; }For Site3, which is just a single php file and does not have any images
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; }
Hope this helps someone who is having similar issues with setting up subdirectories for multiple sites. This is still not as straight forward as Xampp is but once we figure it out, its lot simple and everytime we add a subdirectory we can copy over the location settings.