仅用于 POST 的 NGINX 基本身份验证

8 nginx mercurial http-basic-authentication

我正在设置 nginx 来为 Mercurial 存储库提供服务。它在根本不使用基本身份验证或当我完全使用基本身份验证时有效。

我想要做的是只对 POST 请求使用基本身份验证,因此任何人都可以获取访问权限,但只有经过身份验证的用户才能推送。

我尝试了以下,

if ($request_method = POST) {
  auth_basic "Restricted";
  auth_basic_user_file /path/to/userfile
}
Run Code Online (Sandbox Code Playgroud)

但是它抱怨“此处不允许使用 auth_basic 指令”。

我该如何解决这个问题?

Mit*_*tar 14

你应该使用limit_except

limit_except GET HEAD {
    auth_basic 'Restricted';
    auth_basic_user_file /path/to/userfile;
}
Run Code Online (Sandbox Code Playgroud)

它从 nginx 0.8.48 开始工作,在旧版本中存在一个错误,fastcgi_pass在 limit_except 块中没有继承它。