gpt4 book ai didi

apache 2.4,嵌套 If 指令

转载 作者:行者123 更新时间:2023-12-01 09:48:56 24 4
gpt4 key购买 nike

我尝试使用以下指令 block 在 apache 配置文件中设置一些环境变量,前提是设置了 header “X-Forwarded-Proto”。

<If "-n req('X-Forwarded-Proto')">
SetEnv NON_EMPTY_PROTO on
<If "req('X-Forwarded-Proto') =~ /https/">
SetEnv HTTPS on
</If>
<Else>
SetEnv HTTPS off
</Else>
</If>

因此,环境变量 NON_EMPTY_PROTO 已按预期设置,但变量 HTTPS 未设置。但是,如果我避免仅为测试而嵌套,则两个变量都按预期设置。

<If "-n req('X-Forwarded-Proto')">
SetEnv NON_EMPTY_PROTO on
</If>

<If "req('X-Forwarded-Proto') =~ /https/">
SetEnv HTTPS on
</If>
<Else>
SetEnv HTTPS off
</Else>

为什么在第一个示例中未验证内部 block ?指令文档没有提到任何关于可能或不可能嵌套的内容。

顺便说一句,我知道我可以使用下面这样的逻辑等价物,但如果场景变得更复杂,嵌套会更简单、更方便。

<If "-n req('X-Forwarded-Proto') && req('X-Forwarded-Proto') =~ /https/">
SetEnv HTTPS on
</If>
<ElseIf "-n req('X-Forwarded-Proto')">
SetEnv HTTPS off
</ElseIf>

我明确询问嵌套版本在这里不起作用的原因。

最佳答案

更新答案:

从 Apache 2.4.26 开始嵌套 <If>指令是允许的。

Apache 2.4 ChangeLog有以下变化:

Evaluate nested If/ElseIf/Else configuration blocks. [Luca Toscano, Jacob Champion]

嵌套 <If>问题中的指令应该在 Apache 2.4.26 及更高版本中工作。

虽然我无法确认问题中的具体规则,但这里有一个带有嵌套 <If> 的 htaccess 规则的工作示例指令:

RewriteEngine On

<If "%{REQUEST_URI} =~ /flintstones$/ ">
<If "%{QUERY_STRING} == 'character=fred'">
ErrorDocument 200 "Fred says, \"Yabba Dabba Doo\" <a href=\"flintstones\">back</a>"
</If>
<ElseIf "%{QUERY_STRING} == 'character=barney'">
ErrorDocument 200 "Barney says, \"Uh hee hee hee... Okay Fred!\" <a href=\"flintstones\">back</a>"
</ElseIf>
<Else>
ErrorDocument 200 "Pick a Flintstones character: <a href=\"flintstones?character=fred\">Fred</a> or <a href=\"flintstones?character=barney\">Barney</a>"
</Else>
RewriteRule ^ - [R=200,L]
</If>

原答案

<If>指令不能嵌套在另一个 <If> 中指令。

Apache 2.4 Documentation for the If Directive有以下通知:

Not a scripting language

The name of this directive is very familiar to programmers and admins but it should not be confused with its counterpart in scripting languages. For example, the current implementation does not contemplate the possibility of having a <If> section inside another one (the inner <If> will be ignored).

没有给出他们选择不允许嵌套 If 指令的原因。

唯一的other information about nesting provided在文档中指出每个部分类型在是否可以嵌套方面有所不同:

Nesting of sections

Some section types can be nested inside other section types. On the one hand, <Files> can be used inside <Directory>. On the other hand, <If> can be used inside <Directory>, <Location>, and <Files> sections (but not inside another <If>). The regex counterparts of the named section behave identically.

关于apache 2.4,嵌套 If 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com