跳过网站的某些区域的nginx PHPcaching?

我刚刚build立了一个新的服务器与Nginx(我是新来的)和PHP。 在我的网站上基本上有3种不同types的文件:

  • 像CSS,JS和一些图像的静态内容(大多数图像在外部CDN)
  • 主要的PHP / MySQL数据库驱动的网站,基本上就像一个静态的网站
  • dynamicPHP / MySQL论坛

这是我从这个问题和这个页面的理解,静态文件不需要特别的处理,将尽可能快的服务。

我遵循上述问题的答案设置cachingPHP文件,现在我有这样的configuration:

location ~ \.php$ { try_files $uri =404; fastcgi_cache one; fastcgi_cache_key $scheme$host$request_uri; fastcgi_cache_valid 200 302 304 30m; fastcgi_cache_valid 301 1h; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /srv/www/example$fastcgi_script_name; fastcgi_param HTTPS off; } 

然而,现在我想要防止在论坛上caching(无论是对于每个人还是对于login用户 – 没有检查后者在论坛软件中是否可行)。 我听说位置块内的“如果是邪恶的”,所以我不确定如何进行。 如果在位置块内,我可能会在中间添加这个:

 if ($request_uri ~* "^/forum/") { fastcgi_cache_bypass 1; } # or possible this, if I'm able to cache pages for anonymous visitors if ($request_uri ~* "^/forum/" && $http_cookie ~* "loggedincookie") { fastcgi_cache_bypass 1; } 

这工作是否正常,还是有更好的方法来实现这一目标?

你可以使用,如果在服务器块它是完全安全的。 与相关行的工作示例:

 server { listen 80; ...skip... if ($uri ~* "/forum" ) {set $no_cache 1;} ...skip... location ~ \.php$ { fastcgi_cache_bypass $no_cache; fastcgi_no_cache $no_cache; ...other fastcgi-php content... } 

另一种解决方法是在默认的位置块之上单独放置一个位置块,以便捕获和发送针对论坛的事物的不同行为:

 location ~ ^/forum/.*\.php$ { // forum php setup here } location ~ \.php$ { // regular php setup here } 

它不完全是干的,但是当你在6个月内回来的时候,它很简单易读。