| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- ---
- title: "Reverse proxy"
- description: "Host-sharing your Git service with HTTPS"
- icon: "shield-halved"
- ---
- Running Gogs behind a reverse proxy allows you to serve it on standard ports (80/443) with a clean and nice URL in the browser address bar, add TLS termination, and integrate with existing web server infrastructure.
- <Note>
- Make sure the `EXTERNAL_URL` in your `custom/conf/app.ini` matches the actual URL users will access. When using a reverse proxy for TLS termination, keep `PROTOCOL = http` in Gogs and set `EXTERNAL_URL` to `https://`. The reverse proxy handles the encryption, and Gogs communicates with it over plain HTTP on the local network.
- </Note>
- ## Caddy
- <Tabs>
- <Tab title="Standard">
- Add the following server block to your `Caddyfile` and reload:
- ```caddy
- gogs.example.com {
- proxy / http://localhost:3000
- }
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = https://gogs.example.com/
- ```
- <Tip>
- Caddy automatically provisions TLS certificates via Let's Encrypt when you use a domain name.
- </Tip>
- </Tab>
- <Tab title="Subpath">
- To serve Gogs under a subpath, note the trailing `/`:
- ```caddy
- example.com {
- proxy /gogs/ http://localhost:3000
- }
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = https://example.com/gogs/
- ```
- </Tab>
- </Tabs>
- ## NGINX
- <Tabs>
- <Tab title="Standard">
- Add the following `server` block inside the `http` section of your `nginx.conf` (or in a file under `sites-available`), then reload the NGINX configuration:
- ```nginx
- server {
- listen 80;
- server_name gogs.example.com;
- location / {
- proxy_pass http://localhost:3000;
- }
- }
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = http://gogs.example.com/
- ```
- </Tab>
- <Tab title="Subpath">
- To serve Gogs under a subpath, note the trailing `/` on both the `location` and `proxy_pass` directives:
- ```nginx
- server {
- listen 80;
- server_name example.com;
- location /gogs/ {
- proxy_pass http://localhost:3000/;
- }
- }
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = http://example.com/gogs/
- ```
- </Tab>
- <Tab title="HTTPS">
- Install [Certbot](https://certbot.eff.org/) and obtain a [Let's Encrypt](https://letsencrypt.org/) certificate:
- ```bash
- sudo apt install certbot python3-certbot-nginx
- sudo certbot --nginx -d gogs.example.com
- ```
- Certbot will automatically modify your Nginx configuration to use HTTPS. Your Nginx server block will look similar to:
- ```nginx
- server {
- listen 443 ssl;
- server_name gogs.example.com;
- ssl_certificate /etc/letsencrypt/live/gogs.example.com/fullchain.pem;
- ssl_certificate_key /etc/letsencrypt/live/gogs.example.com/privkey.pem;
- location / {
- proxy_pass http://localhost:3000;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- }
- server {
- listen 80;
- server_name gogs.example.com;
- return 301 https://$host$request_uri;
- }
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = https://gogs.example.com/
- ```
- Certbot sets up automatic renewal via a cron job or systemd timer. Verify with:
- ```bash
- sudo certbot renew --dry-run
- ```
- </Tab>
- </Tabs>
- ### Large file uploads
- If you encounter HTTP `413 Request Entity Too Large` errors when pushing large files through NGINX, add `client_max_body_size` to your server block:
- ```nginx
- server {
- listen 80;
- server_name gogs.example.com;
- client_max_body_size 50m;
- location / {
- proxy_pass http://localhost:3000;
- }
- }
- ```
- <Tip>
- Adjust the `client_max_body_size` value to match or exceed the maximum file size you expect users to push. The default NGINX limit is only 1 MB.
- </Tip>
- ## Apache 2
- <Tabs>
- <Tab title="Standard">
- Create or edit your virtual host configuration file (e.g. `/etc/apache2/vhost.d/gogs.conf`):
- ```apache
- <VirtualHost *:80>
- ServerName gogs.example.com
- ProxyPreserveHost On
- ProxyRequests off
- ProxyPass / http://127.0.0.1:3000
- ProxyPassReverse / http://127.0.0.1:3000
- </VirtualHost>
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = http://gogs.example.com/
- ```
- </Tab>
- <Tab title="Subpath">
- To serve Gogs under a subpath, omit the trailing slash after the port number in the `ProxyPass` directives:
- ```apache
- <VirtualHost *:80>
- ServerName example.com
- <Proxy *>
- Order allow,deny
- Allow from all
- </Proxy>
- ProxyPass /gogs http://127.0.0.1:3000
- ProxyPassReverse /gogs http://127.0.0.1:3000
- </VirtualHost>
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = http://example.com/gogs/
- ```
- </Tab>
- <Tab title="HTTPS">
- Enable the `ssl` module in addition to the proxy modules:
- ```bash
- sudo a2enmod proxy proxy_http ssl
- sudo systemctl restart apache2
- ```
- Apache virtual host configuration:
- ```apache
- <VirtualHost *:443>
- ServerName gogs.example.com
- SSLEngine on
- SSLCertificateFile /path/to/cert.pem
- SSLCertificateKeyFile /path/to/key.pem
- ProxyPreserveHost On
- ProxyRequests off
- ProxyPass / http://127.0.0.1:3000/
- ProxyPassReverse / http://127.0.0.1:3000/
- </VirtualHost>
- # Redirect HTTP to HTTPS
- <VirtualHost *:80>
- ServerName gogs.example.com
- Redirect permanent / https://gogs.example.com/
- </VirtualHost>
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = https://gogs.example.com/
- ```
- </Tab>
- </Tabs>
- ## lighttpd
- <Tabs>
- <Tab title="Standard">
- Add the following to your lighttpd configuration:
- ```lighttpd
- server.modules += ( "mod_proxy" )
- $HTTP["host"] == "gogs.example.com" {
- proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "3000" ) ) )
- }
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = http://gogs.example.com/
- ```
- </Tab>
- <Tab title="Subpath">
- To serve Gogs under a subpath, requires lighttpd **1.4.46 or later** for the `proxy.header` directive:
- ```lighttpd
- server.modules += ( "mod_proxy" )
- $HTTP["url"] =~ "^/gogs/" {
- proxy.server = ( "" => ( ( "host" => "localhost", "port" => "3000" ) ) )
- proxy.header = ( "map-urlpath" => ( "/gogs/" => "/" ) )
- }
- ```
- Set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = http://example.com/gogs/
- ```
- </Tab>
- </Tabs>
- ## IIS
- Create a new website in IIS and use the following `web.config` file.
- <Note>
- If you do not need HTTPS handled by IIS, remove the entire `RedirectToHttps` rule section from the configuration below.
- </Note>
- ```xml
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <system.webServer>
- <rewrite>
- <rules>
- <rule name="RedirectToHttps" stopProcessing="true">
- <match url=".*" />
- <conditions>
- <add input="{HTTPS}" pattern="off" ignoreCase="true" />
- </conditions>
- <action type="Redirect"
- url="https://{HTTP_HOST}{REQUEST_URI}"
- redirectType="Permanent"
- appendQueryString="false" />
- </rule>
- <rule name="ReverseProxyInboundRule" stopProcessing="true">
- <match url="(.*)" />
- <action type="Rewrite"
- url="http://localhost:3000/{R:1}" />
- </rule>
- </rules>
- <outboundRules>
- <rule name="ReverseProxyOutboundRule"
- preCondition="ResponseIsHtml">
- <match filterByTags="A, Form, Img"
- pattern="^http(s)?://localhost:3000/(.*)" />
- <action type="Rewrite"
- value="http{R:1}://gogs.example.com/{R:2}" />
- </rule>
- <preConditions>
- <preCondition name="ResponseIsHtml">
- <add input="{RESPONSE_CONTENT_TYPE}"
- pattern="^text/html" />
- </preCondition>
- </preConditions>
- </outboundRules>
- </rewrite>
- </system.webServer>
- </configuration>
- ```
- Then set the matching external URL in `custom/conf/app.ini`:
- ```ini
- [server]
- EXTERNAL_URL = https://gogs.example.com/
- ```
- ## Native HTTPS
- If you are not using a reverse proxy, Gogs can serve HTTPS directly. Update the `[server]` section of `custom/conf/app.ini`:
- ```ini
- [server]
- PROTOCOL = https
- EXTERNAL_URL = https://gogs.example.com/
- CERT_FILE = custom/https/cert.pem
- KEY_FILE = custom/https/key.pem
- ```
- | Option | Description | Default |
- |--------|-------------|---------|
- | `PROTOCOL` | Set to `https` to enable native TLS. | `http` |
- | `CERT_FILE` | Path to the TLS certificate file (PEM format). | `custom/https/cert.pem` |
- | `KEY_FILE` | Path to the TLS private key file (PEM format). | `custom/https/key.pem` |
- | `TLS_MIN_VERSION` | Minimum TLS version. Options: `TLS10`, `TLS11`, `TLS12`, `TLS13`. | `TLS12` |
|