Git 服务器基于nginx配置http(s)协议

Git HTTP协议简介

从git官方文档上看,git实现http(s)协议有两种方式

  1. 智能(Smart)HTTP
  2. 哑(Dump)HTTP

其中Smart HTTP是自Git 1.6.6版本开始引入的一种新的、更智能的协议。基于该协议可以实现对git仓库的读写功能。而在Dump HTTP里 web 服务器仅把裸版本库当作普通文件来对待,提供文件服务,更多的是用来实现只读功能。

基于Nginx配置http(s)

安装相关依赖

Git官方文档里,配置Git server支持http协议拿的是Apache httpd举例的。经过查验,如果希望用nginx做webserver,则需要另外的fcgiwrap以及spawn-fcgi。

具体的逻辑则是:nginx若要支持git-http-backend,需要fcgiwarp的帮助。而如果希望fcgiwarp以daemon方式运行,则需要spawn-fcgi。既然知道了逻辑,那就准备开干吧。好在安装比较简单。

1
2
3
4
5
6
7
yum install fcgi-devel spawn-fcgi
#以上需要epel源
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure
make && make install

借用github上别人写的启动脚本(详见参考资料),按照自己的实际环境,修改好放置/etc/init.d/下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
#
# git-fcgi The Git HTTP/FastCGI server
#
# chkconfig: - 80 20
# processname: git-fcgi
# description: Git HTTP/FastCGI server
# pidfile: /var/run/git-fcgi.pid

### BEGIN INIT INFO
# Provides: git-fcgi
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop Git HTTP/FastCGI server
### END INIT INFO

# Source function library.
. /etc/init.d/functions

# Config & Vars
prog=git-fcgi
childs=1
pidfile=/var/run/git-fcgi.pid
lockfile=/var/lock/subsys/git-fcgi
sockfile=/var/run/git-fcgi.sock
sockmode=0700;
sockuser=nginx
sockgroup=nginx
proguser=git
proggroup=git
gitexec=/usr/libexec/git-core/git-http-backend
fcgiexec=/usr/local/sbin/fcgiwrap
spawnexec=/usr/bin/spawn-fcgi
progexec="${spawnexec} -u ${proguser} -g ${proggroup} -U ${sockuser} -G ${sockgroup} -P ${pidfile} -s ${sockfile} -M ${sockmode} -- ${fcgiexec} -f -c ${childs} -p ${gitexec}"
RETVAL=0

# Functions
start() {
echo -n $"Starting ${prog}: "
[ -n "${sockfile}" -a -S "${sockfile}" ] && rm -f ${sockfile}
daemon "${progexec} > /dev/null"
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}

stop() {
echo -n $"Stopping ${prog}: "
[ -n "${sockfile}" -a -S "${sockfile}" ] && rm -f ${sockfile}
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
return $RETVAL
}

restart() {
stop
start
}

reload() {
restart
}

force_reload() {
restart
}

rh_status() {
status -p ${pidfile} ${prog}
}

# Main
case "$1" in
start)
rh_status > /dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
restart
;;
reload)
reload
;;
force-reload)
force_reload
;;
condrestart|try-restart)
if rh_status > /dev/null 2>&1; then
restart
fi
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|force_reload|condrestart|try-restart|status|help}"
RETVAL=2
esac

exit $RETVAL
1
2
3
4
5
chmod u+x /etc/init.d/git-fcgi
service git-fcgi start
chkconfig git-fcgi on
#将git仓库的所属权限修改为$proguser
chown -R git.git yougit_dir

配置nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#Git nginx conf
server {
listen 80;
server_name yougit.git.com;
#return 301 https://$server_name$request_uri;
root /data/git/;
error_log /var/log/nginx/git.error.log warn;
access_log /var/log/nginx/git.access.log main;

location ~ ^/([^/]+\.git)(/.*|$) {
include fastcgi_params;
fastcgi_param PATH_INFO $uri;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
fastcgi_param GIT_PROJECT_ROOT $document_root;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_pass unix:/var/run/git-fcgi.sock;
}
}

现在,你可以使用git clone http://xxx.xxx/**.git 的方式操作了。

尾巴

若需要使用验证,可使用htpasswd生成用户名以及密码,再配置到nginx的配置文件中。若仓库接收git push时报错403,可在仓库中设置git config http.receivepack true

参考资料