从Nginx密码身份验证中排除一个目录

时间:2017-06-15 20:38:59

标签: ubuntu nginx debian-based

我已经设置了我的Nginx服务器以对所有内容进行身份验证,但我想排除/var/www/html/t/sms/plivo下的所有文件以进行密码身份验证。我尝试过使用不同的路径但是当我尝试从浏览器访问/var/www/html/t/sms/plivo下的文件时,它总是要求输入密码。

以下是我的/etc/nginx/sites-available/default文件

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        auth_basic "Private Property";
        auth_basic_user_file /etc/nginx/.htpasswd;

        #no password for the plivo folder so we can recieve messages!
        location = /t/sms/plivo/ {
                auth_basic off;
                allow all; # Allow all to see content
        }

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

2 个答案:

答案 0 :(得分:1)

y a NaN b 21.5 语法匹配一个URI而不是其下的所有URI。此外,您应该使用location =修饰符来阻止正则表达式^~块干扰。有关location块的评估顺序的规则,请参阅this document

如果location下有任何PHP文件,则需要添加嵌套位置块来处理这些文件。

例如:

/t/sms/plivo/

location ^~ /t/sms/plivo/ { auth_basic off; allow all; # Allow all to see content location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } } 块除了您配置中已具有相同名称的块之外。并且,您可能不需要location ~ \.php$语句,除非您有一些我看不到的allow all规则。

答案 1 :(得分:0)

希望它对任何人都有帮助-我们必须跳过url下所有uri的身份验证,所以

location ^~ /some/location/to_skip/ {
  auth_basic off;
  try_files $uri $uri/ /index.html;
}