AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1051989
Accepted
Adam Birds
Adam Birds
Asked: 2021-02-02 11:50:52 +0800 CST2021-02-02 11:50:52 +0800 CST 2021-02-02 11:50:52 +0800 CST

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

  • 772

我运行 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; }
}

不知道我哪里出错了,或者是否有可能。谢谢

nginx wordpress php-fpm
  • 1 1 个回答
  • 165 Views

1 个回答

  • Voted
  1. Best Answer
    Tero Kilkanen
    2021-02-02T14:15:01+08:002021-02-02T14:15:01+08:00

    这很可能发生,因为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其分配给变量。

    • 3

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve