diff --git a/content/blog/make-cgit-serve-pdfs-directly/index.rst b/content/blog/make-cgit-serve-pdfs-directly/index.rst new file mode 100644 index 0000000..5da8dc3 --- /dev/null +++ b/content/blog/make-cgit-serve-pdfs-directly/index.rst @@ -0,0 +1,26 @@ +--- +title: "How to make cgit serve PDF files as direct downloads" +date: 2025-11-17T23:42:00+01:00 +summary: > + cgit is great, but by default when you click on a PDF file in a repository content listing it will show you a + hexdump of the file. You can access the actual file by clicking the "plain" link on top of the listing, but that's + not only annoying, for large PDF files rendering the hexdump can also hang browser tabs. +--- + +cgit is great, but by default when you click on a PDF file in a repository content listing it will show you a +hexdump of the file. You can access the actual file by clicking the "plain" link on top of the listing, but that's +not only annoying, for large PDF files rendering the hexdump can also hang browser tabs. + +I found a quick and easy solution to this problem, which I'm documenting here because it seems nobody on the +internet has really done this before, and the usual AI assistants (ChatGPT and Claude) are both deeply confused. + +You just add a simple rewrite rule to your nginx config that 302-redirects requests to ``/tree/.../foobar.pdf`` to +``/plain/.../foobar.pdf``. Here's the rule, make sure you put them in your nginx config *before* the location directive +proxying requests to cgit. + +.. code:: nginx + + location ~ ^/([^/]+)/tree/(.*\.pdf)$ { + return 302 /$1/plain/$2; + } +