Php fastcgi main/common config

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

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

Hello!

On Thu, Oct 14, 2010 at 11:45 AM, Reinis R. [email protected] 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

Hello!

On Thu, Oct 14, 2010 at 11:45 AM, Reinis R. [email protected] 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:

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

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

Hello!

On Thu, Oct 14, 2010 at 1:56 PM, Reinis R. [email protected] 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