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 / 问题 / 1034754
Accepted
Rino Bino
Rino Bino
Asked: 2020-09-22 07:42:48 +0800 CST2020-09-22 07:42:48 +0800 CST 2020-09-22 07:42:48 +0800 CST

替代在 nginx 位置块中使用 if 语句

  • 772

Nginx 配置目标:

  • 检查某个 cookie 是否存在(不管值是什么)
  • 如果它不存在,请包含特定的配置导入。
  • 我只想将我的规则应用于某些位置,我不能将这些条件应用于全局 nginx 配置。这是特定的位置。

目前,配置看起来像这样,并且运行良好:

  location ~ ^/somedestination\.php(/|$) {
    <... truncated other config lines for readability ...>

    include includes/xx-cache-config.conf;

    <... truncated other config lines for readability ...>

    fastcgi_pass php-fpm:9000;
  }

该服务器有许多位置块,每个块都有import自己的缓存配置。这是一个只有 cgi 的应用程序,这里没有直接的 try_files。

因此,为了实现上述目标,我正在尝试类似的东西,检查$cookie_user变量是否存在,并且仅在不存在 Cooke 时应用我的配置:

  location ~ ^/somedestination\.php(/|$) {

    # only include cache config if the cookie is null/empty
    if ($cookie_user = false) {
      include includes/xx-cache-config.conf;
    }

    fastcgi_pass php-fpm:9000;
  }

似乎合乎逻辑,但问题出在: 我偶然发现了这个:https ://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/


问题:如果我们不应该if在 Location 上下文中使用(根据 nginx 官方文档),我应该怎么做?


编辑:这是条件子句中包含的内容。目前nginx 总是包含这个配置,我现在的目标是有时不包含它(当 cookie 存在时)。

注意:区域配置本身包含在其他地方。这个包含的配置启用它。

includes/xx-cache-config.conf

fastcgi_cache CACHE_TLRS;

fastcgi_cache_bypass $no_resource_cache;

fastcgi_no_cache $no_resource_cache;

fastcgi_cache_valid 200 301 48h;
fastcgi_cache_valid 302     4h;

fastcgi_cache_revalidate on;
fastcgi_cache_use_stale http_500 http_503 timeout updating;

fastcgi_cache_lock on;
fastcgi_cache_lock_age 15s;
fastcgi_cache_lock_timeout 5s;

fastcgi_ignore_headers cache-control;

fastcgi_cache_key "$scheme$host$uri";
add_header X-Cache $upstream_cache_status;
cache nginx
  • 1 1 个回答
  • 579 Views

1 个回答

  • Voted
  1. Best Answer
    Rino Bino
    2020-09-22T11:09:41+08:002020-09-22T11:09:41+08:00

    这个我想多了。

    在做了一些阅读之后,似乎导入是在服务器启动时评估的,所以你不能把它们放在一个条件中。

    所以,我最终使用了fastcgi_cache_bypass指令,我完全没有使用if就实现了目标。

    修改现有线路 fastcgi_cache_bypass $no_resource_cache;

    将其更改为:fastcgi_cache_bypass $cookie_user $no_resource_cache;

    如果变量不为空且不是“0”,这将绕过缓存

    文档链接: http: //nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_bypass

    • 1

相关问题

  • 缩放大文件下载?

  • Gzip 与反向代理缓存

  • 如何禁用tomcat缓存?我遇到了奇怪的静态文件问题

  • mod_mem_cache 提供错误的内容!

  • NFS 缓存导致间歇性滞后

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