# Rewrite and FastCGI

**URL:** <https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554>\
**Category:** NGINX\
**Created:** [October 9, 2012, 2:04pm UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554 "2012-10-09T14:04:07Z")\
**Posts on this page:** 8\
**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:** [October 9, 2012, 2:04pm UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/1 "2012-10-09T14:04:07Z")

</div>

Hello everyone!

I’m trying to use rewrite and fastcgis with Nginx 1.2.1 (from the  
Debian Wheezy package).

My root directory looks like this:  
/www/dir1  
/www/dir2

In my server’s file I have this:  
root /www/;

```
    # dir1
    location /dir1 {
            rewrite ^(.*) https://$host$1 permanent;
    }

    # php5-fpm
    location ~ \.(php|phtml)$ {
                    include /etc/nginx/fastcgi_params;
                    fastcgi_pass 

```

unix:/var/run/php-fpm/php-fpm.sock;  
fastcgi\_param SCRIPT\_FILENAME  
/www/$fastcgi\_script\_name;  
}  
}

The rewrite is working great for a html page (for example) but not for  
a php page.  
Same results with:  
root /www/;

```
    location /dir1 {
            rewrite ^(.*) https://$host$1 permanent;
            # php5-fpm
            location ~ \.(php|phtml)$ {
                    include /etc/nginx/fastcgi_params;
                    fastcgi_pass 

```

unix:/var/run/php-fpm/php-fpm.sock;  
fastcgi\_param SCRIPT\_FILENAME  
/www/$fastcgi\_script\_name;  
}  
}

I need to use php’s fastcgi for dir1 and dir2 but only to redirect  
dir1 in https. To be honest I try many settings but without any  
results.  
It seems that fastcgi (or upstream) have always the priority on  
location.

A solution could be to add a rewrite in the php’s location too to have  
something like this (but that seems rally ugly to me):  
root /www/;

```
    location /dir1 {
            rewrite ^(.*) https://$host$1 permanent;
            # php5-fpm
            location ~ \.(php|phtml)$ {
                    include /etc/nginx/fastcgi_params;
                    fastcgi_pass 

```

unix:/var/run/php-fpm/php-fpm.sock;  
fastcgi\_param SCRIPT\_FILENAME  
/www/$fastcgi\_script\_name;  
}  
}

```
    # php5-fpm
    location ~ \.(php|phtml)$ {
                    include /etc/nginx/fastcgi_params;
                    fastcgi_pass 

```

unix:/var/run/php-fpm/php-fpm.sock;  
fastcgi\_param SCRIPT\_FILENAME  
/www/$fastcgi\_script\_name;  
}  
}

Any help would be really appreciated!

Thanks for reading anyway.

Regards.

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [October 9, 2012, 2:53pm UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/2 "2012-10-09T14:53:48Z")

</div>

On Tue, Oct 09, 2012 at 02:03:40PM +0200, Thomas M. wrote:

Hi there,

> /www/$fastcgi\_script\_name;  
> }  
> }

> The rewrite is working great for a html page (for example) but not for  
> a php page.

One request is handled in one location.

You have

```
location /dir1 {}
location ~ \.(php|phtml)$ {}

```

Possibly what you want is

```
location ^~ /dir1 {}
location ~ \.(php|phtml)$ {}

```

or maybe

```
location /dir1 {}
location / {
  location ~ \.(php|phtml)$ {}
}

```

See [Module ngx\_http\_core\_module](http://nginx.org/r/location) for details.

(Possibly what you want is some other configuration: the important thing  
to keep in mind is: for this request, which one location do you wish to  
handle it? Then configure the locations accordingly.)

## f

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

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [October 9, 2012, 4:51pm UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/3 "2012-10-09T16:51:16Z")

</div>

Hi Francis.

FYI I read the documentation before to send my first email but I  
misunderstood the part about “^~” and also I didnt clearly realized  
that “One request is handled in one location”.

So with your clarification I was able to make something working:

# DocumentRoot

root /www/;

# dir1

location /dir1 {  
rewrite ^(.\*) https://$host$1 permanent;  
}

# dir2

location /dir2 {  
# php5-fpm  
location ~ .(php|phtml)$ {  
fastcgi\_param PHP\_VALUE  
“include\_path=/www/dir2:/www/dir2/common/include:.”;  
fastcgi\_param ENVIRONMENT\_NAME devs;  
fastcgi\_param DB\_CONF\_DIRECTORY /etc/itf/db/devs/;

```
  include /etc/nginx/sites-conf.d/php-fpm;
}

```

}  
# All others  
location / {  
# php5-fpm  
location ~ .(php|phtml)$ {  
fastcgi\_param ENVIRONMENT\_NAME devs;  
fastcgi\_param DB\_CONF\_DIRECTORY /etc/db/devs/;

```
  include /etc/nginx/sites-conf.d/php-fpm;
}
}

```

It seems to work as expected; I guess I could use “^~” too but I  
didn’t tried yet.  
At first I wanted to avoid repetition of the php-fpm’s part but I  
didn’t realized that it wasn’t doable.

Thanks for your help and again this is really appreciated!

Regards.

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

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [October 10, 2012, 11:33am UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/4 "2012-10-10T11:33:02Z")

</div>

I’ve tried various combinations of burst=2, nodelay, 1r/s or 1r/m, with  
and  
without limit\_conn, with and without keepalive, with and without  
“location  
/”, etc… and requests are never being limited, as shown by the  
access.log  
entries below:

Posted at Nginx Forum:

> **[Re: Rewrite and FastCGI.](https://forum.nginx.org/read.php?2,231534,231568#msg-231568)**
>
> Hello everyone! I'm trying to use rewrite and fastcgis with Nginx 1.2.1 (from the Debian Wheezy package). My root directory looks like this: /www/dir1 /www/dir2 In my server's file I have this: root /www/;

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [October 11, 2012, 9:48am UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/5 "2012-10-11T09:48:29Z")

</div>

Hi!

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

> 

English is not my native language and I’m really bad at it so this can  
be the explanation of my misunderstanding. 😕  
Anyway maybe the section about location could be reorganized a bit to  
explain possibilities ([= | ~ | ~\* | ^~]) in a dedicated part.

> That looks reasonable. “^~” only matters if you have (top level) regex  
> matches – which here, you don’t.
> 
> (Avoiding top level regex matches makes it very easy to know which  
> location{} will match any particular request. That’s usually considered  
> a Good Thing.)

Ok, good to know.

> In this case, you want different fastcgi\_param lines for different php  
> scripts – so repeating the common config (by using “include”) and adding  
> the specific parts, like you have done, is probably the best way.  
> Thanks for this confirmation.

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [October 11, 2012, 1:13pm UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/6 "2012-10-11T13:13:38Z")

</div>

2012/10/11 Thomas M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi!

Hello.

> English is not my native language and I’m really bad at it so this can  
> be the explanation of my misunderstanding. 😕  
> Anyway maybe the section about location could be reorganized a bit to  
> explain possibilities ([= | ~ | ~\* | ^~]) in a dedicated part.

English isn’t my native language, too, but regular expressions  
(“regex” resp. “RegExp”) is such a wide-spreaded topic, that you could  
read about the basics at wikipedia, too →

> **[Regular expression](https://en.wikipedia.org/wiki/Regular_expression)**
>
> A regular expression (shortened as regex or regexp), sometimes referred to as a rational expression, is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. Regular expression techniques are developed in theoretical computer science and formal language theory. The concept of regular expressions began in the 1950s, when the American mathematicia...

Choose the translation in your mother tongue at the left hand sidebar  
😉

Best regards, Andre

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [October 11, 2012, 1:20pm UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/7 "2012-10-11T13:20:39Z")

</div>

Hello Andre.

2012/10/11 Andre J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> (“regex” resp. “RegExp”) is such a wide-spreaded topic, that you could  
> read about the basics at wikipedia, too →   
> [Regular expression - Wikipedia](http://en.wikipedia.org/wiki/Regular_expression)  
> Choose the translation in your mother tongue at the left hand sidebar 😉
> 
> Best regards, Andre

Indeed, you are right.

Regards.

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [October 10, 2012, 2:37pm UTC](https://www.ruby-forum.com/t/rewrite-and-fastcgi/223554/8 "2012-10-10T14:37:05Z")

</div>

On Tue, Oct 09, 2012 at 04:50:32PM +0200, Thomas M. wrote:

Hi there,

great that you found a configuration that works.

> FYI I read the documentation before to send my first email but I  
> misunderstood the part about “^~” and also I didn’t clearly realized  
> that “One request is handled in one location”.

Having re-read the docs, I can see that the interpretation could be  
made clearer. Can you suggest any wording that might have helped you  
understand then, what you do now? Maybe it can become easier for the  
next person 😉

Would adding a link to

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

have helped, do you think? It is an example rather than complete  
documentation, and it leaves out the “^~” thing, so maybe it wouldn’t  
have been directly useful.

> So with your clarification I was able to make something working:

> location /dir1 {  
> rewrite ^(.\*) https://$host$1 permanent;  
> }

> location /dir2 {  
> # php5-fpm  
> location ~ .(php|phtml)$ {  
> fastcgi\_param PHP\_VALUE  
> “include\_path=/www/dir2:/www/dir2/common/include:.”;  
> fastcgi\_param ENVIRONMENT\_NAME devs;  
> fastcgi\_param DB\_CONF\_DIRECTORY /etc/itf/db/devs/;
> 
> ```
> include /etc/nginx/sites-conf.d/php-fpm;
> }
> 
> ```
> 
> }

> It seems to work as expected; I guess I could use “^~” too but I  
> didn’t tried yet.

That looks reasonable. “^~” only matters if you have (top level) regex  
matches – which here, you don’t.

(Avoiding top level regex matches makes it very easy to know which  
location{} will match any particular request. That’s usually considered  
a Good Thing.)

> At first I wanted to avoid repetition of the php-fpm’s part but I  
> didn’t realized that it wasn’t doable.

In this case, you want different fastcgi\_param lines for different php  
scripts – so repeating the common config (by using “include”) and  
adding  
the specific parts, like you have done, is probably the best way.

> Thanks for your help and again this is really appreciated!

You’re welcome.

All the best,

## f

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