了解 Apache RewriteMap 和 RewriteLock

发布于 2024-11-19 16:37:48 字数 460 浏览 3 评论 0原文

我接手了一个相当重型的 LAMP 应用程序的开发。原始开发人员使用带有 RewriteMap 的 .htaccess 文件和 PHP 脚本来处理应用程序的某些条件。

具体来说,当客户端请求某些子域模式时,RewriteMap 捕获它们并将它们发送到适当的应用程序模块。

我对典型的 mod_rewrite 重定向非常满意,并且我认为我已经弄清楚了基本的 RewriteMap 概念;但我正在努力寻找有关 RewriteLock 如何工作的合适文档。根据 Apache 文档:

该指令设置 mod_rewrite 需要与 RewriteMap 程序通信的同步锁定文件的文件名。当您想要使用重写映射程序时,请将此锁定文件设置为本地路径(不在安装 NFS 的设备上)。其他类型的重写映射不需要它。

但这对我来说仍然有点模糊。 RewriteLock 的确切用途和功能是什么?它是如何工作的?

I've taken over development of a fairly heavy-duty LAMP application. The original dev used an .htaccess file with RewriteMap and a PHP script to handle certain conditions of the app.

Specifically, when certain subdomain patterns are requested by the client, the RewriteMap catches them and sends them to the appropriate application module.

I'm quite comfortable with typical mod_rewrite redirects, and I think I've got the basic RewriteMap concept figured out; but I'm struggling to find decent documentation on how RewriteLock works. According to the Apache docs:

This directive sets the filename for a synchronization lockfile which mod_rewrite needs to communicate with RewriteMap programs. Set this lockfile to a local path (not on a NFS-mounted device) when you want to use a rewriting map-program. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-11-26 16:37:48

RewriteLock 与 prg: 关键字一起使用。 RewriteMap 可以与多个关键字一起使用,以使用文本文件 (txt:)、哈希文件 (dbm:)、随机文本 (rnd:)或外部映射脚本(这是 prg: 关键字)。在此模式下,外部脚本在 apache 启动时启动。然后,对于每个传入请求,当 mod-rewrite 调用 prg: 映射时,apache 会将输入发送到该脚本并读取输出流以获取值。

在这种情况下,必须使用 RewriteLock 来防止并行请求(因此并行输入到该外部进程)在此进程标准输出上混合答案。它是一种锁定机制(一个文件、给定路径、一个经典令牌、只有一个用户),用于强制对该外部映射脚本的调用进行序列化。 恕我直言,在使用 prg 时,应该通过 mod-rewrite 透明地应用它:因为我从未发现 prg 情况下此锁定不是强制性的

编辑:

事实上,您可以使用外部prg:如果输出的随机化不是问题,则无需rewriteLock,即对于给定的条目,您可以获得另一个条目给出的响应,例如在脚本中执行一些高级 rnd:,您自己的循环服务。但是,如果输出必须反映条目,那么您就需要该信号量,这当然会减慢重写映射过程。

因此,如果您仅使用哈希图或文本图,则无需设置 RewriteLock。

编辑:

您可能会在此线程上找到有用的详细信息,就像当 apache 调用 prg 并等待答案时,锁定文件仅存在几毫秒的事实。

编辑:
关于这个问题,一个奇怪的事实是:

原始开发人员使用了带有 RewriteMap 的 .htaccess 文件

这很奇怪,因为 RewriteMap 无法处理 .htaccess 文件,.htaccess 是动态读取的配置条目,而 RewriteMap 如上所述 此处 Context 行只能在主配置或 VirtualHost 配置中。它不能位于位置、目录或 .htaccess 中。所以这很可能永远不会在 .htaccess 中起作用。

现在@puk 要求提供 RewriteMap 用法的示例。好吧,在 Stack Overflow 中搜索“RewriteMap”将向您展示几个真实的示例:

RewriteLock is used with the prg: keyword. 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 ). In this mode the external script is launched when apache start. 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.

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. 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.

Edit:

Well in fact you could use an external prg: without the rewriteLock if randomization of the output is not a problem, i.e. 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. But if the output must reflect the entry, then you need that semaphore, which of course can slow down the rewritemap process.

So if you're only using the hashmap or textmap you do not need to set the 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.

Edit:
On the question one strange fact is:

The original dev used an .htaccess file with RewriteMap

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. It cannot be in a Location, a Directory or a .htaccess. So chances are this will never work in a .htaccess.

Now @puk asked for an example of RewriteMap usage. Well, searching for "RewriteMap" in Stack overflow will show you several real examples:

向地狱狂奔 2024-11-26 16:37:48

如果您定义多个 RewriteLock 指令或者在 VHOST 配置中使用它,Apache 就会挂起。

RewriteLock 应在服务器配置级别指定,并且只能指定一次。此锁定文件将由所有 prg 类型映射使用。所以如果你想使用多个prg映射,我建议使用内部锁定机制,例如在PHP中有flock函数,并且简单地忽略apache在错误日志中写入的警告。

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

Apache hangs if you define more than one RewriteLock directives or if you use it in a VHOST config.

The RewriteLock should be specified at server config level and ONLY ONCE. This lock file will be used by all prg type maps. 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.

See here for more info:
http://books.google.com/books?id=HUpTYMf8-aEC&lpg=PP1&pg=PA298#v=onepage&q&f=false

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文