# How to setup nginx to make php works in site subdirectories

**URL:** <https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723>\
**Category:** NGINX\
**Created:** [February 12, 2012, 12:00pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723 "2012-02-12T12:00:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 12, 2012, 12:00pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/1 "2012-02-12T12:00:17Z")

</div>

Hello,

I’m nuewbie in nginx, I’ve installed and setup nginx & php,  
everything is fine except .php files located in site’s subdirectories  
are not processed at all.

i.e. [example.com/download.php](http://example.com/download.php) works fine, but  
[example.com/stuff/dosomething.php](http://example.com/stuff/dosomething.php) is sent to client as plain text.

Please, would any kind soul tell me what should I change to resolve the  
problem.  
Thank you.

I use those settings.

# 

# [example.com](http://example.com)

# 

server {  
listen 80;  
server\_name [example.com](http://example.com);

```
 access_log /var/log/nginx/example.com.access.log;

 location / {
     root /var/www/nginx-default/example.com;
     index index.html index.htm index.php;
 }

```

## Parse all .php file in the /var/www directory

```
 location ~ .php$ {
     fastcgi_split_path_info ^(.+\.php)(.*)$;
     fastcgi_pass backend;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME

```

/var/www/nginx-default/example.com$fastcgi\_script\_name;  
include fastcgi\_params;  
fastcgi\_param QUERY\_STRING $query\_string;  
fastcgi\_param REQUEST\_METHOD $request\_method;  
fastcgi\_param CONTENT\_TYPE $content\_type;  
fastcgi\_param CONTENT\_LENGTH $content\_length;  
fastcgi\_intercept\_errors on;  
fastcgi\_ignore\_client\_abort off;  
fastcgi\_connect\_timeout 60;  
fastcgi\_send\_timeout 180;  
fastcgi\_read\_timeout 180;  
fastcgi\_buffer\_size 128k;  
fastcgi\_buffers 4 256k;  
fastcgi\_busy\_buffers\_size 256k;  
fastcgi\_temp\_file\_write\_size 256k;  
}

## Disable viewing .htaccess & .htpassword

```
 location ~ /\.ht {
     deny all;
 }

```

}

upstream backend {  
server 127.0.0.1:9000;  
}

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 12:12pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/2 "2012-02-12T12:12:04Z")

</div>

Hello,

Maybe you could use virtual conf, ie:

location ~ .php$ {  
root /home/example/public\_html;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/home/example/public\_html$fastcgi\_script\_name;  
include fastcgi\_params;  
}

Regards,  
Joe

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 12:33pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/3 "2012-02-12T12:33:52Z")

</div>

Hello, Joe, I’ve tried to use settings from your post, unfortunately  
nothing changed.  
Scripts in site’s root are processed, but in subdirectories are not (

Posted at Nginx Forum:

> **[Re: How to setup nginx to make php works in site subdirectories](https://forum.nginx.org/read.php?2,222301,222303#msg-222303)**
>
> Hello, I'm nuewbie in nginx, I've installed and setup nginx & php, everything is fine except .php files located in site's subdirectories are not processed at all. i.e. example.com/download.php works fine, but example.com/stuff/dosomething.php is...

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 1:53pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/4 "2012-02-12T13:53:55Z")

</div>

12 февраля 2012, 15:00 от Dmitry T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> problem.  
> server\_name [example.com](http://example.com);  
> fastcgi\_split\_path\_info ^(.+.php)(.\*)$;  
> fastcgi\_ignore\_client\_abort off;  
> location ~ /.ht {  
> deny all;  
> }  
> }
> 
> upstream backend {  
> server 127.0.0.1:9000;  
> }

That isn’t your complete configuration now, is it? Another  
location block (which you haven’t posted) seems to be  
matching your subdirectory requests. Add this to your  
server config and then check your error log to see what’s  
matching your requests - look for log entries that look  
like “using configuration” to find the matching location  
block.

error\_log /var/log/nginx/example.com.error.log debug;  
root /var/www/nginx-default/example.com;

Always set the root directory inside the server configuration  
block, otherwise it will be reset to the --prefix configuration  
argument that nginx was compiled with (run “nginx -V”  
to find out yours).

Max

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 1:22pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/5 "2012-02-12T13:22:16Z")

</div>

Hello Dmitry,

Please try to put the subdirectory name behind /

Regards,  
Joe

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 2:00pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/6 "2012-02-12T14:00:10Z")

</div>

On Sun, Feb 12, 2012 at 06:33:09AM -0500, dimentiy wrote:

Hi there,

> Scripts in site’s root are processed, but in subdirectories are not (

Using your configuration, I’m unable to reproduce your reported output.

There are some tidy-up fixes that could be done to your configuration,  
but what you have should be working already.

I suspect that either it _is_ working but your browser cache is hiding  
that from you; or you have an extra location{} block that means that  
your  
“.php$” one isn’t being used.

Suggestion: create a file  
/var/www/nginx-default/example.com/stuff/new.php  
with the contents:

==

\<?php print\_r($\_GET); ?\>
# stuff/new.php

and look at the result of getting [example.com/stuff/new.php](http://example.com/stuff/new.php). That should  
remove the “browser cache” possibility.

If it is still not what you expect, then something like the debug log  
should show you which location{} is being used for this request.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 6:48pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/7 "2012-02-12T18:48:58Z")

</div>

Hello, Joe,

behind what /?

I’ve tried different variants. But I really misunderstand how it should  
be.

The goal is to make nginx process .php files whereever they located  
begining from site’s root folder.

This settings works fine with scripts in site’s root.

location ~ .php$ {  
root /var/www/nginx-default/example.com;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/var/www/nginx-default/example.com$fastcgi\_script\_name;  
include fastcgi\_params;  
}

This redirects ‘[example.com/popups/popup\_on\_enter.php](http://example.com/popups/popup_on_enter.php)’ to php-fpm but it  
returns ‘File not found’.

location /popups/popup\_on\_enter.php {  
root /var/www/nginx-default/example.com/popups;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/var/www/nginx-default/example.com/popups$fastcgi\_script\_name;  
include fastcgi\_params;  
}

I suppose there sould be variant with one ‘location’ directive  
processing all the files in /var/www/nginx-default/example.com.  
But due to my little experience I can’t realize how to reach the goal.

Thank you.

Posted at Nginx Forum:

> **[Re: How to setup nginx to make php works in site subdirectories](https://forum.nginx.org/read.php?2,222301,222314#msg-222314)**
>
> Hello, I'm nuewbie in nginx, I've installed and setup nginx & php, everything is fine except .php files located in site's subdirectories are not processed at all. i.e. example.com/download.php works fine, but example.com/stuff/dosomething.php is...

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 7:05pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/8 "2012-02-12T19:05:06Z")

</div>

Hello, Francis,

Yes, I have another ‘location’ but within the another ‘server’

Exactly I have four ‘servers’ with different root. Like  
‘[one.example.com](http://one.example.com)’, ‘[two.example.com](http://two.example.com)’, ‘[three.example.com](http://three.example.com)’,  
‘[four.example.com](http://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; }).

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

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

Thank you.

## Francis D. Wrote:

> There are some tidy-up fixes that could be done to  
> your configuration,  
> but what you have should be working already.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,222301,222315#msg-222315](http://forum.nginx.org/read.php?2,222301,222315#msg-222315)

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 7:15pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/9 "2012-02-12T19:15:02Z")

</div>

Hello, Francis,

I found out how to enable it

error\_log path/to/log;

Thank you.

Posted at Nginx Forum:

> **[Re: How to setup nginx to make php works in site subdirectories](https://forum.nginx.org/read.php?2,222301,222316#msg-222316)**
>
> Hello, I'm nuewbie in nginx, I've installed and setup nginx & php, everything is fine except .php files located in site's subdirectories are not processed at all. i.e. example.com/download.php works fine, but example.com/stuff/dosomething.php is...

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 9:10pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/10 "2012-02-12T21:10:35Z")

</div>

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](http://one.example.com)’, ‘[two.example.com](http://two.example.com)’, ‘[three.example.com](http://three.example.com)’,  
> ‘[four.example.com](http://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](http://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://one.example.com)[http://localhost:8000/](http://localhost:8000/)  
curl -i -H Host:\ [one.example.com](http://one.example.com)[http://localhost:8000/get.php](http://localhost:8000/get.php)  
curl -i -H Host:\ [one.example.com](http://one.example.com)[http://localhost:8000/sub/get.php](http://localhost:8000/sub/get.php)

curl -i -H Host:\ [two.example.com](http://two.example.com)[http://localhost:8000/](http://localhost:8000/)  
curl -i -H Host:\ [two.example.com](http://two.example.com)[http://localhost:8000/sub/](http://localhost:8000/sub/)  
curl -i -H Host:\ [two.example.com](http://two.example.com)[http://localhost:8000/sub/index.php](http://localhost:8000/sub/index.php)  
curl -i -H Host:\ [two.example.com](http://two.example.com)[http://localhost:8000/sub/get.php](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?

> **[A debugging log](http://nginx.org/en/docs/debugging_log.html)**

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 9:28pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/11 "2012-02-12T21:28:36Z")

</div>

Thank you every body, people!

I’m quite stupid… I entirly can’t think… There was \<? ?\> short tags  
disabled, all the scripts use short tags, but only the one I tested  
locating in root directory had \<?php long tag… because I write it  
Damn

I wasted your and my time

Posted at Nginx Forum:

> **[Re: How to setup nginx to make php works in site subdirectories](https://forum.nginx.org/read.php?2,222301,222321#msg-222321)**
>
> Hello, I'm nuewbie in nginx, I've installed and setup nginx & php, everything is fine except .php files located in site's subdirectories are not processed at all. i.e. example.com/download.php works fine, but example.com/stuff/dosomething.php is...

---

<div class="post-metadata">

**Author:** ![dmitry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@dmitry](https://www.ruby-forum.com/u/dmitry)\
**Post date:** [February 12, 2012, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-setup-nginx-to-make-php-works-in-site-subdirectories/216723/12 "2012-02-12T22:52:52Z")

</div>

On Sun, Feb 12, 2012 at 03:28:03PM -0500, dimentiy wrote:

Hi there,

> I’m quite stupid… I entirly can’t think… There was \<? ?\> short tags  
> disabled, all the scripts use short tags, but only the one I tested  
> locating in root directory had \<?php long tag… because I write it

Congratulations on finding that that was the problem.

Now that we know the answer, is there any way it could have been found  
earlier? This could help the next person who has a similar problem and  
is searching the mailing list archive.

I think that

curl -i [http://one.example.com/stuff/new.php](http://one.example.com/stuff/new.php)

would show the “X-Powered-By: PHP” header which indicates that the file  
was handled by php (even if it was just a pass-through): and copying  
new.php to new.txt and trying

curl -i [http://one.example.com/stuff/new.txt](http://one.example.com/stuff/new.txt)

would show the same content but without the PHP header line.

Maybe that would point to the php configuration as being the next place  
to look.

All the best,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
