如何使用auth_basic_user_file限制NGINX重写“位置”

时间:2015-02-05 03:50:33

标签: authentication nginx .htpasswd

我的NGINX服务器块中有以下位置块:

location ~ ^/admin {
  auth_basic "Restricted";
  auth_basic_user_file /etc/nginx/.htpasswd;
  if (!-e $request_filename){
      rewrite ^(.*)$ /index.php?uri=$1;
  }
}

但它不起作用。 /etc/nginx/.htpasswd文件存在并使用htpasswd命令生成:

sudo htpasswd -c /etc/nginx/.htpasswd admin

之前我已经完成了这项工作,但这次它刚刚停止工作。这次唯一的区别是我这次尝试在重写块内部执行auth_basic_user_file。

1 个答案:

答案 0 :(得分:0)

该配置无效,因为重写阶段if XYZ将始终在访问阶段auth_XYZ之前运行,无论您如何在位置块中排列它们。

See Nginx Phase Order

除了已有的身份验证要求之外,还需要将身份验证要求添加到php块中。