2013-08-29 19 views
0

我已經建立了一個測試VirtualBox的/ Debian的Wheezy7.1機裸淨安裝機+ nginx的+ PHP-FPMPHP不是一個位置指令

我有SSL,PHP,basic_auth內工作並允許/拒絕在服務器級別上工作。

但是,如果我想在auth東西是隻有一條路徑中,auth的作品,但PHP的東西不(在index.php獲取網頁瀏覽器下載)

我知道它有什麼做nginx怎麼位置指令比賽,但我不知道它是什麼...

這裏是我的配置文件:

server { 
     listen   80; 
     server_name www.test.com; 
     rewrite  ^https://$server_name$request_uri? permanent; 
} 



# HTTPS server 

server 
{ 
    listen 443; 
    server_name www.test.com; 

    root /srv/vhosts/www.test.com/html; 
    index index.php ; 

    ssl on; 
    ssl_certificate /etc/nginx/certs/STAR.test.com.crt; 
    ssl_certificate_key /etc/nginx/certs/STAR.test.com.key; 

    ssl_session_timeout 5m; 

    ssl_protocols SSLv3 TLSv1; 
    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP; 
    ssl_prefer_server_ciphers on; 


    location/{ 
     try_files $uri $uri/ =404; 
    } 
     location ~ \.php$ { 
       try_files $uri =404; 
       fastcgi_split_path_info ^(.+\.php)(/.+)$; 
       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini 

       # With php5-fpm: 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_index index.php; 
       include fastcgi_params; 
     } 

     # deny access to .htaccess files, if Apache's document root 
     # concurs with nginx's one 
     # 
     location ~ /\.ht { 
       deny all; 
     } 

    location ^~ /testdir/ { 
     auth_basic "gib login"; 
     auth_basic_user_file /etc/nginx/htpasswd/www.test.com.htpasswd; 
     allow 192.168.1.3; # my workstation ip 
     deny all; 
    } 
} 

編輯:看的第一個評論,謝謝!

+0

我設法解決了我的問題,將'location ^〜/ testdir /'更改爲'location/testdir /'。我仍然不知道這是爲什麼會起作用,並希望有人能詳細說明。謝謝! – Kaurin

回答

2

According to the nginx documentation,位置指令的順序很重要。

要確定哪個位置指令與特定查詢匹配,首先檢查 文字字符串。字符串匹配查詢的開始部分 - 將使用最具體的匹配。 之後,按照 中定義的順序檢查正則表達式的配置文件。匹配 查詢的第一個正則表達式將停止搜索。如果找不到正則表達式匹配 ,則使用文字字符串搜索的結果。

嘗試組織您的位置指令,以便首先保護您要保護的目錄,然後是PHP,然後是您的try__files指令。我在我的系統上做了一個快速測試,按照以下順序構建位置塊,允許testdir被保護,並且index.php仍然被執行。

location ^~ /testdir/ { 
    auth_basic "gib login"; 
    auth_basic_user_file /etc/nginx/htpasswd/www.test.com.htpasswd; 
    allow 192.168.1.3; # my workstation ip 
    deny all; 
} 

location ~ \.php$ { 

} 

location ~/{ 
    try_files $uri $uri/ 404; 
} 
+0

謝謝你的幫助!你能看看問題評論部分的評論嗎?謝謝! – Kaurin

+0

看看我提供的文檔鏈接。 Nginx以特定的順序處理位置塊。 [這個答案](http://stackoverflow.com/questions/1011101/nginx-location-directive-doesnt-seem-to-be-working-am-i-missing-something/1099252#1099252)提供了一些更深入的瞭解那。 –