# Need To Run Multiple Location Blocks

**URL:** <https://www.ruby-forum.com/t/need-to-run-multiple-location-blocks/221494>\
**Category:** NGINX\
**Created:** [July 17, 2012, 10:28am UTC](https://www.ruby-forum.com/t/need-to-run-multiple-location-blocks/221494 "2012-07-17T10:28:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 17, 2012, 10:28am UTC](https://www.ruby-forum.com/t/need-to-run-multiple-location-blocks/221494/1 "2012-07-17T10:28:27Z")

</div>

I have the following location blocks:

location ^~ /publish {  
allow 127.0.0.1;

```
  #Allow home
  allow 99.100.101.102;

  deny all;
}

```

Then the following location block to process PHP after:

```
location ~\.php {
  try_files $uri =404;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_intercept_errors on;
  fastcgi_pass 127.0.0.1:9000;
  include /etc/nginx/fastcgi_params;
}

```

The problem is that a request to /publish/execute.php is only hitting  
the first location block, and not hitting the fastcgi PHP execution  
location block. How can I make the publish location block also check for  
PHP files and if so, execute them according to the second location  
block?

Thanks.

Posted at Nginx Forum:

> **[Need To Run Multiple Location Blocks](https://forum.nginx.org/read.php?2,228624,228624#msg-228624)**
>
> I have the following location blocks: location ^~ /publish { allow 127.0.0.1; #Allow home allow 99.100.101.102; deny all; } Then the following location block to process PHP after:

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [July 17, 2012, 10:30am UTC](https://www.ruby-forum.com/t/need-to-run-multiple-location-blocks/221494/2 "2012-07-17T10:30:47Z")

</div>

On Tue, Jul 17, 2012 at 10:42 AM, justin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The problem is that a request to /publish/execute.php is only hitting  
> the first location block, and not hitting the fastcgi PHP execution  
> location block. How can I make the publish location block also check for  
> PHP files and if so, execute them according to the second location  
> block?

Put another .php$ block inside the /publish block.

location ^~ /publish/ {  
…  
location ~ .php$ {  
…  
}  
}

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [July 18, 2012, 12:31am UTC](https://www.ruby-forum.com/t/need-to-run-multiple-location-blocks/221494/3 "2012-07-18T00:31:59Z")

</div>

Edho,

Thanks for the reply. This is a bit sloppy though because let us assume  
I have multiple publish location blocks. Copy and pasting the PHP  
location block multiple times is bad practice, because if I need to make  
changes, I have to update the PHP block multiple times. Is there a way  
to make the PHP location block a variable and use it in multiple  
locations?

Thanks much.

Posted at Nginx Forum:

> **[Re: Need To Run Multiple Location Blocks](https://forum.nginx.org/read.php?2,228624,228658#msg-228658)**
>
> I have the following location blocks: location ^~ /publish { allow 127.0.0.1; #Allow home allow 99.100.101.102; deny all; } Then the following location block to process PHP after:

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [July 18, 2012, 1:12am UTC](https://www.ruby-forum.com/t/need-to-run-multiple-location-blocks/221494/4 "2012-07-18T01:12:17Z")

</div>

So I pulled the PHP location block out, and put it in its own file  
called php.conf. Then I simply did:

include /etc/nginx/php.conf

In each location block that needs to execute PHP. This seems to have  
worked quite nicely. However, is there a better way of doing this?

Thanks.

Posted at Nginx Forum:

> **[Re: Need To Run Multiple Location Blocks](https://forum.nginx.org/read.php?2,228624,228659#msg-228659)**
>
> I have the following location blocks: location ^~ /publish { allow 127.0.0.1; #Allow home allow 99.100.101.102; deny all; } Then the following location block to process PHP after:

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [July 18, 2012, 3:45am UTC](https://www.ruby-forum.com/t/need-to-run-multiple-location-blocks/221494/5 "2012-07-18T03:45:28Z")

</div>

On Jul 18, 2012 6:12 AM, “justin” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I pulled the PHP location block out, and put it in its own file  
> called php.conf. Then I simply did:
> 
> include /etc/nginx/php.conf
> 
> In each location block that needs to execute PHP. This seems to have  
> worked quite nicely. However, is there a better way of doing this?

AFAIK, that’s the best way.
