我按照这个教程Git repository over HTTP WebDAV with nginx进行操作,但是用户/密码限制不起作用。任何人都可以克隆存储库。
之前我使用的是SVN + Apache + DAV_SVN配置,并且有一个用htpasswd创建的密码文件和一个用于权限控制的authz文件。我想要做同样的事情,使用git+nginx。这可能吗?
感谢您的帮助!
我按照这个教程Git repository over HTTP WebDAV with nginx进行操作,但是用户/密码限制不起作用。任何人都可以克隆存储库。
之前我使用的是SVN + Apache + DAV_SVN配置,并且有一个用htpasswd创建的密码文件和一个用于权限控制的authz文件。我想要做同样的事情,使用git+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中/gir之后的repo传递给 /usr/lib/git-core/git-http-backend
。例如,http://git.mydomain.com/git/someapp
将指向 someapp
代码库。该仓库位于GIT_PROJECT_ROOT
的 fastcgi_param
中定义为 /srv/git/someapp
,可以更改以适应您的服务器。
这非常有用,您可以在nginx上应用HttpAuthBasicModule
来通过HTTP保护您的repo访问密码。
编辑:如果缺少git-http-backend
,则可以在Ubuntu/Debian上安装git-core
软件包,或者在基于RPM的平台上查看如何在CENTOS 5.5上安装git?
/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。您还需要配置它以设置存储库的根目录:
sudo vim /etc/gitweb.conf
htpasswd
命令将用户添加到/etc/nginx/.htpasswd
中:sudo apt-get install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd username
-c
开关,因为它只会创建文件(Nginx在其配置目录中默认没有.htpasswd文件)。504
错误。我只对您的配置进行了微小的调整。您是否曾经让它工作过? - Moritz Friedrich添加更多细节,我们需要3个组件:nginx
、git-http-backend
和fcgiwrap
。
git-http-backend
是一个独立的可执行二进制文件,可以从https://github.com/git/git构建。它是处理git http/https访问的官方解决方案,我不知道是否存在更好的解决方案。fastcgi_bind
),因此应该使用另一个FastCGI服务器,例如fcgiwarp
(一个很好的手册https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/)fastcgi_pass unix:/tmp/cgi.sock;
(参考其他答案)fastcgi
不是必须的,git-http-backend
也不仅限于fastcgi,而且fastcgi也不是最简单、性能最好的选择。
例如,我编写了一个servlet来在nginx和git-http-backend之间进行交互,使用了nginx的proxy_pass
,它也可以正常工作!
git init --bare
创建了一个裸仓库。也就是说,不要托管检出的代码,除非你正在使用git clone <repo> --bare
。除此之外,谷歌应该可以帮到你。 - Mike Mackintoshgit update-server-info
命令,这将生成一个.git/info/refs
文件,以解决我遇到的404错误。 - ThorSummonerfastcgi_pass localhost:9001;
更改为fastcgi_pass unix:/run/fcgiwrap.socket;
。 - Galvani