How to setup nginx to make php works in site subdirectories

Francis Daly francis at daoine.org
Sun Feb 12 20:09:54 UTC 2012


On Sun, Feb 12, 2012 at 01:04:30PM -0500, dimentiy wrote:

Hi there,

> Yes, I have another 'location' but within the another 'server'

If you have no other location{} in the server{} block that is being used,
and you can see from the access log that the browser is actually making the
request and not using a cached version, then I'm afraid that I have no
idea what your system is doing.

> Exactly I have four 'servers' with different root. Like
> 'one.example.com', 'two.example.com', 'three.example.com', 
> 'four.example.com'.
> Each of them are similar and looks like I posted above as I said the
> difference is only in root (location / { root xxx; }).

When I use a config like what you posted, I see no problem.

(Generally, you don't put "root" inside a location{} block unless you
know why you are doing it. Leave it inside the server{} block and things
are clearer.)

Can you provide an exact config file that shows the problem, that I can
copy-paste and demonstrate the problem myself? Usually, if you begin
with a simple config file and just add a few lines at a time, it will
become clear at what point things stopped working as you expect.

> You say that verything is fine and should already work. Sounds cool.
> I'm dummy with nginx and php-fpm.

Using this config file

===
events {
    worker_connections  1024;
    debug_connection 127.0.0.1;
}

http {
    server {
        listen       8000;
        server_name one.example.com;
        root /usr/local/nginx/one;
        index index.html index.htm index.php;

        location / {
        }
        location ~ php {
            fastcgi_pass   backend;
            include fastcgi.conf;
        }
    }
    server {
        listen       8000;
        server_name two.example.com;
        root /usr/local/nginx/two;
        index index.html index.htm index.php;

        location ~ php {
            fastcgi_pass   backend;
            include fastcgi.conf;
        }
    }
    upstream backend {
        server unix:php.sock;
    }
}
===

I get the expected results from each of

curl -i -H Host:\ one.example.com  http://localhost:8000/
curl -i -H Host:\ one.example.com  http://localhost:8000/get.php
curl -i -H Host:\ one.example.com  http://localhost:8000/sub/get.php

curl -i -H Host:\ two.example.com  http://localhost:8000/
curl -i -H Host:\ two.example.com  http://localhost:8000/sub/
curl -i -H Host:\ two.example.com  http://localhost:8000/sub/index.php
curl -i -H Host:\ two.example.com  http://localhost:8000/sub/get.php

where I see (respectively) the content of /usr/local/nginx/one/index.htm,
and then the php-processed output of /usr/local/nginx/one/get.php,
/usr/local/nginx/one/sub/get.php, /usr/local/nginx/two/index.php,
/usr/local/nginx/two/sub/index.php (twice), and
/usr/local/nginx/two/sub/get.php,

The only difference between

    include fastcgi.conf;

and

    include fastcgi_params;

is that the former includes the line

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

so that it doesn't have to be added separately.

(Note also that the "~ php" locations are probably not exactly what you
want on a live site.)

If you still have a problem with your configuration, please include the
usual debug details:

* what did you do
* what did you see
* what did you expect to see

and someone may have a better chance of reproducing what you are seeing.

> Could you tell, please, how to enable debug log?

http://nginx.org/en/docs/debugging_log.html
http://wiki.nginx.org/Debugging

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list