Setting up SSL for Forgejo on your homelab/vps using nginx reverse proxy

Goal: Setup Forgejo on my VPS and be able to use SSL with it

Issues

Solution

2 pieces of information were key:

  1. Having nginx proxy all SSL requests to the forgejo http server

  2. Finding the right nginx location directive to catch all /git/* requests correctly

Details

This assumes you have a newly installed Forgejo server running on your VPS at port 3000 and running under systemd. See Forgejo installation / admin docs for details.

Configure Nginx

Edit your Nginx config. I’m just using the default at:

$ vim /etc/nginx/sites-enabled/default

And add something like this inside your server block (preferably near the top so it’s the first match):

location ~/git(?.*)$ {

proxysetheader Host $host;

proxysetheader X-Real-IP $remote_addr;

proxysetheader X-Forwarded-For $remote_addr;

proxysetheader X-Forwarded-Proto: https;

proxysetheader X-Url-Scheme: https;

proxypass http://127.0.0.1:3000$subdir;

}

Explanation:

Restart Nginx

$ sudo systemctl restart nginx

Configure forgejo for the subdirectory

$ sudo vim /etc/forgejo/app.ini

And change ‘ROOT_URL’ to point to your subdirectory

ROOT_URL = https://smallweb.space/git/

Restart forgejo

$ sudo systemctl restart forgejo

The End

That’s it, you should now be able to navigate to

https:///git/

and see forgejo loaded with https://

Go ahead and make that admin user (you didn’t do that over http did you?)

Sources

[Server Fault](https://serverfault.com/questions/792326/nginx-proxy-pass-using-subfolder)

[Nginx Location Examples](https://www.thegeekstuff.com/2017/05/nginx-location-examples/)

[SO Question on SSL with Nginx Reverse Proxy](https://stackoverflow.com/questions/16042647/whats-the-de-facto-standard-for-a-reverse-proxy-to-tell-the-backend-ssl-is-used)