2011-06-20 29 views
14

儘管我已經找到了關於如何配置git/nginx來獲得我的回購的所有鏈接,但我無法讓它們工作。如何通過使用用戶/密碼的NGINX通過HTTP服務GIT?

我遵循本教程Git repository over HTTP WebDAV with nginx,但用戶/密碼限制不起作用。任何人都可以克隆存儲庫。

我來自一個使用SVN + Apache + DAV_SVN的配置,帶有一個用於密碼的文件(使用htpasswd創建)以及一個authz文件。我想用git + nginx來做同樣的事情。那可能怎麼樣?

感謝您的幫助!

回答

18

看看下面的文章,http://www.toofishes.net/blog/git-smart-http-transport-nginx/

它提供了一個樣本nginx的配置:

http { 
    ... 
    server { 
     listen  80; 
     server_name git.mydomain.com; 

     location ~ /git(/.*) { 
      # fcgiwrap is set up to listen on this host:port 
      fastcgi_pass localhost:9001; 
      include  fastcgi_params; 
      fastcgi_param SCRIPT_FILENAME  /usr/lib/git-core/git-http-backend; 
      # export all repositories under GIT_PROJECT_ROOT 
      fastcgi_param GIT_HTTP_EXPORT_ALL ""; 
      fastcgi_param GIT_PROJECT_ROOT /srv/git; 
      fastcgi_param PATH_INFO   $1; 
     } 
    } 
} 

這樣做是通過你的回購其位於URL /混帳後,以/usr/lib/git-core/git-http-backend 。例如,http://git.mydomain.com/git/someapp將指向someapp存儲庫。此回購將位於GIT_PROJECT_ROOTfastcgi_param中定義的/srv/git/someapp,並且可以更改爲適合您的服務器。

這非常有用,您可以將HttpAuthBasicModule應用於nginx,以通過HTTP密碼保護您的repo訪問。

編輯:如果你缺少git-http-backend,你可以在Ubuntu/Debian或者基於RPM平臺上安裝git-core包看How can git be installed on CENTOS 5.5?

+0

我按照這個例子在nginx上設置git。然而,每次我嘗試克隆回購時,我最終都會報錯: '致命:http:// :9000/git/test。混帳/信息/裁判無效:這是一個git重新 pository' 有沒有錯誤的/var/log/nginx/error.log和訪問日誌,我可以看到: - - [03/Oct/2015:08:35:42 +0200]「GET /git/test.git/info/refs?service=git-upload-pack HTTP/1.1」200 424859「 - 」「git/1.9.4.msysgit .1「 您是否遇到類似問題? 謝謝, Janusz –

+0

不行,但要確保它是一個由'git init --bare'創建的裸倉庫。也就是說,不要託管代碼,除非你在做'git clone --bare' - 除此之外,谷歌應該幫助 –

+0

我最終找到了適合我的例子:http://serverfault.com/questions/483726 /如何對化妝的git-智能HTTP的運輸工作,上nginx的。謝謝。 –

9

這裏是Git的一個完整的配置,以HTTP,使用TLS加密,基本身份驗證和GitWeb(一個非常簡單的存儲庫查看器)。我假設這個版本庫的根目錄在/ home/git中。

# Remove this block if you don't want TLS 
server { 
    listen 80; 
    server_name git.YOURDOMAIN.com; 
    return 301 https://$host$request_uri; 
} 

server { 
    listen  443 ssl; # Replace 443 ssl by 80 if you don't want TLS 
    server_name git.YOURDOMAIN.com; 
    root   /usr/share/gitweb; # Remove if you don't want Gitweb 

    error_log /home/git/nginx-error.log; 
    access_log /home/git/nginx-access.log; 

    # Remove ssl_* lines if you don't want TLS 
    ssl_certificate   /etc/letsencrypt/live/git.YOURDOMAIN.com/fullchain.pem; 
    ssl_certificate_key  /etc/letsencrypt/live/git.YOURDOMAIN.com/privkey.pem; 
    ssl_protocols    TLSv1 TLSv1.1 TLSv1.2; 
    ssl_prefer_server_ciphers on; 
    ssl_ciphers    'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; 

    # Remove auth_* if you don't want HTTP Basic Auth 
    auth_basic "YOURDOMAIN Git"; 
    auth_basic_user_file /etc/nginx/.htpasswd; 

    # static repo files for cloning over https 
    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ { 
     root /home/git/; 
    } 

    # requests that need to go to git-http-backend 
    location ~ ^.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ { 
     root /home/git/; 

     fastcgi_pass unix:/var/run/fcgiwrap.socket; 
     fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; 
     fastcgi_param PATH_INFO   $uri; 
     fastcgi_param GIT_PROJECT_ROOT $document_root; 
     fastcgi_param GIT_HTTP_EXPORT_ALL ""; 
     fastcgi_param REMOTE_USER $remote_user; 
     include fastcgi_params; 
    } 

    # Remove all conf beyond if you don't want Gitweb 
    try_files $uri @gitweb; 
    location @gitweb { 
     fastcgi_pass unix:/var/run/fcgiwrap.socket; 
     fastcgi_param SCRIPT_FILENAME /usr/share/gitweb/gitweb.cgi; 
     fastcgi_param PATH_INFO   $uri; 
     fastcgi_param GITWEB_CONFIG  /etc/gitweb.conf; 
     include fastcgi_params; 
    } 
} 

您必須安裝的Git,和的GitWeb FastCgiWrap:

sudo apt-get install git gitweb fcgiwrap 

對於TLS,我用Let's Encrypt免費證書。

sudo letsencrypt certonly -d git.YOURDOMAIN.com --rsa-key-size 4096 

要訪問Gitweb,只需瀏覽到git.YOURDOMAIN.com。你還需要將它配置設置倉庫根:

sudo vim /etc/gitweb.conf 

爲了得到HTTP基本認證,您必須使用htpasswd命令將用戶添加到/etc/nginx/.htpasswd

sudo apt-get install apache2-utils 
sudo htpasswd -c /etc/nginx/.htpasswd username 

刪除-c在下次運行命令時切換,因爲它只創建文件(Nginx默認情況下在其配置目錄中沒有.htpasswd文件)。

如果你想要更復雜,功能強大的GitHub,請檢查Gitlab

+0

我現在這個是一個很老的帖子,但我似乎無法得到推動工作 - 它總是導致一個'504'。我只對你的配置做了微小的調整。你有沒有這個工作? –

+0

是的,它正在工作(而且它仍然在工作,但是自從那個答案以來沒有觸及它)。 –

+0

你介意查找你安裝的fcgiwrap和git版本嗎?如果你來過德國,我願意贊助你一杯啤酒:) –

相關問題