负载均衡服务器环境下的PHP请求文件缓存
我希望在 PHP 应用程序中编写一个基本的 PHP 文件缓存驱动程序,将所有流量路由到前端控制器。例如,假设使用 apache mod_proxy_balancer 进行以下简化设置:
在单服务器环境中,我将缓存磁盘上的请求响应的目录结构与请求 URI 匹配。然后,像下面这样的简单 apache 重写规则可以允许 apache 返回静态缓存文件(如果存在)并完全避免 PHP 进程:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /front_controller.php [L]
显然,这在负载平衡环境中是有问题的,因为缓存文件只会写入磁盘在提供请求并缓存结果的特定 PHP 服务器上。
解决问题...
所以,为了解决这个问题,我想我可以删除一些代码,让各个后端 PHP 服务器向负载均衡器写入/删除缓存数据。然而,由于对 mod_proxy_balancer 的功能(以及任何其他负载平衡选项,真的)一无所知,我需要对以下问题进行一些外部验证:
以及问题......
- 是否可以像上面那样进行某种形式的检查RewriteRules 让前端负载均衡器在向后端服务器之一发送请求之前提供静态文件?
- 这是否可取?是否应该允许负载均衡器专门路由流量而不用担心提供静态内容?
- 在 PHP 服务器级别的缓存文件上使用可接受的 TTL 并处理可接受的过时缓存重叠级别是否会更好?
最后,如果这个问题太宽泛或者已经得到回答,我们深表歉意;由于我前面提到的对负载平衡主题的无知,我不太确定要搜索什么。
I'm looking to write a basic PHP file caching driver in a PHP application that routes all traffic to a front controller. For example's sake, assume the following simplified setup using apache mod_proxy_balancer:
In a single-server environment I would cache request responses on disk in a directory structure matching the request URI. Then, simple apache rewrite rules like the following could allow apache to return static cache files (if they exist) and avoid the PHP process altogether:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /front_controller.php [L]
Obviously, this is problematic in a load-balanced environment because the cache file would only be written to disk on the specific PHP server where the request was served up and the results cached.
Solving the issue ...
So, to solve this problem, I figured I could knock out some code to have the individual back-end PHP servers write/delete cache data to the load balancer. However, being mostly ignorant as to the capabilities of mod_proxy_balancer (and any other load balancing options, really), I need some outside verification for the following questions:
And the questions ...
- Is it possible to do some form of checking like the above RewriteRules to have the front-facing load balancer serve up a static file before sending off requests to one of the backend servers?
- Is this even advisable? Should the load balancer be allowed to route traffic exclusively and not be bothered with serving up static content?
- Would it be better to just use an acceptable TTL on the cached files at the PHP server level and deal with an accepted level of stale cache overlap?
Finally, apologies if this is is too broad or has already been answered; I'm not really sure what to search for as a result of my aforementioned ignorance on the load-balancing subject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于最简单的解决方案,您可以使用 NFS。通过 NFS 在所有 PHP 服务器上挂载文件系统,它的作用类似于本地存储,但对于所有服务器都是相同的。要变得更复杂一点,可以使用 Nginx 或 Varnish 之类的东西来缓存 NFS 文件系统上的内容。
使用memcache也是一种可行的替代方案,它是一种基于分布式内存的存储系统。 Memcache 的好处是,如果您不想,则无需管理缓存清除或清除。您可以为每个缓存项目设置 TTL,或者如果 memcache 已满,它会自动清除缓存项目。
For the simplest solution, you can use NFS. Mount a file system via NFS on all of the PHP servers and it acts like local storage, but is the same for all servers. To get a little more sophisticated, use something like Nginx or Varnish that can cache what is on the NFS file system.
Using memcache is also a viable alternative, which is a distributed memory based storage system. The nice thing about memcache is that you don't need to manage cache clearing or purging if you don't want to. You can set TTL for each cached item, or if memcache gets full, it automatically purges cached items.
这听起来像是 Nginx 可以轻松完成的事情,并且无需写入磁盘上的文件。
Nginx 可以进行负载平衡和缓存,这里有一个教程:
http://nathanvangheem.com/news/nginx-with-built-in-load-balancing-and-caching
This sounds like something Nginx could do easily, and would remove the need to write to files on disk.
Nginx can do the load balancing and caching, here's a tutorial on it:
http://nathanvangheem.com/news/nginx-with-built-in-load-balancing-and-caching