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

Tob*_* P. 2 configuration fastcgi nginx nginx-location

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

我有以下要求:

  • 在 php 5.6 应用程序上/运行(fastcgi 后端127.0.0.1:9000
  • /crmphp 7.0 应用程序上运行,它必须相信它正在运行/(fastcgi 后端127.0.0.1:9001
  • 事实上还有很少的后端,但是有了这两个我可以自己制作它们

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

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

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

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

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

  2. 你必须/crm$fastcgi_script_name. 推荐的方法是使用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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)