如何使用用户/密码通过NGINX通过HTTP提供GIT?

时间:2011-06-20 16:09:15

标签: git nginx

尽管我已经找到了关于如何配置git / nginx以获取我的回购的所有链接,但我无法让它们工作。

我遵循了本教程Git repository over HTTP WebDAV with nginx,但用户/密码限制不起作用。任何人都可以克隆存储库。

我来自使用SVN + Apache + DAV_SVN的配置,带有密码文件(使用htpasswd创建)和authz文件。我想使用git + nginx做同样的事情。怎么可能?

感谢您的帮助!

3 个答案:

答案 0 :(得分:20)

请查看以下文章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;
        }
    }
}

这样做是将位于/ git后面的repo传递给/usr/lib/git-core/git-http-backend。例如,http://git.mydomain.com/git/someapp将指向someapp存储库。此回购邮件将位于/srv/git/someappfastcgi_param定义的GIT_PROJECT_ROOT,并且可以更改为适合您的服务器。

这非常有用,您可以将HttpAuthBasicModule应用于nginx以通过HTTP密码保护您的repo访问。

修改:如果您遗漏git-http-backend,可以在Ubuntu / Debian或基于RPM的平台上安装git-core软件包,查看How can git be installed on CENTOS 5.5?

答案 1 :(得分:14)

以下是Git over HTTP的完整配置,包括TLS加密,基本身份验证和GitWeb。我假设存储库' root位于/home/git。您应该将example.com替换为您的域名。

# Remove this block if you don't want TLS
server {
    listen 80;
    server_name git.example.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.example.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.example.com/fullchain.pem;
    ssl_certificate_key       /etc/letsencrypt/live/git.example.com/privkey.pem;
    ssl_protocols             TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    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 "example 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.example.com --rsa-key-size 4096

要访问Gitweb,只需浏览git.example.com即可。您还需要对其进行配置以设置存储库' root:

sudo vim /etc/gitweb.conf

要获得HTTP Basic Auth,您必须使用htpasswd命令将用户添加到/etc/nginx/.htpasswd

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

下次运行命令时删除-c开关,因为它只创建文件(Nginx默认情况下在其配置目录中没有.htpasswd文件)。

如果你想要更复杂,更强大的GitHub,请检查Gitlab

答案 2 :(得分:0)

要添加更多详细信息,我们需要3个组成部分:nginxgit-http-backendfcgiwrap

  • git-http-backend是可从https://github.com/git/git构建的独立可执行二进制文件。这是处理git http / https访问的官方解决方案,我不知道它是否是存在的最好的解决方案。
  • Nginx没有内置的常规FastCGI服务器(或者我找不到正确使用nginx的fastcgi_bind的方法)。 因此,应该使用另一个fastcgi服务器,例如fcgiwarp(一本不错的手册https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/
  • 在nginx配置中使用fastcgi_pass unix:/tmp/cgi.sock;(参考其他答案)

==更新==

fastcgi不是必须的,git-http-backend不仅是针对fastcgi编写的,而且fastcgi不是最简单的也不是一种性能。 例如,我编写了一个servlet,使用nginx的proxy_pass在nginx和git-http-backend之间进行交互,它也可以工作!