2012-05-07 84 views
0

在運行Nginx的Arch Linux服務器上,我正確安裝了cgit。我想用一個基本的認證密碼保護cgit,除了一個目錄/pub/。如在the documentation上看到的那樣,我考慮將server上下文作爲認證,並且/pub/目錄的location上下文得到例外。我試圖this link正確地獲取路徑。Nginx和auth_basic與cgit

這裏是相應部分的nginx配置文件。

server { 
    listen  80; 
    server_name git.nicosphere.net; 
    index cgit.cgi; 
    gzip off; 

    auth_basic "Restricted"; 
    auth_basic_user_file /srv/gitosis/.htpasswd; 

    location/{ 
     root /usr/share/webapps/cgit/; 
    } 

    location ^~ /pub/ { 
     auth_basic off; 
    } 

    if (!-f $request_filename) { 
     rewrite ^/([^?/]+/[^?]*)?(?:\?(.*))?$ /cgit.cgi?url=$1&$2 last; 
    } 

    location ~ \.cgi$ { 
     gzip off; 
     include fastcgi_params; 
     fastcgi_pass 127.0.0.1:9001; 
     fastcgi_index cgit.cgi; 
     fastcgi_param SCRIPT_FILENAME /usr/share/webapps/cgit/cgit.cgi; 
     fastcgi_param DOCUMENT_ROOT /usr/share/webapps/cgit/; 
    } 
} 

這要求我認證任何網址。對於一些更簡單的測試,我試圖在沒有身份驗證的情況下離開root,並且只使用/pub/進行身份驗證。在這種情況下,它根本不要求密碼。到目前爲止,我設法保護一切或沒有。

感謝您的幫助,並對我的近似英語表示歉意。

回答

1

我想你想是這樣的:

server { 
    listen  80; 
    server_name git.nicosphere.net; 
    index cgit.cgi; 
    gzip off; 

    root /usr/share/webapps/cgit; 

    # $document_root is now set properly, and you don't need to override it 
    include fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME $document_root/cgit.cgi; 

    location/{ 
     try_files $uri @cgit; 
    } 

    # Require auth for requests sent to cgit that originated in location/ 
    location @cgit { 
     auth_basic "Restricted"; 
     auth_basic_user_file /srv/gitosis/.htpasswd; 

     gzip off; 
     # rewrites in nginx don't match the query string 
     rewrite ^/([^/]+/.*)?$ /cgit.cgi?url=$1 break; 
     fastcgi_pass 127.0.0.1:9001; 
    } 

    location ^~ /pub/ { 
     gzip off; 
     rewrite ^/([^/]+/.*)?$ /cgit.cgi?url=$1 break; 
     fastcgi_pass 127.0.0.1:9001; 
    } 
} 
+0

謝謝你, 這是更好的,但是有一個奇怪的現象,當我去'/酒吧/',它要求我輸入密碼,當我按'取消'時,它顯示頁面(沒有CSS和.PNG)。當我嘗試根目錄時,它確實顯示401未經授權的頁面。如果我認證正確,一切都很好(使用CSS/PNG)。 所以,我想我仍然有問題,但它更好。 –

+0

對我來說工作非常好,非常感謝。 –