【问题标题】:Multiple fpm socket in one nginx site by locations一个 nginx 站点中的多个 fpm 套接字(按位置)
【发布时间】:2018-07-23 01:34:40
【问题描述】:

想象一下 nginx 中的默认站点

server {
    listen 80;
    server_name example.com;
}

server {
    listen 80 default_server;
    client_max_body_size 0;
    server_name _;

    index index.php index.html index.htm;
    set $root_path '/www/public';
    root $root_path;

    try_files $uri @rewrite;

    rewrite ^/(?!api/)(.*)/$ /$1 permanent;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ \.php$ {
        fastcgi_index /index.php;

        ##### I THINK LOCATION CONFIG MUST COMES HERE #####
        fastcgi_pass unix:/var/run/php/xxxx.sock;
        ###################################################

        include fastcgi_params;
        fastcgi_split_path_info           ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO           $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED     $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME     $document_root$fastcgi_script_name;
        access_log syslog:server=loghost,facility=local7,tag=nginx,severity=info syslogformat if=$dolog;
    }
}

我们需要在重写(到 index.php)之后分离 url 并将它们传递给不同的 fpm 套接字
例如,我想将
fastcgi_pass unix:/var/run/php/1111.sock; 用于^/action-1/等网址

fastcgi_pass unix:/var/run/php/2222.sock; 其他任何事情

我该怎么做?感谢任何帮助

【问题讨论】:

    标签: php nginx nginx-location fpm


    【解决方案1】:

    设置默认套接字,并为其他套接字匹配单独的路径。

    server {
    
        ...
        set $php_socket "1";
    
        if ($uri ~* "^/action-1/") {
            set $php_socket "2";
        }
        ...
    
        location ~ \.php$ {        
            ...
            if ($php_socket = '1') { fastcgi_pass unix:/var/run/php/1.sock; }
            if ($php_socket = '2') { fastcgi_pass unix:/var/run/php/2.sock; }
            ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2016-10-09
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多