Cyril N. Cyril N. - 1 month ago 13
Git Question

How to serve GIT through HTTP via NGINX with user/password?

Despite all the links I've found on how to configure git/nginx to get my repos, I can't make them work.

I followed this tutorial, Git repository over HTTP WebDAV with nginx, but the user/password restriction doesnt' work. Anyone can clone the repository.

I'm from a configuration using SVN + Apache + DAV_SVN, with a file for password (created with htpasswd), and a file for the authz. I'd like to do the same, using git+nginx. How's that possible ?

Thanks for your help!

Answer

Take a look at the following article, http://www.toofishes.net/blog/git-smart-http-transport-nginx/

It provides a sample nginx config:

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;
        }
    }
}

What this does is pass your repo which is located after /git in the url, to /usr/lib/git-core/git-http-backend. Example, http://git.mydomain.com/git/someapp would point to the someapp repository. This repo would be located in /srv/git/someapp as defined in the fastcgi_param of GIT_PROJECT_ROOT and can be changed to fit your server.

This is very useful and you can apply HttpAuthBasicModule to nginx to password protect your repo's access via HTTP.

Edit: If you are missing git-http-backend, you can install the git-core package on Ubuntu/Debian or on RPM based platforms look at How can git be installed on CENTOS 5.5?