Dưới đây là một cấu hình đầy đủ cho Git qua HTTP, với mã hóa TLS, Basic Auth và GitWeb (trình xem kho rất đơn giản). Tôi giả định rằng thư mục gốc của kho nằm trong/home/git.
# Remove this block if you don't want TLS
server {
listen 80;
server_name git.YOURDOMAIN.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.YOURDOMAIN.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.YOURDOMAIN.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/git.YOURDOMAIN.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
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 "YOURDOMAIN 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;
}
}
Bạn phải cài đặt Git, Gitweb và FastCgiWrap:
sudo apt-get install git gitweb fcgiwrap
Đối với TLS, tôi sử dụng Let's Encrypt chứng chỉ miễn phí.
sudo letsencrypt certonly -d git.YOURDOMAIN.com --rsa-key-size 4096
Để truy cập Gitweb, chỉ cần duyệt tới git.YOURDOMAIN.com. Bạn cũng sẽ cần phải cấu hình nó để thiết lập thư mục gốc của kho:
sudo vim /etc/gitweb.conf
Để có được HTTP Basic Auth, bạn phải sử dụng lệnh htpasswd
thêm người dùng vào /etc/nginx/.htpasswd
:
sudo apt-get install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd username
Remove -c
chuyển sang lần sau khi bạn chạy lệnh, vì nó chỉ tạo tệp (Nginx không có tệp .htpasswd theo mặc định trong thư mục cấu hình của nó).
Nếu bạn muốn một cái gì đó phức tạp hơn, mạnh mẽ, GitHub giống như, hãy kiểm tra Gitlab.
Nguồn
2016-04-01 17:18:08
Tôi đã làm theo ví dụ này để thiết lập git trên nginx. Tuy nhiên mỗi lần tôi thử bản sao repo tôi kết thúc với lỗi: 'chết người: http: //: 9000/git/test.git/info/refs không hợp lệ: là này một git tái pository ' Không có lỗi được /var/log/nginx/error.log và trong bản ghi truy cập tôi có thể thấy: - - [03/Tháng 10 năm 2015: 08: 35: 42 +0200] "GET /git/test.git/info/refs?service=git-upload-pack HTTP/1.1" 200 424859 "-" "git/1.9.4.msysgit .1 " Bạn có gặp sự cố tương tự không? Cảm ơn, Janusz –
không, nhưng hãy đảm bảo rằng kho lưu trữ trống của nó được tạo bằng 'git init --bare'. tức là, không lưu trữ mã đã kiểm tra, trừ khi bạn đang thực hiện 'git clone --bare' - ngoài ra, các googles sẽ giúp –
Tôi cuối cùng đã tìm thấy ví dụ phù hợp với tôi: http://serverfault.com/questions/483726/how-to-make-git-thông minh-http-transport-work-on-nginx. Cảm ơn. –