我们应该为每个虚拟主机还是全局配置添加php-fpm?

时间:2015-08-12 03:59:04

标签: nginx

这是我的虚拟主机配置。我不确定,但这是定义事物的理想方式吗?

对于每个域/子域,我都添加了一个位置〜/ .php指令。我应该在全局nginx.conf中添加它而不是为我添加的每个域单独指定吗?

示例配置: -

server {
  listen 80;
  server_name example.com;

location / {
   root /var/www/example.com;
   index index.php;

}


location ~ \.php$ {
   root /var/www/example.com/;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
  #fastcgi_read_timeout 30000;
  #fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
}

server {
  listen 80;
  server_name www.example.net;

location / {
  root /var/www/example.net/;
  index index.php;

}


location ~ \.php$ {
  root /var/www/example.net/;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  # fastcgi_read_timeout 30000;
  # fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
}

1 个答案:

答案 0 :(得分:0)

您无法将location置于server块之外。您可以做的最好的事情是将重复的部分放入文件和include该文件。

此外,通常你不要在每个root内声明location指令,在server内声明一次。

并且,BTW,index指令可能在server块之外,在这种情况下,它会影响没有自己的server指令的所有index块。< / p>

所以这里的配置与你的保存完全相同,但重复次数较少。

index index.php;

server {
  listen 80;
  server_name example.com;
  root /var/www/example.com;

  include location-php;
}

server {
  listen 80;
  server_name example.net;
  root /var/www/example.net;

  include location-php;
}

location-php文件:

location ~ \.php$ {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
}