2017-06-15 61 views
1

我已經設置了我的Nginx服務器以對所有內容進行身份驗證,但是我想排除/var/www/html/t/sms/plivo之下的所有文件以進行密碼驗證。我試過使用不同的路徑,但當我嘗試從我的瀏覽器訪問/var/www/html/t/sms/plivo下的文件時,它總是要求輸入密碼。從Nginx密碼驗證中排除一個目錄

下面是我的/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; 
     } 
} 

回答

1

location =語法匹配一個URI,而不是所有的URI的下它。此外,您應該使用^~修飾符來防止正則表達式location塊發生干擾。有關location塊的評估訂單的規則,請參閱this document

如果你有任何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聲明,除非您有一些我看不到的規則deny