# Php fastcgi main/common config

**URL:** <https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433>\
**Category:** NGINX\
**Created:** [October 14, 2010, 11:25am UTC](https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433 "2010-10-14T11:25:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 14, 2010, 11:25am UTC](https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433/1 "2010-10-14T11:25:38Z")

</div>

Hello!

Is there any main/common configuration for fastcgi php under nginx?

I mean this days I use the following configuration (thanks again Igor  
for the alias idea):

server {  
listen 80;  
location / {  
root /www/home/;  
index index.php index.html index.htm;  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:11000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME /www/home$fastcgi\_script\_name;  
include fastcgi\_params;  
}  
}  
location /site1/ {  
alias /www1/site1/;  
index index.php index.html index.htm;  
location ~ ^/site1/(.+.php)$ {  
alias /www1/site1/$1;  
fastcgi\_pass 127.0.0.1:11000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $request\_filename;  
include fastcgi\_params;  
}  
}  
location /site2/ {  
alias /www2/site2/;  
index index.php index.html index.htm;  
location ~ ^/site2/(.+.php)$ {  
alias /www2/site2/$1;  
fastcgi\_pass 127.0.0.1:11000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $request\_filename;  
include fastcgi\_params;  
}  
}  
}

I would like to reduce my config to something like this:

server {  
listen 80;  
index index.php index.html index.htm;  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:11000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
$MACRO\_TO\_REAL\_PATH$fastcgi\_script\_name;  
include fastcgi\_params;  
}  
location / {  
root /www/home/;  
}  
location /site1/ {  
alias /www1/site1/;  
}  
location /site2/ {  
alias /www2/site2/;  
}  
}

Obviously $MACRO\_TO\_REAL\_PATH is not exists yet. The question is the  
second configuration is possible with nginx 0.8.52 or not? Or might is  
there any simple solution instead of lot of fastcgi stuff?

Bye,  
a

---

<div class="post-metadata">

**Author:** ![Artifex\_Maximus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Artifex\_Maximus](https://www.ruby-forum.com/u/Artifex_Maximus)\
**Post date:** [October 14, 2010, 11:46am UTC](https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433/2 "2010-10-14T11:46:22Z")

</div>

> Obviously $MACRO\_TO\_REAL\_PATH is not exists yet. The question is the  
> second configuration is possible with nginx 0.8.52 or not? Or might is  
> there any simple solution instead of lot of fastcgi stuff?

You can always use (in the fastcgi\_params file):

fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;

At least it works for me so the .php location handler is just simple:

```
    location ~ \.php$ {
            fastcgi_pass 127.0.0.1:1026;
            fastcgi_index index.php;
            include fastcgi_params;
    }

```

Of course it needs to be within each server {} block as there are no  
global  
location within http {} scope.

rr

---

<div class="post-metadata">

**Author:** ![Artifex\_Maximus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Artifex\_Maximus](https://www.ruby-forum.com/u/Artifex_Maximus)\
**Post date:** [October 14, 2010, 1:38pm UTC](https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433/3 "2010-10-14T13:38:45Z")

</div>

Hello!

On Thu, Oct 14, 2010 at 11:45 AM, Reinis R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> location ~ .php$ {  
> fastcgi\_pass 127.0.0.1:1026;  
> fastcgi\_index index.php;  
> include fastcgi\_params;  
> }

Have tried but gives http error 404 to all php script. I had changed  
fastcgi\_params, rearrange conf as you outlined. In brief:

server {  
location ~ .php$ { fastcgi stuffs }  
location / { root /www1/site/; }  
location /site/ { alias /www2/page/; }  
}

Bye,  
a

---

<div class="post-metadata">

**Author:** ![Artifex\_Maximus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Artifex\_Maximus](https://www.ruby-forum.com/u/Artifex_Maximus)\
**Post date:** [October 14, 2010, 12:05pm UTC](https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433/4 "2010-10-14T12:05:52Z")

</div>

Hello!

On Thu, Oct 14, 2010 at 11:45 AM, Reinis R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> location ~ .php$ {  
> fastcgi\_pass 127.0.0.1:1026;  
> fastcgi\_index index.php;  
> include fastcgi\_params;  
> }

Thank you very much! Sounds good and I will try it. Is it works with  
nested location as well? Look for Igor’s post:  
[http://forum.nginx.org/read.php?2,3059,4571](http://forum.nginx.org/read.php?2,3059,4571)

> Of course it needs to be within each server {} block as there are no global  
> location within http {} scope.

Yes I mean common within server block even if global would be better.  
But within server block is much easier than within each location.

Bye,  
a

---

<div class="post-metadata">

**Author:** ![Artifex\_Maximus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Artifex\_Maximus](https://www.ruby-forum.com/u/Artifex_Maximus)\
**Post date:** [October 14, 2010, 1:57pm UTC](https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433/5 "2010-10-14T13:57:41Z")

</div>

> Have tried but gives http error 404 to all php script. I had changed  
> fastcgi\_params, rearrange conf as you outlined. In brief:

Can you compare (change) your fastcgi\_params file to this and see if it  
works then (be sure to restart nginx too)?:

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\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_param SCRIPT\_NAME $fastcgi\_script\_name;  
fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_param DOCUMENT\_URI $document\_uri;  
fastcgi\_param DOCUMENT\_ROOT $document\_root;  
fastcgi\_param SERVER\_PROTOCOL $server\_protocol;  
fastcgi\_param REMOTE\_ADDR $remote\_addr;  
fastcgi\_param REMOTE\_PORT $remote\_port;  
fastcgi\_param SERVER\_ADDR $server\_addr;  
fastcgi\_param SERVER\_PORT $server\_port;  
fastcgi\_param SERVER\_NAME $host;

rr

---

<div class="post-metadata">

**Author:** ![Artifex\_Maximus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Artifex\_Maximus](https://www.ruby-forum.com/u/Artifex_Maximus)\
**Post date:** [October 18, 2010, 11:22am UTC](https://www.ruby-forum.com/t/php-fastcgi-main-common-config/197433/6 "2010-10-18T11:22:39Z")

</div>

Hello!

On Thu, Oct 14, 2010 at 1:56 PM, Reinis R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
> fastcgi\_param SCRIPT\_NAME $fastcgi\_script\_name;  
> fastcgi\_param REQUEST\_URI $request\_uri;  
> fastcgi\_param DOCUMENT\_URI $document\_uri;  
> fastcgi\_param DOCUMENT\_ROOT $document\_root;  
> fastcgi\_param SERVER\_PROTOCOL $server\_protocol;  
> fastcgi\_param REMOTE\_ADDR $remote\_addr;  
> fastcgi\_param REMOTE\_PORT $remote\_port;  
> fastcgi\_param SERVER\_ADDR $server\_addr;  
> fastcgi\_param SERVER\_PORT $server\_port;  
> fastcgi\_param SERVER\_NAME $host;

I have copied this lines to my fastcgi\_params and changed the  
nginx.conf to the suggested one. My file had this line

fastcgi\_param SERVER\_NAME $server\_name;

instead of yours.

I recompile nginx with debug feature to look deeper into  
communication. On php request nginx found the required php related  
config lines:

‘2010/10/18 11:07:51 [debug] 45041#0: \*1 using configuration “.php$”’

But the document root variable is bad for some reason:

‘2010/10/18 11:07:51 [debug] 45041#0: \*1 http script copy:  
“SCRIPT\_FILENAME”  
2010/10/18 11:07:51 [debug] 45041#0: \*1 http script var:  
“/usr/local/etc/nginx/html”  
2010/10/18 11:07:51 [debug] 45041#0: \*1 http script var:  
“/www/php/getplugins.php”  
2010/10/18 11:07:51 [debug] 45041#0: \*1 fastcgi param:  
“SCRIPT\_FILENAME: /usr/local/etc/nginx/html/www/php/getplugins.php”  
2010/10/18 11:07:51 [debug] 45041#0: \*1 http script copy: “SCRIPT\_NAME”  
2010/10/18 11:07:51 [debug] 45041#0: \*1 http script var:  
“/www/php/getplugins.php”’

It uses /usr/local/etc/nginx/html as document root instead of my  
/www/site1. How could I define the document root for php scripts?

Bye,  
a
