简体   繁体   English

如何通过 Nginx 为不同的 PHP 使用不同的 PHP-FPM 池?

[英]How can I use different PHP-FPM pools for different PHP's with Nginx?

The directory structure looks like this:目录结构如下所示:

/home/test/index.php
/home/test/stats.php
/home/test/contact.php

When I call the URL: testsite.com/stats.php当我调用 URL 时: testsite.com/stats.php
I want to use the pool [back] but all other PHP's (index.php, contact.php) should use the pool [front].我想使用池 [back] 但所有其他 PHP(index.php,contact.php)应该使用池 [front]。

/etc/php/7.3/fpm/pool.d/back.conf /etc/php/7.3/fpm/pool.d/back.conf

[back]
listen = 127.0.0.1:9001
listen.backlog = 4096
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data
listen.allowed_clients = 127.0.0.1
listen.mode = 0660
; --------- ondemand ---------
pm = ondemand
pm.max_children = 50
pm.max_requests = 500
pm.process_idle_timeout = 10s

pm.status_path = /backstatus

/etc/php/7.3/fpm/pool.d/front.conf /etc/php/7.3/fpm/pool.d/front.conf

[front]
listen = 127.0.0.1:9000
listen.backlog = 4096
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data
listen.allowed_clients = 127.0.0.1
listen.mode = 0660
; --------- static ---------
pm = static
pm.max_children = 5

pm.status_path = /frontstatus

/etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name testsite.com; 

    access_log off;     
    log_not_found off;      
    error_log /var/log/nginx/error.log;

    root /home/test;

    index index.php index.html;     

    location ~ ^/back(status|ping)$ {
            access_log off;             
            allow 127.0.0.1;
            allow ::1;
            allow xx.xxx.xxx.xx; # own IP
            deny all;

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9001;
    }

    location ~ ^/front(status|ping)$ {
            access_log off;
            allow 127.0.0.1;
            allow ::1;
            allow xx.xxx.xxx.xx; # own IP
            deny all;

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
    }   

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Deny all attempts to access hidden files such as 
    # .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
    }    
}

When I open the URL: testsite.com/frontstatus?html当我打开 URL 时:testsite.com/frontstatus? html
I see that I am using the pool [front] because the process manager is static.我看到我正在使用池 [front] 因为进程管理器是 static。

When I open the URL: testsite.com/backstatus?html当我打开 URL 时:testsite.com/backstatus? html
I see that I am using the pool [back] because the process manager is ondemand.我看到我正在使用池 [返回],因为进程管理器是按需的。

What should the location block look like, that the pool [back] is taken at stats.php and the pool [front] at all others??位置块应该是什么样子,池 [back] 在 stats.php 和池 [front] 在所有其他位置?

testsite.com/frontstatus?html 
testsite.com/frontping

use this location:使用这个位置:

location ~ ^/front(status|ping)$ {
   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
   fastcgi_pass 127.0.0.1:9000;
}

that's correct and这是正确的

testsite.com/backstatus?html
testsite.com/backping

use this location:使用这个位置:

location ~ ^/back(status|ping)$ {
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_pass 127.0.0.1:9001;
}

that is correkt too.这也是正确的。

But

testsite.com/index.php 
testsite.com/contact.php 
testsite.com/stats.php

use all the same location:使用所有相同的位置:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
}

thats wrong... only the那是错误的......只有

testsite.com/stats.php

should use the the location with the fastcgi_pass 127.0.0.1:9001;应该使用 fastcgi_pass 127.0.0.1:9001 的位置;

all other PHP's should use the location with the fastcgi_pass 127.0.0.1:9000;所有其他 PHP 应该使用 fastcgi_pass 127.0.0.1:9000 的位置;

How must i write this locations?我该如何写这个位置?

location = /stats.php {
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       fastcgi_pass 127.0.0.1:9001;
}

it does not work这没用

I tried to put this block我试图把这个块

location = /stats.php {
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       fastcgi_pass 127.0.0.1:9001;
}

before this:在这之前:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
}

and also before this:在此之前:

location ~ ^/back(status|ping)$ {
        access_log off;             
        allow 127.0.0.1;
        allow ::1;
        allow xx.xxx.xxx.xx; # own IP
        deny all;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9001;
}

but nothing works:(但没有任何效果:(

Once I add this location:一旦我添加了这个位置:

location = /stats.php {
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       fastcgi_pass 127.0.0.1:9001;
}

All PHP's use this location with the fastcgi_pass 127.0.0.1:9001, which should not be so.所有 PHP 都使用这个位置和 fastcgi_pass 127.0.0.1:9001,不应该这样。

What can I do so that only the stats.php uses the fastcgi_pass 127.0.0.1:9001 instead of the fastcgi_pass 127.0.0.1:9000??我该怎么做才能只有 stats.php 使用 fastcgi_pass 127.0.0.1:9001 而不是 fastcgi_pass 127.0.0.1:9000?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM