简体   繁体   English

用 RewriteLock 理解 Apache RewriteMap

[英]Understanding Apache RewriteMap with RewriteLock

I've taken over development of a fairly heavy-duty LAMP application.我已经接管了一个相当繁重的 LAMP 应用程序的开发。 The original dev used an .htaccess file with RewriteMap and a PHP script to handle certain conditions of the app.原始开发人员使用带有 RewriteMap 的 .htaccess 文件和 PHP 脚本来处理应用程序的某些条件。

Specifically, when certain subdomain patterns are requested by the client, the RewriteMap catches them and sends them to the appropriate application module.具体来说,当客户端请求某些子域模式时,RewriteMap 会捕获它们并将它们发送到适当的应用程序模块。

I'm quite comfortable with typical mod_rewrite redirects, and I think I've got the basic RewriteMap concept figured out;我对典型的 mod_rewrite 重定向很满意,而且我想我已经弄清楚了基本的 RewriteMap 概念; but I'm struggling to find decent documentation on how RewriteLock works.但我很难找到关于 RewriteLock 如何工作的体面文档。 According to the Apache docs:根据 Apache 文档:

This directive sets the filename for a synchronization lockfile which mod_rewrite needs to communicate with RewriteMap programs.该指令设置 mod_rewrite 需要与 RewriteMap 程序通信的同步锁文件的文件名。 Set this lockfile to a local path (not on a NFS-mounted device) when you want to use a rewriting map-program.当您想使用重写映射程序时,将此锁定文件设置为本地路径(而不是安装在 NFS 的设备上)。 It is not required for other types of rewriting maps.其他类型的重写映射不需要它。

But this is still a little vague for me.但这对我来说仍然有点模糊。 Whats the exact purpose and function of RewriteLock and how does it work? RewriteLock 的确切用途和 function 是什么,它是如何工作的?

RewriteLock is used with the prg: keyword. RewriteLock 与prg:关键字一起使用。 RewriteMap can be used with several keywords, to use text files ( txt: ), hashfiles ( dbm: ), randomized text ( rnd: ) or external mapping scripts ( this one is the prg: keyword ). RewriteMap可以与多个关键字一起使用,以使用文本文件 ( txt: :)、哈希文件 ( dbm: :)、随机文本 ( rnd: :) 或外部映射脚本(这个是prg:关键字)。 In this mode the external script is launched when apache start.在这种模式下,外部脚本在 apache 启动时启动。 Then for every incoming request, when mod-rewrite is calling the prg: mapping, apache sends input to that script and reads the output stream to get the value.然后对于每个传入的请求,当 mod-rewrite 调用prg:映射时,apache 将输入发送到该脚本并读取 output stream 以获取值。

RewriteLock must be used in that case to prevent parallel requests (so parallel inputs to that external process) to mix answers on this process standard output.在这种情况下,必须使用 RewriteLock 来防止并行请求(因此并行输入到该外部进程)在此进程标准 output 上混合答案。 It's a locking mechanism (a file, the given path, which is a classical token, only one user) to enforce serialization of the calls to this external mapping script.这是一种锁定机制(一个文件,给定的路径,这是一个经典的令牌,只有一个用户)来强制对这个外部映射脚本的调用进行序列化。 IMHO it should be transparently applied by mod-rewrite when using prg: as I never found a prg case where this locking thing is not mandatory . 恕我直言,在使用 prg 时应该由 mod-rewrite 透明地应用它:因为我从未发现这种锁定事物不是强制性的 prg 情况

Edit:编辑:

Well in fact you could use an external prg: without the rewriteLock if randomization of the output is not a problem, ie for a given entry you can get a response which was given for another entry, like in a script doing some advanced rnd:, your own round-robin service.事实上,你可以使用外部 prg: 如果 output 的随机化没有问题,则无需 rewriteLock,即对于给定条目,您可以获得为另一个条目给出的响应,例如在执行一些高级 rnd 的脚本中:,您自己的循环服务。 But if the output must reflect the entry, then you need that semaphore, which of course can slow down the rewritemap process.但是如果 output 必须反映条目,那么您需要那个信号量,这当然会减慢 rewritemap 过程。

So if you're only using the hashmap or textmap you do not need to set the RewriteLock.因此,如果您只使用 hashmap 或 textmap,则无需设置 RewriteLock。

Edit:编辑:

You may find useful details on this thread , like the fact the lock file exists only for a few milliseconds, when apache calls the prg and waits for an answer.您可能会在此线程上找到有用的详细信息,例如当 apache 调用 prg 并等待答案时,锁定文件仅存在几毫秒的事实。

Edit: On the question one strange fact is:编辑:关于这个问题,一个奇怪的事实是:

The original dev used an .htaccess file with RewriteMap原始开发人员使用了带有 RewriteMap 的.htaccess文件

This is strange because RewriteMap cannot work on .htaccess files, .htaccess are configuration entries read dynamically and RewriteMap as stated here in the Context line can only be set in the main configuration or in a VirtualHost configuration.这很奇怪,因为 RewriteMap 无法在 .htaccess 文件上工作,.htaccess 是动态读取的配置条目,并且上下文行中所述RewriteMap 只能在主配置或 VirtualHost 配置中设置。 It cannot be in a Location, a Directory or a .htaccess.它不能位于位置、目录或 .htaccess 中。 So chances are this will never work in a .htaccess.所以很有可能这在 .htaccess 中永远不会起作用。

Now @puk asked for an example of RewriteMap usage.现在@puk 要求提供 RewriteMap 用法的示例。 Well, searching for "RewriteMap" in Stack overflow will show you several real examples:好吧,在 Stack Overflow 中搜索“RewriteMap”会给你展示几个真实的例子:

Apache hangs if you define more than one RewriteLock directives or if you use it in a VHOST config.如果您定义多个 RewriteLock 指令或在 VHOST 配置中使用它,Apache 会挂起。

The RewriteLock should be specified at server config level and ONLY ONCE. RewriteLock 应该在服务器配置级别指定,并且只指定一次。 This lock file will be used by all prg type maps.此锁定文件将被所有 prg 类型映射使用。 So if you want to use multiple prg maps, I suggest using an internal locking mechanism, for example in PHP there is the flock function, and simply ignore the warning apache writes in the error log.因此,如果您想使用多个 prg 映射,我建议使用内部锁定机制,例如在 PHP 中有群 function,而只需忽略警告 ZB6EFD606D118D0F62066E31419FF04 写入错误日志。

See here for more info: http://books.google.com/books?id=HUpTYMf8-aEC&lpg=PP1&pg=PA298#v=onepage&q&f=false请参阅此处了解更多信息: http://books.google.com/books?id=HUpTYMf8-aEC&lpg=PP1&pg=PA298#v=onepage&q&f=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM