+++ date = "2019-07-31T22:37:06+00:00" publishdate = "2023-12-29T07:08:55+00:00" title = "Gitea in a Subdirectory with Nginx" slug = "gitea-in-a-sub-directory-with-nginx" author = "Thedro" tags = ["nginx","git"] type = "posts" summary = "Sometimes we want to expose a web application quickly without provisioning a new subdomain or SSL certificate." draft = "" syntax = "1" toc = "" updated = "2022-03-02" +++ {{< image source="/images/gitea-in-a-sub-directory-with-nginx.png" title="Gitea's Home Page" >}} Gitea's Home Page {{< /image >}} Running web applications from the subdirectory of a `URL` has its benefits. There doesn't seem to be much talk about this {{< sidenote mark="technique." set="right" >}}A series of posts about running my favorite open source applications from a `URL` subdirectory sounds like a good idea.{{< /sidenote >}} It's useful when you need to expose a web application quickly without provisioning a new subdomain and `SSL` certificate. For example let's run [Gitea](https://gitea.io/en-us/) from the subdirectory of a domain using [`nginx`](http://nginx.org/en/docs/) We will make Gitea accessible from a `URL` of the form `https://www.example.com/gitea` or `https://www.example.com/git`. To accomplish this, reverse proxy using a separate location block in the `nginx` {{< sidenote mark="configuration." set="right" >}}We are connecting over a `unix` socket to access the Gitea instance. The forward slashes at the end of the `proxy_pass` are important and match the location block.{{< /sidenote >}} ```nginx {options="hl_lines=2"} location /gitea/ { proxy_pass http://unix:/opt/gitea/var/lib/gitea/gitea.socket:/; } ``` Then in Gitea's `app.ini` configuration, adjust the server block to match the location block specified {{< sidenote mark="above." set="left" >}}Be sure that [ broad caching and deny directives](https://www.digitalocean.com/community/tools/nginx) are not applied to the reverse proxy location block or you might end up with strange `404s` and exotic behavior.{{< /sidenote >}} ```ini [server] PROTOCOL = unix SSH_DOMAIN = example.com DOMAIN = http://www.example.com/gitea/ ROOT_URL = http://www.example.com/gitea/ HTTP_ADDR = /opt/gitea/var/lib/gitea/gitea.socket ``` In [future versions of Gitea](https://github.com/go-gitea/gitea/commit/f49d160447899270fbca6370cb7ab2742dce85dc), the `unix` protocol value is deprecated in favour of `http+unix`. ```ini {options="hl_lines=2"} [server] PROTOCOL = http+unix SSH_DOMAIN = example.com DOMAIN = http://www.example.com/gitea/ ROOT_URL = http://www.example.com/gitea/ HTTP_ADDR = /opt/gitea/var/lib/gitea/gitea.socket ``` A user can access the full Gitea instance from the `gitea/` subdirectory of the `URL`. All applications are not created equal and some blow up badly in a subdirectory. Gitea supports this natively from the configuration. Some applications require elaborate tricks, workarounds, or source code changes that are probably not worth the effort.