Problems with git-http-backend and Nginx

177 Views Asked by At

I'm trying to host a simple Git server on a Debian 11 ARM server, using stagit and a git hook to build purely static git pages, using git-http-backend as a smart git service. I've resolved the domain to the server IP (I'm using Cloudflare, and I have a CDN proxy turned on), and until I modify the Nginx configuration below, I can access the domain to get the Nginx welcome page.

I already have git, stagit, nginx, fcgiwrap, apache2-utils installed on my Debian server

But with my configuration, this git server keeps showing up as redirected (more than 20 times) and doesn't work at all

I created a git user with a home directory of /home/git, where I intend to store the git repository, e.g:

/home/git/repo1.git
/home/git/repo2.git
...

Each individual Git repository has the git-daemon-export-ok

ls /home/git/repo1.git
branches  config  description  git-daemon-export-ok  HEAD  hooks  info  objects  refs

/var/www/git is the directory where stagit builds its static git pages, and it has a directory structure like this

/var/www/git
├── favicon.png
├── index.html
├── logo.png
├── repo1.git
│   ├── atom.xml
│   ├── commit
│   ├── files.html
│   ├── index.html -> log.html
│   ├── log.html
│   ├── logo.png -> ../logo.png
│   ├── refs.html
│   ├── style.css -> ../style.css
│   └── tags.xml
├── repo2.git
│   ├── atom.xml
│   ├── commit
│   ├── files.html
│   ├── index.html -> log.html
│   ├── log.html
│   ├── logo.png -> ../logo.png
│   ├── refs.html
│   ├── style.css -> ../style.css
│   └── tags.xml
└── style.css

My Nginx configuration is as follows

server {
    listen 80;
    listen [::]:80;

    server_name git.mydomain.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name git.mydomain.com;
    root /var/www/git;
    index index.html index.htm index.nginx-debian.html;

    access_log /var/log/nginx/git.mydomain.com-access.log;
    error_log  /var/log/nginx/git.mydomain.com-error.log;
    gzip off;

    ssl_certificate /etc/nginx/cert/git.mydomain.com.pem;
    ssl_certificate_key /etc/nginx/cert/git.mydomain.com.key;

    location ~ /git_read(/.*) {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT /home/git;
        fastcgi_param REMOTE_USER $remote_user;
        fastcgi_param PATH_INFO $1;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }

    location ~ /git_write(/.*) {
        auth_basic "Require password to push to git.mydomain.com:";
        # HTTP Basic Auth for git push
        auth_basic_user_file /etc/nginx/.htpasswd;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT /home/git;
        fastcgi_param REMOTE_USER $remote_user;
        fastcgi_param PATH_INFO $1;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }

    location ~ /home/git(/.*) {
        if ($arg_service = git-receive-pack) {
            rewrite /home/git(/.*) /git_write$1 last;
        }
        if ($uri ~ ^/home/git/.*/git-receive-pack) {
            rewrite /home/git(/.*) /git_write$1 last;
        }
        if ($arg_service = git-upload-pack) {
            rewrite /home/git(/.*) /git_read$1 last;
        }
        if ($uri = ^/home/git/.*/git-receive-pack) {
            rewrite /home/git(/.*) /git_read$1 last;
        }
    }
}

When I try to clone a repository, such as /home/git/repo1.git against https://git.mydomain.com/repo1.git, it prompts

git clone git.mydomain.com/repo1.git                                                                   02:10:48
Cloning into 'stagit'...
fatal: unable to access 'https://git.mydomain.com/repo1.git/': Maximum (20) redirects followed

I also tried cURL

curl -i https://git.mydomain.com/repo1.git/info/refs\?service\=git-upload-pack                              9s 02:12:27

HTTP/2 301
date: Mon, 08 May 2023 18:13:14 GMT
content-type: text/html
location: https://git.mydomain.com/repo1.git/info/refs?service=git-upload-pack
cf-cache-status: DYNAMIC
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=DSwlz8PC%2Fwxsz35EsZTayjherWuZIeRCUgeP5fh1i6FvbNPndKIzAVIqvGlnPUKj%2Ba%2BqQiLvkw5w8409hOPR7ahtPyfEUD9jMs8irWWh6AHxnw8xxBN4sTtqFCWX"}],"group":"cf-nel","max_age":604800}
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
server: cloudflare
cf-ray: 7c43ace7ebcc492b-SIN
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>
0

There are 0 best solutions below