大网数据广告活动
服务器运维问答

Issues with nginx basic_auth exemption

· 52CE 我爱测

I'm trying to exempt a location from basic_auth whereas the rest of the site does require it. Reading up on it my config should work but for some reason it still returns 401 for /asd/qwe/. server { server_name domain.com www.doma…

问题原文

原帖链接 · user2108948 · CC BY-SA 3.0

I'm trying to exempt a location from basic_auth whereas the rest of the site does require it. Reading up on it my config should work but for some reason it still returns 401 for /asd/qwe/.

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;
        }
    }

}

已采纳解答

原帖链接 · user2108948 · CC BY-SA 4.0

Finnaly found a solution to it:

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;
  }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-09-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。