nginx basic_auth 豁免问题

nginx basic_auth 豁免问题

我正在尝试将某个位置从 basic_auth 中豁免,而站点的其余部分确实需要它。阅读它后,我的配置应该可以工作,但出于某种原因,它仍然为 /asd/qwe/ 返回 401。

server {
    server_name domain.com www.domain.com;

    root /var/www/domains/domain.com/htdocs/public;

    index index.php index.htm;

    location / {
        location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.domain.com.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /var/www/domains/domain.com/htdocs/public$fastcgi_script_name;
        }

      auth_basic "Restricted";
      auth_basic_user_file /var/www/domains/htpasswd/domain.com.htpasswd;

    }

    location /asd/qwe/ {
          auth_basic off;
          allow all;

        location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.domain.com.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /var/www/domains/domain.com/htdocs/public$fastcgi_script_name;
        }
    }

}

答案1

最终找到了解决方案:

server {
  server_name domain.com www.domain.com;
  root /var/www/domains/domain.com/htdocs/public;
  access_log /var/log/nginx/domains/domain.com/access.log;
  error_log /var/log/nginx/domains/domain.com/error.log;

  index index.php index.htm;

  auth_basic_user_file /var/www/domains/htpasswd/domain.com.htpasswd;

  location ~ /.svn/ {
      deny all;
  }

  location ~ /.htaccess {
      deny all;
  }

  location ^~ /psp/callback/ {
        auth_basic off;

        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.domain.com.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/domains/domain.com/htdocs/public$fastcgi_script_name;
   }

  location / {
    auth_basic "Restricted";
  }

  location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/var/run/php5-fpm.domain.com.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /var/www/domains/domain.com/htdocs/public$fastcgi_script_name;
    }

  # Zend Framework rewrite
  if (!-e $request_filename) {
      set $zfRewrite 1;
  }
  if ($zfRewrite = 1) {
      rewrite ^.*$ /index.php last;
  }
}

答案2

除非您使用文字匹配(= 位置前缀),否则处理会在第一次匹配时停止。将您的 /asd/qwe 块放在 / 块上方,它应该可以工作。

有关更多信息,请参阅有关 location 指令的 nginx 文档:

http://wiki.nginx.org/HttpCoreModule#location

相关内容