Skip to content

Instantly share code, notes, and snippets.

@stbuehler
Created January 9, 2012 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stbuehler/1585144 to your computer and use it in GitHub Desktop.
Save stbuehler/1585144 to your computer and use it in GitHub Desktop.
smart http git + cgit with lighttpd2 (needs fcgi-cgi)
git = {
docroot "/home/gitosis/repositories";
mime_types (
# css for cgit
".css" => "text/css; charset=utf-8",
# git mime types, default for objects files
"HEAD" => "text/plain",
"alternates" => "text/plain",
"http-alternates" => "text/plain",
"refs" => "text/plain",
"packs" => "text/plain; charset=utf-8",
".pack" => "application/x-git-packed-objects",
".idx" => "application/x-git-packed-objects-toc",
"" => "application/x-git-loose-object"
);
# if you want to generate objects/info/packs on the fly (git-update-server-info should keep it synced), exclude it here: and req.path !~ "\.git/objects/info/packs$"
if phys.is_file and !req.query and req.path =~ "\.git/(HEAD|info|objects|refs|description)(/|$)" {
# static file handling
if req.path =~ "\.git/(HEAD|objects/info/alternates|objects/info/http-alternates|info/refs)$" {
header.add "Expires" => "Fri, 01 Jan 1980 00:00:00 GMT";
header.add "Pragma" => "no-cache";
header.add "Cache-Control" => "no-cache, max-age=0, must-revalidate";
} else {
# packs and objects don't "change" (even if they are deleted they are still valid)
expire "access plus 1 year";
}
static;
} else if req.path !~ "(^|\.git)/" and phys.is_dir {
# dirlisting for directories, unless we want to handle them with cgit
dirlist ( "encode-readme" => false, "exclude-prefix" => ( "gitosis-admin.git", "gitolite-admin.git", "branches", "hooks", "logs", "config", "git-daemon-export-ok" ) );
} else if req.path =~ "\.git/(info/refs|git-(upload|receive)-pack|objects/info/packs)$" {
if req.path =~ "\.git/git-receive-pack$" or req.query =~ "(^|&)service=git-receive-pack" {
# no push (anonymous push is disabled by default anyway)
access.deny;
}
env.set "PATH_INFO" => "%{req.path}";
env.set "SCRIPT_NAME" => "/";
# for git in a subdirectory you need to wrap everything into a conditional block, and replace the docroot with alias:
# if req.path =^ "/git" { ... }
# alias "/git" => "/home/gitosis/repositories";
# and use these PATH_INFO / SCRIPT_NAME settings:
# if req.path =~ "/git(/.*)$" {
# env.set "PATH_INFO" => "%1";
# env.set "SCRIPT_NAME" => "/git";
# }
env.set "GIT_PROJECT_ROOT" => "%{phys.docroot}";
env.set "GIT_HTTP_EXPORT_ALL" => ""; # export all - remove if you want to use git-daemon-export-ok
env.set "SCRIPT_FILENAME" => "/usr/lib/git-core/git-http-backend"; # path to git-http-backend binary
fastcgi "unix:/var/run/lighttpd/sockets/www-cgi.sock";
# else handle with cgit / static files for cgit
} else if request.path =^ "/static/" {
alias ( "/static/" => "/usr/share/cgit" );
} else {
rewrite "/cgit.cgi?url=%{enc:request.path}&%{request.query}";
docroot "/usr/lib/cgi-bin";
fastcgi "unix:/var/run/lighttpd/sockets/www-cgi.sock";
}
};
var.vhosts = var.vhosts + [
"git.lighttpd.net" => git,
"cgit.lighttpd.net" => git
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment