根据 URL 中的参数使用不同的 PHP 上游 - NGinx

根据 URL 中的参数使用不同的 PHP 上游 - NGinx

我运行 Nginx 和 PHP-FPM。Nginx 在 www-data 下运行,而 PHP-FPM 则以每个网站的单独用户身份运行。我使用 Nginx FASTCGI 缓存,并使用 WordPress 的 Nginx Helper 插件。遗憾的是,由于用户不同,我们无法使用插件中的“清除全部”功能,因为 php 用户无权访问缓存。

由于安全原因,更改权限不是一种选择。

如果 URL 中存在以下参数,我想要做的是使用不同的 PHP-FPM 池。

nginx_helper_urls=all

我当前的 NGinx 配置如下:

    upstream examplebackend {
        server unix:/var/run/php-fcgi-example.sock;
}

upstream cachedeletebackend {
        server unix:/var/run/php-fcgi-cachedelete.sock;
}

server {
        listen 80 default;
        server_name www.example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 80;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 443 ssl;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}

server {
    listen 443 ssl;
    ssl_protocols TLSv1.2 TLSv1.3;
    server_name www.example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    access_log   /home/examplecom/logs/example.com.access.log;
    error_log    /home/examplecom/logs/example.com.error.log;

    root /home/examplecom/public_html;
    index index.php;

    set $skip_cache 0;

    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 1;
    }   
    if ($query_string != "") {
        set $skip_cache 1;
    }   

    # Don't cache uris containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }   

    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }    

    location ~ \.php$ {
        try_files $uri =404; 
        include fastcgi_params;
    if ( $args ~ nginx_helper_urls=all ) { fastcgi_pass cachedeletebackend; }
        fastcgi_pass examplebackend;

        fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;

        fastcgi_cache WORDPRESS;
        fastcgi_cache_valid  60m;
    }

    location ~ /purge(/.*) {
        fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    }   

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /\. { deny  all; access_log off; log_not_found off; }
}

不确定我哪里做错了,或者这是否有可能。谢谢

答案1

这可能是因为如果是邪恶的

在nginx中,大部分if语句都应该用feature来实现map

对于您的情况,首先定义map级别http

map $arg_nginx_helper_urls $backend {
    all cachedeletebackend;
    default examplebackend;
}

然后,在 PHPlocation块中使用:

location ~ \.php$ {
    try_files $uri =404; 
    include fastcgi_params;
    fastcgi_pass $backend;

    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;

    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid  60m;
}

$backend该映射将根据查询参数值为变量分配一个值nginx_helper_urls。默认情况下,examplebackend使用。如果参数值为all,则将cachedeletebackend分配给变量。

相关内容