我运行 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
语句应该用map
feature 来实现。在您的情况下,首先定义
map
inhttp
级别:然后,在 PHP
location
块中使用:该映射将
$backend
根据查询参数值为变量分配一个nginx_helper_urls
值。默认情况下,examplebackend
使用。如果参数值为all
,则将cachedeletebackend
其分配给变量。