如果传递了某个参数,nginxclosuresauth_basic的位置

有URL /index.php 。 访问它应该是受限制的,如果它的参数'foo'被设置为'bar',即/index.php应该被任何人自由地打开,并且打开/index.php?foo=bar只能在基本的授权。 我知道if指令,

 location = index.php { if ($arg_foo ~ bar) { auth_basic ""; auth_basic_user_file myauth; } fastcgi_pass unix:/var/run/php-fpm.socket; } 

…但是if不允许将auth_basic放在if子句中, if是一个单独的location 。 所以我认为应该有一种rewrite到另一个位置,可能会传递一个包含我们想要访问的原始位置的头文件,检查头文件并在通过身份validation之后重写到该位置,但是这看起来像是太多了一个黑客,可能会做得更容易,但我不知道如何。

我build议你使用map ,因为auth_basic有一个特殊的off值禁用authentication:

 http { [...] map $arg_foo $auth_realm { default off; ~bar ""; } server { [...] location = /index.php { auth_basic $auth_realm; auth_basic_user_file myauth; fastcgi_pass unix:/var/run/php-fpm.socket; } } } 

注意:我怀疑你提供的configuration片断不起作用,因为location指令的URI前缀不是以强制/字符开始的。