【问题标题】:Exclude one directory from Nginx password authenticationNginx密码认证排除一个目录
【发布时间】:2017-11-18 11:41:37
【问题描述】:

我已经将我的 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;
        }
}

【问题讨论】:

    标签: ubuntu nginx debian-based


    【解决方案1】:

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

    如果您在/t/sms/plivo/ 下有任何 PHP 文件,您将需要添加一个嵌套的位置块来处理这些文件。

    例如:

    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 语句,除非您有一些我看不到的deny 规则。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-02
        • 2020-05-31
        • 2013-12-05
        • 1970-01-01
        • 2013-11-06
        相关资源
        最近更新 更多