我刚刚用 nginx(我是新手)和 PHP 设置了一个新服务器。在我的网站上,基本上有 3 种不同类型的文件:
我从这个问题和这个页面了解到,静态文件不需要特殊处理,会尽快提供。
我按照上述问题的答案为 PHP 文件设置缓存,现在我有一个这样的配置:
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;
}
Run Code Online (Sandbox Code Playgroud)
但是,现在我想防止在论坛上缓存(无论是针对所有人还是仅针对登录用户 - 还没有检查后者是否适用于论坛软件)。我听说位置块内有“如果是邪恶的”,所以我不确定如何继续。使用位置块内的 if 我可能会在中间添加它:
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;
}
Run Code Online (Sandbox Code Playgroud)
这会正常工作,还是有更好的方法来实现这一目标?
您可以使用 if 在服务器块中它是完全安全的。相关行的工作示例:
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...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4626 次 |
| 最近记录: |