Nginx multiple php sites

Hi,
I’ve been searching (last two days) to find how to setup multiple php
sites
using nginx. Couldn’t find any documentation around it, hence writing it
here.
I’ve two codeignetor (php framework) in two different folders and served
from same domain. For an example,

example.com => /var/www/example/index.php
example.com/blog => /var/www/example/blog/index.php

I made the first one to work using the following code. Can someone help
me
to modify this config to fit the second requirement?!
(the configurations I tried doesn’t pass the query strings - hence
pasting
the working version)

server {
listen 80;
server_name example.com;

location / {
    root   /var/www/example/;
    try_files  $uri  $uri/ /index.php?$args;
    index index.php index.html index.htm;
}

location ~^ \.php$ {
    root  /var/www/example/;
    fastcgi_pass 127.0.0.1:9000;
    index  index.php;
    fastcgi_param SCRIPT_FILENAME 

$document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Thanks in advance.

Posted at Nginx Forum:

Hi,

Simply changing server_name, root should work . If you are using a
different port for different fastcgi process pool you have to change
that
too.

Perhaps this didnt work for you because you have

root /var/www/example/;

in the php location . Move up the root in location / to the server {}
level and delete the root from php location . This is also the best
approach as per nginX docs

I also think CodeIgniter needs the PATH_INFO environment variable set.

Moreover, there is a potential security breach in your current
configuration
https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/
.
The recommended way (when using the same machine for front-end and
back-end
services) it to use try_files to check for the existence of the invoked
PHP
script. However, since using try_files with fastcgi_split_pathinfo
wreaks
havoc (that won’t fix, not a bug… oO)
http://trac.nginx.org/nginx/ticket/321, I would recommend patching you
PHP location like the following:

location ~^ .php$ {

root /var/www/example/; # You should follow Anoop piece of advice

index  index.php;
fastcgi_pass 127.0.0.1:9000;

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

$fastcgi_script_name is a system path to a file,

while $uri is… an URI which might not always correspond to a file

try_files $fastcgi_script_name =404; # Checking PHP script existence

A trick to go beyond the bug/‘feature’ using try_files with

fastcgi_split_pathinfo
set $path_info $fastcgi_path_info
fastcgi_param PATH_INFO $path_info;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

}

Hope I helped,

B. R.

— Original message —
From: “B.R.” [email protected]
Date: 15 July 2014, 19:16:19

I also think CodeIgniter needs the PATH_INFO environment variable set.

This is not true, as you can choose of using PATH_INFO, QUERY_STRING,
REQUEST_URI and so on, so this tricks with path_info is excessive.
Just use try_files and fastcgi_split_path_info

include fastcgi_params;

On Tue, Jul 15, 2014 at 4:31 PM, Anoop A. wrote:

Perhaps this didnt work for you because you have

root /var/www/example/;

in the php location . Move up the root in location / to the server {}
level and delete the root from php location . This is also the best
approach as per nginX docs


Anoop P Alias
GNUSYS


nginx mailing list
[email protected]
http://mailman.nginx.org/mailman/listinfo/nginx

If you use try_files with fastcgi_split_path_info, do not try to set
PATH_INFO with $fastcgi_path_info directly as it will be empty.

If PATH_INFO is not set (as it might not be required like you point it
out)
and stick with the $fastcgi_script_name of the split directive, then the
PATH_INFO 2-lines trick is indeed useless but the rest is correct and
recommended.

B. R.

On Tue, Jul 15, 2014 at 09:15:51AM -0400, devsathish wrote:

Hi there,

example.com => /var/www/example/index.php
example.com/blog => /var/www/example/blog/index.php

I made the first one to work using the following code. Can someone help me
to modify this config to fit the second requirement?!

What request do you make, that does not return the response that you
expect?

location / {
    root   /var/www/example/;
    try_files  $uri  $uri/ /index.php?$args;
    index index.php index.html index.htm;
}

I suspect that you will want some requests to “fall back” to
/blog/index.php instead of /index.php.

Possibly using a “location /blog/ {}” would help with that.

If that doesn’t point you in the right direction, can you be very
specific
in one example of what you do / what you get / what you want to get?

f

Francis D. [email protected]