nginx 服务器为不同的子文件夹使用多个 fastcgi 后端

nginx server using multiple fastcgi backends for different subfolders

在 Whosebug 上有多个问题如何使用具有不同 fastcgi 后端的子文件夹或类似的问题但没有任何工作正常 - 经过数小时的尝试和阅读文档(可能遗漏了一个小细节)我放弃了。

我有以下要求:

我尝试先为位置前缀定义单独的 php 上下文,然后再尝试删除 /crm 前缀。但似乎我做错了什么,因为 /crm 每次都使用 /.

的 php 上下文

我的实际精简配置,删除了所有不相关的内容和所有失败的测试:

server {
    listen       80;
    server_name  myapp.localdev;

    location /crm {
        root       /var/www/crm/public;
        index      index.php;
        try_files  $uri /index.php$is_args$args;

        location ~ \.php$ {
            # todo: strip /crm from REQUEST_URI
            fastcgi_pass   127.0.0.1:9001; # 9001 = PHP 7.0
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    location / {
        root       /var/www/intranet;
        index      index.php;
        try_files  $uri /index.php$is_args$args;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000; # 9000 = PHP 5.6
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

您的配置中有两个小错误:

  1. try_files的最后一个参数是内部重定向,当可以找到之前的none个文件时。这意味着您要将 CRM 位置设置为 try_files $uri /crm/index.php$is_args$args;

  2. 您必须从 $fastcgi_script_name 中删除 /crm。推荐的做法是使用 fastcgi_split_path_info ^(?:\/crm\/)(.+\.php)(.*)$;

可能有效的配置如下所示:

server {
    listen       80;
    server_name  myapp.localdev;

    location /crm {
        root       /var/www/crm/public;
        index      index.php;
        try_files  $uri /crm/index.php$is_args$args;

        location ~ \.php$ {
            # todo: strip /crm from REQUEST_URI
            fastcgi_pass   127.0.0.1:9001; # 9001 = PHP 7.0

            fastcgi_split_path_info ^(?:\/crm\/)(.+\.php)(.*)$;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

            include        fastcgi_params;
        }
    }

    location / {
        root       /var/www/intranet;
        index      index.php;
        try_files  $uri /index.php$is_args$args;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000; # 9000 = PHP 5.6
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

运行 这在 Ubuntu 14.04 和 Nginx 1.10 上。

您可以尝试指定socket

PHP7

fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

注:PHP7套接字的路径是"same"为PHP5。不是 /var/run/php7-fpm.sock。我偶然发现了一些文章,指出这是默认路径。请检查它是如何安装在您的服务器上的。

PHP5

fastcgi_pass unix:/var/run/php5-fpm.sock;

此外,运行 PHP7,您可能会遇到 Permission Denied 错误。此问题可能是由于 /etc/php/7.0/fpm/pool.d/www.conf 中的用户问题所致。在 PHP7 配置中 user/group 是 www-data 而 Nginx 用户是 nginx.

这是 PHP7 配置:

listen.owner = www-data
listen.group = www-data

在我的例子中,我将 Nginx 用户更改为 www-data

希望对您有所帮助。