CGit, Nginx and Arch Linux

May 6, 2012

Here’s a quick reminder to get CGit and Nginx running together on an Arch Linux server. I assume you have already Nginx up and running.

First, we install the require packages.

pacman -S fcgiwrap cgit

The default configuration values for fcgiwrap should be fine, but you can check anyway in /etc/conf.d/fcgiwrap.

Here the Nginx configuration for the CGit part. EDIT: check the update code at the end of the post.

server {
    listen       80;
    server_name  git.nicosphere.net;
    index cgit.cgi;
    gzip off;

    location / {
        root /usr/share/webapps/cgit/;
    }

    if (!-f $request_filename) {
        rewrite ^/([^?/]+/[^?]*)?(?:\?(.*))?$ /cgit.cgi?url=$1&$2 last;
    }
                                                }

    location ~ \.cgi$ {
        gzip off;
        include fastcgi_params;
        fastcgi_pass    127.0.0.1:9001;
        fastcgi_index   cgit.cgi;
        fastcgi_param   SCRIPT_FILENAME /usr/share/webapps/cgit/cgit.cgi;
        fastcgi_param   DOCUMENT_ROOT /usr/share/webapps/cgit/;

    }
}

Obviously, make sure the server_name is setup right. The SCRIPT_FILENAME and DOCUMENT_ROOT are those used with the official Arch Linux package, it might be different if you retrieved your own tarball.

Now, let’s get all this running.

rc.d start fcgiwrap && rc.d restart nginx

Don’t forget to add the fcgiwrap daemon to the corresponding rc.conf line, to get it working when if you restart the server.

Once it’s running, you might setup the /etc/cgitrc file as usual. You certainly want add at least the CSS file as follow:

css=/cgit.css

EDIT: As I wanted a basic authentication, with an exception for the /pub/ directiory, and couldn’t find the correct answer, I asked to StackOverflow. The answer works fine for me, and even better that the previous code. For the record, here the final nginx configuration:

server {
    listen       80;
    server_name  git.nicosphere.net;
    index cgit.cgi;
    gzip off;

    root /usr/share/webapps/cgit;

    # $document_root is now set properly, and you don't need to override it
    include fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME $document_root/cgit.cgi;

    location / {
        try_files $uri @cgit;
    }

    # Require auth for requests sent to cgit that originated in location /    
    location @cgit {
        auth_basic "Restricted";
        auth_basic_user_file /srv/gitosis/.htpasswd;

        gzip off;
        # rewrites in nginx don't match the query string
        rewrite ^/([^/]+/.*)?$ /cgit.cgi?url=$1 break;
        fastcgi_pass    127.0.0.1:9001;
    }

    location ^~ /pub/  {
        gzip off;
        rewrite ^/([^/]+/.*)?$ /cgit.cgi?url=$1 break;
        fastcgi_pass    127.0.0.1:9001;
    }
}
Tweet

Related-ish Posts

About

I'm Nicolas Paris, aka Nic0, I like to share about programming and Linux tricks. Follow me on Twitter, where the content is pretty much like here, mainly programming stuff. Or visite my website