# Question about nested locations / PHP

**URL:** <https://www.ruby-forum.com/t/question-about-nested-locations-php/217723>\
**Category:** NGINX\
**Created:** [March 9, 2012, 11:13pm UTC](https://www.ruby-forum.com/t/question-about-nested-locations-php/217723 "2012-03-09T23:13:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 9, 2012, 11:13pm UTC](https://www.ruby-forum.com/t/question-about-nested-locations-php/217723/1 "2012-03-09T23:13:33Z")

</div>

Not sure if this is the right place to post this; apologies in advance  
if it is!

I’m having a bit of trouble with nested locations and PHP. I have a  
setup where I want all PHP files to go through fastcgi, and have an  
expiry of epoch. However, one particular PHP file is used to generate  
CSS stylesheets, and I want this file to have an expiry of 30 days. My  
current code looks like this:

location ~ .php$  
{  
location ~ /css.php  
{  
expires 30d;  
add\_header Pragma public;  
add\_header Cache-Control “public”;  
}

expires epoch

// Fastcgi stuff…  
}

I’ve tried various combinations (having the nested location at the end,  
etc) but all that happens is the css.php file doesn’t get sent through  
the fastcgi process, and the raw PHP code is sent to the client. I’ve  
messed up somewhere, and I’m new to nginx (just migrated from Apache) so  
if someone could point out my error / give a quick explanation of how  
nested locations work, it would be ideal!

Thanks in advance!

Adrian

---

<div class="post-metadata">

**Author:** ![Adrian\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@Adrian\_H](https://www.ruby-forum.com/u/Adrian_H)\
**Post date:** [March 10, 2012, 1:48am UTC](https://www.ruby-forum.com/t/question-about-nested-locations-php/217723/2 "2012-03-10T01:48:18Z")

</div>

On Fri, 2012-03-09 at 22:12 +0000, Adrian H. wrote:

> location ~ /css.php

You don’t need nested locations. This should work:

location = /css.php {  
expires 30d;  
add\_header Pragma public;  
add\_header Cache-Control “public”;  
include fastcgi\_params;  
fastcgi\_pass …  
}

location ~ .php$ {  
expires epoch  
include fastcgi\_params;  
fastcgi\_pass …  
}

Information on the order of processing can be found here:

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration)**
>
> nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port \*:80:

> **[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

Regards,  
Cliff

---

<div class="post-metadata">

**Author:** ![Adrian\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@Adrian\_H](https://www.ruby-forum.com/u/Adrian_H)\
**Post date:** [March 10, 2012, 2:41am UTC](https://www.ruby-forum.com/t/question-about-nested-locations-php/217723/3 "2012-03-10T02:41:00Z")

</div>

Thanks. Could it be done with nested locations at all? Do I really have  
to include all the PHP config for each block?

-Adrian

---

<div class="post-metadata">

**Author:** ![Adrian\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@Adrian\_H](https://www.ruby-forum.com/u/Adrian_H)\
**Post date:** [March 10, 2012, 11:12pm UTC](https://www.ruby-forum.com/t/question-about-nested-locations-php/217723/4 "2012-03-10T23:12:31Z")

</div>

On Sun, 2012-03-11 at 01:15 +0400, Maxim D. wrote:

> And, as Cliff already suggested, you don’t really need nested  
> locations here, it’s better to define exact location for  
> /css.php, i.e. something like this:
> 
> ```
> location = /css.php {
> expires 30d;
> fastcgi_pass ...
> ...
> }
> 
> ```

I was concerned that using an absolute location might not be general  
enough, since I seem to see a lot of PHP apps that use URLS like  
“/css.php/these/are/args”, which might cause this location to be skipped  
if css.php happened to use that scheme.

Regards,  
Cliff

---

<div class="post-metadata">

**Author:** ![Adrian\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@Adrian\_H](https://www.ruby-forum.com/u/Adrian_H)\
**Post date:** [March 10, 2012, 10:16pm UTC](https://www.ruby-forum.com/t/question-about-nested-locations-php/217723/5 "2012-03-10T22:16:27Z")

</div>

Hello!

On Fri, Mar 09, 2012 at 10:12:45PM +0000, Adrian H. wrote:

> {  
> }
> 
> I’ve tried various combinations (having the nested location at  
> the end, etc) but all that happens is the css.php file doesn’t  
> get sent through the fastcgi process, and the raw PHP code is  
> sent to the client. I’ve messed up somewhere, and I’m new to  
> nginx (just migrated from Apache) so if someone could point out  
> my error / give a quick explanation of how nested locations  
> work, it would be ideal!

You have to define the “fastcgi\_pass” directive in both locations  
for this to work.

And, as Cliff already suggested, you don’t really need nested  
locations here, it’s better to define exact location for  
/css.php, i.e. something like this:

```
location = /css.php {
    expires 30d;
    fastcgi_pass ...
    ...
}

location ~ \.php$ {
    expires epoch;
    fastcgi_pass ...
    ...
}

```

Maxim D.
