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 / 问题 / 749321
Accepted
Tim
Tim
Asked: 2016-01-16 00:39:57 +0800 CST2016-01-16 00:39:57 +0800 CST 2016-01-16 00:39:57 +0800 CST

Nginx:替代 if inside location 块,用于基于变量缓存标头

  • 772

我正在尝试使用 Nginx 页面缓存而不是 Wordpress 缓存。缓存似乎工作正常,但我无法根据变量设置条件缓存标头 - 用户是否登录到 wordpress。如果用户已登录,我希望应用无缓存标头,否则 Wordpress 和 CDN 都可以将页面缓存一天。我发现我只能在 if 语句中添加一个标题。

我已经阅读(但没有完全理解,因为这里已经很晚了)[如果是邪恶的][1]。我还找到了关于堆栈交换的答案(在我的笔记本电脑上,现在找不到),在 if 块中只有一个 add_header 有效。

谁能给我一个可能更好的替代方案的想法?我知道我可以将过期与缓存控制结合起来,但我想要更多的标题,而且我想理解和学习。

这是一个显着简化的配置,其中包含相关部分。

server {
  server_name example.com;

  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/|/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/ /blog/index.php?args;
  }

  location ~ \.(hh|php)$ {
    fastcgi_keep_conn on;
    fastcgi_intercept_errors on;
    fastcgi_pass  php;
    include  fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    # Cache Stuff
    fastcgi_cache CACHE_NAME;
    fastcgi_cache_valid 200 1440m;
    add_header X-Cache $upstream_cache_status;

    fastcgi_cache_methods GET HEAD;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;

    add_header Z_ABCD "Test header";

    if ($skip_cache = 1) {
      add_header Cache-Control "private, no-cache, no-store";
      add_header CACHE_STATUS "CACHE NOT USED";
    }
    if ($skip_cache = 0) {
      add_header Cache-Control "public, s-maxage = 240";
      expires 1d;
      add_header CACHE_STATUS "USED CACHE";
    }

    add_header ANOTHER_HEADER "message";
    }
}
nginx
  • 2 2 个回答
  • 3712 Views

2 个回答

  • Voted
  1. Richard Smith
    2016-01-16T02:34:21+08:002016-01-16T02:34:21+08:00

    指令的替代方案if是map指令。假设CACHE_STATUSvsCACHE_STATIC只是您问题中的一个错字,您可以试试这个:

    map $http_cookie $expires {
        default 1d;
        ~*wordpress_logged_in off;
    }
    map $http_cookie $control {
        default "public, s-maxage = 240";
        ~*wordpress_logged_in "private, no-cache, no-store";
    }
    map $http_cookie $status {
        default "USED CACHE";
        ~*wordpress_logged_in "CACHE NOT USED";
    }
    server {
        ...
        location ~ \.(hh|php)$ {
            ...
            expires $expires;
            add_header Cache-Control $control;
            add_header CACHE_STATUS $status;
        }
    }
    

    map指令应放置在容器内http(与块处于同一级别server),如上所示。

    该map指令在此处记录。

    • 1
  2. Best Answer
    Tim
    2016-01-16T13:23:48+08:002016-01-16T13:23:48+08:00

    根据@Richard Smith 提供的答案,我自己提出了一种解决方案,但它并没有完全满足我的需求。我使用了更多的缓存控制标头,并删除了不必要的 expires 指令。

    这位于服务器块内

    if ($skip_cache = 1) {
      set $cacheControl "private, max-age=0, s-maxage=0, no-cache, no-store";
    }
    if ($skip_cache = 0) {
      set $cacheControl "public, max-age=86400, s-maxage=86400";
    }
    

    然后进入每个适用的位置块

    add_header Cache-Control $cacheControl;
    

    这意味着在 location 块内不需要“if”。我认为这可以解决问题,但是如果有人有其他想法,我仍然很感兴趣。

    • 0

相关问题

  • 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