Nginx

根據 URL 中的參數使用不同的 PHP 上游 - NGinx

  • February 1, 2021

我執行 Nginx 和 PHP-FPM。Nginx 在 www-data 下執行,PHP-FPM 作為每個網站的單獨使用者執行。我使用 Nginx FASTCGI 記憶體並使用 WordPress 的 Nginx Helper 外掛。不幸的是,由於使用者不同,我們不能在外掛中使用“Purge All”功能,因為 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; }
}

不知道我哪裡出錯了,或者是否有可能。謝謝

這很可能發生,因為If 是 evil

在 nginx 中,大部分if語句應該用mapfeature 來實現。

在您的情況下,首先定義mapinhttp級別:

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其分配給變數。

引用自:https://serverfault.com/questions/1051989