Avice for my vhost configuration

Hello,

I plan to configure my nginx server with a couple of vhosts.
For each of them I want:

  • to use php
  • deny access begining by a dot
  • not logging access to favicon

So my configuration would look like that
server {

location ~ .php$ {
root /var/www/htdocs/sites/expertinet;
fastcgi_pass unix:/tmp/php.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /. {
access_log off;
log_not_found off;
deny all;
}

location = /favicon.ico {
return 204;
access_log off;
log_not_found off;
expires 30d;
}
}

This in each of my virtual host configuration. This is very redundant.
For example if I want to use tcp socket for fastcgi_pass, I need to edit
every single vhost configuration.

What are you advices to avoid this ? What is the recommended practice ?
Someone adviced my to use include… Could you show me an example ?

Thank you

Yup, include is the way I would do that personally.

Documentation: Core functionality
The funny thing is you already are using the ‘include’ directive: look
at
your ‘include fastcgi_params;’ line. There must be a ‘fastcgi_params’
file
in your configuration directory…
That probably comes from the part you copied/pasted from the sample doc.

The way to go would be to put the redundant configuration part in it
then
call it wherever necessary in the vhosts conf.
The docs tell you that include can be used in any context you wish, you
just need to decide on the granularity.

Hope I helped,

B. R.

— Original message —
From: “Mik J” [email protected]
Date: 6 August 2013, 00:44:37

fastcgi_pass 127.0.0.1:9000;

}

What are you advices to avoid this ? What is the recommended practice ?

Someone adviced my to use include… Could you show me an example ?

  You must read docs. 

http://nginx.org/en/docs/ngx_core_module.html#include

For you:

location ~ .php$ {
root /var/www/htdocs/sites/expertinet; ← you should avoid this,
read Pitfalls and Common Mistakes | NGINX

include my_fastcgi_params;

include
fastcgi_params;
}

in my_fastcgi_params:

fastcgi_pass unix:/tmp/php.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


Cheers,

Hello, Thank you both for your answer.

I did read the page
Core functionality but I sometimes
get confused how to put things in order exactly.

I removed the root stanza in the location block.

As for fastcgi_params I already have the line
fastcgi_param SCRIPT_NAME $fastcgi_script_name;

which looks like the one you wrote
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
in your my_fastcgi_params

What is the difference between those two ? I didn’t see SCRIPT_NAME in
the HttpFastcgiModule documentation whereas SCRIPT_FILENAME is defined
by “Parameter SCRIPT_FILENAME is used by PHP for determining the name of
script to execute”

Also, where should I put my include files ? It seems that /etc/nginx is
the default location. Or you put them in another directory ?

Cheers