reverse-proxy.mdx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ---
  2. title: "Reverse proxy"
  3. description: "Host-sharing your Git service with HTTPS"
  4. icon: "shield-halved"
  5. ---
  6. 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.
  7. <Note>
  8. 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.
  9. </Note>
  10. ## Caddy
  11. <Tabs>
  12. <Tab title="Standard">
  13. Add the following server block to your `Caddyfile` and reload:
  14. ```caddy
  15. gogs.example.com {
  16. proxy / http://localhost:3000
  17. }
  18. ```
  19. Set the matching external URL in `custom/conf/app.ini`:
  20. ```ini
  21. [server]
  22. EXTERNAL_URL = https://gogs.example.com/
  23. ```
  24. <Tip>
  25. Caddy automatically provisions TLS certificates via Let's Encrypt when you use a domain name.
  26. </Tip>
  27. </Tab>
  28. <Tab title="Subpath">
  29. To serve Gogs under a subpath, note the trailing `/`:
  30. ```caddy
  31. example.com {
  32. proxy /gogs/ http://localhost:3000
  33. }
  34. ```
  35. Set the matching external URL in `custom/conf/app.ini`:
  36. ```ini
  37. [server]
  38. EXTERNAL_URL = https://example.com/gogs/
  39. ```
  40. </Tab>
  41. </Tabs>
  42. ## NGINX
  43. <Tabs>
  44. <Tab title="Standard">
  45. 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:
  46. ```nginx
  47. server {
  48. listen 80;
  49. server_name gogs.example.com;
  50. location / {
  51. proxy_pass http://localhost:3000;
  52. }
  53. }
  54. ```
  55. Set the matching external URL in `custom/conf/app.ini`:
  56. ```ini
  57. [server]
  58. EXTERNAL_URL = http://gogs.example.com/
  59. ```
  60. </Tab>
  61. <Tab title="Subpath">
  62. To serve Gogs under a subpath, note the trailing `/` on both the `location` and `proxy_pass` directives:
  63. ```nginx
  64. server {
  65. listen 80;
  66. server_name example.com;
  67. location /gogs/ {
  68. proxy_pass http://localhost:3000/;
  69. }
  70. }
  71. ```
  72. Set the matching external URL in `custom/conf/app.ini`:
  73. ```ini
  74. [server]
  75. EXTERNAL_URL = http://example.com/gogs/
  76. ```
  77. </Tab>
  78. <Tab title="HTTPS">
  79. Install [Certbot](https://certbot.eff.org/) and obtain a [Let's Encrypt](https://letsencrypt.org/) certificate:
  80. ```bash
  81. sudo apt install certbot python3-certbot-nginx
  82. sudo certbot --nginx -d gogs.example.com
  83. ```
  84. Certbot will automatically modify your Nginx configuration to use HTTPS. Your Nginx server block will look similar to:
  85. ```nginx
  86. server {
  87. listen 443 ssl;
  88. server_name gogs.example.com;
  89. ssl_certificate /etc/letsencrypt/live/gogs.example.com/fullchain.pem;
  90. ssl_certificate_key /etc/letsencrypt/live/gogs.example.com/privkey.pem;
  91. location / {
  92. proxy_pass http://localhost:3000;
  93. proxy_set_header Host $host;
  94. proxy_set_header X-Real-IP $remote_addr;
  95. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  96. proxy_set_header X-Forwarded-Proto $scheme;
  97. }
  98. }
  99. server {
  100. listen 80;
  101. server_name gogs.example.com;
  102. return 301 https://$host$request_uri;
  103. }
  104. ```
  105. Set the matching external URL in `custom/conf/app.ini`:
  106. ```ini
  107. [server]
  108. EXTERNAL_URL = https://gogs.example.com/
  109. ```
  110. Certbot sets up automatic renewal via a cron job or systemd timer. Verify with:
  111. ```bash
  112. sudo certbot renew --dry-run
  113. ```
  114. </Tab>
  115. </Tabs>
  116. ### Large file uploads
  117. 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:
  118. ```nginx
  119. server {
  120. listen 80;
  121. server_name gogs.example.com;
  122. client_max_body_size 50m;
  123. location / {
  124. proxy_pass http://localhost:3000;
  125. }
  126. }
  127. ```
  128. <Tip>
  129. 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.
  130. </Tip>
  131. ## Apache 2
  132. <Tabs>
  133. <Tab title="Standard">
  134. Create or edit your virtual host configuration file (e.g. `/etc/apache2/vhost.d/gogs.conf`):
  135. ```apache
  136. <VirtualHost *:80>
  137. ServerName gogs.example.com
  138. ProxyPreserveHost On
  139. ProxyRequests off
  140. ProxyPass / http://127.0.0.1:3000
  141. ProxyPassReverse / http://127.0.0.1:3000
  142. </VirtualHost>
  143. ```
  144. Set the matching external URL in `custom/conf/app.ini`:
  145. ```ini
  146. [server]
  147. EXTERNAL_URL = http://gogs.example.com/
  148. ```
  149. </Tab>
  150. <Tab title="Subpath">
  151. To serve Gogs under a subpath, omit the trailing slash after the port number in the `ProxyPass` directives:
  152. ```apache
  153. <VirtualHost *:80>
  154. ServerName example.com
  155. <Proxy *>
  156. Order allow,deny
  157. Allow from all
  158. </Proxy>
  159. ProxyPass /gogs http://127.0.0.1:3000
  160. ProxyPassReverse /gogs http://127.0.0.1:3000
  161. </VirtualHost>
  162. ```
  163. Set the matching external URL in `custom/conf/app.ini`:
  164. ```ini
  165. [server]
  166. EXTERNAL_URL = http://example.com/gogs/
  167. ```
  168. </Tab>
  169. <Tab title="HTTPS">
  170. Enable the `ssl` module in addition to the proxy modules:
  171. ```bash
  172. sudo a2enmod proxy proxy_http ssl
  173. sudo systemctl restart apache2
  174. ```
  175. Apache virtual host configuration:
  176. ```apache
  177. <VirtualHost *:443>
  178. ServerName gogs.example.com
  179. SSLEngine on
  180. SSLCertificateFile /path/to/cert.pem
  181. SSLCertificateKeyFile /path/to/key.pem
  182. ProxyPreserveHost On
  183. ProxyRequests off
  184. ProxyPass / http://127.0.0.1:3000/
  185. ProxyPassReverse / http://127.0.0.1:3000/
  186. </VirtualHost>
  187. # Redirect HTTP to HTTPS
  188. <VirtualHost *:80>
  189. ServerName gogs.example.com
  190. Redirect permanent / https://gogs.example.com/
  191. </VirtualHost>
  192. ```
  193. Set the matching external URL in `custom/conf/app.ini`:
  194. ```ini
  195. [server]
  196. EXTERNAL_URL = https://gogs.example.com/
  197. ```
  198. </Tab>
  199. </Tabs>
  200. ## lighttpd
  201. <Tabs>
  202. <Tab title="Standard">
  203. Add the following to your lighttpd configuration:
  204. ```lighttpd
  205. server.modules += ( "mod_proxy" )
  206. $HTTP["host"] == "gogs.example.com" {
  207. proxy.server = ( "" => ( ( "host" => "127.0.0.1", "port" => "3000" ) ) )
  208. }
  209. ```
  210. Set the matching external URL in `custom/conf/app.ini`:
  211. ```ini
  212. [server]
  213. EXTERNAL_URL = http://gogs.example.com/
  214. ```
  215. </Tab>
  216. <Tab title="Subpath">
  217. To serve Gogs under a subpath, requires lighttpd **1.4.46 or later** for the `proxy.header` directive:
  218. ```lighttpd
  219. server.modules += ( "mod_proxy" )
  220. $HTTP["url"] =~ "^/gogs/" {
  221. proxy.server = ( "" => ( ( "host" => "localhost", "port" => "3000" ) ) )
  222. proxy.header = ( "map-urlpath" => ( "/gogs/" => "/" ) )
  223. }
  224. ```
  225. Set the matching external URL in `custom/conf/app.ini`:
  226. ```ini
  227. [server]
  228. EXTERNAL_URL = http://example.com/gogs/
  229. ```
  230. </Tab>
  231. </Tabs>
  232. ## IIS
  233. Create a new website in IIS and use the following `web.config` file.
  234. <Note>
  235. If you do not need HTTPS handled by IIS, remove the entire `RedirectToHttps` rule section from the configuration below.
  236. </Note>
  237. ```xml
  238. <?xml version="1.0" encoding="UTF-8"?>
  239. <configuration>
  240. <system.webServer>
  241. <rewrite>
  242. <rules>
  243. <rule name="RedirectToHttps" stopProcessing="true">
  244. <match url=".*" />
  245. <conditions>
  246. <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  247. </conditions>
  248. <action type="Redirect"
  249. url="https://{HTTP_HOST}{REQUEST_URI}"
  250. redirectType="Permanent"
  251. appendQueryString="false" />
  252. </rule>
  253. <rule name="ReverseProxyInboundRule" stopProcessing="true">
  254. <match url="(.*)" />
  255. <action type="Rewrite"
  256. url="http://localhost:3000/{R:1}" />
  257. </rule>
  258. </rules>
  259. <outboundRules>
  260. <rule name="ReverseProxyOutboundRule"
  261. preCondition="ResponseIsHtml">
  262. <match filterByTags="A, Form, Img"
  263. pattern="^http(s)?://localhost:3000/(.*)" />
  264. <action type="Rewrite"
  265. value="http{R:1}://gogs.example.com/{R:2}" />
  266. </rule>
  267. <preConditions>
  268. <preCondition name="ResponseIsHtml">
  269. <add input="{RESPONSE_CONTENT_TYPE}"
  270. pattern="^text/html" />
  271. </preCondition>
  272. </preConditions>
  273. </outboundRules>
  274. </rewrite>
  275. </system.webServer>
  276. </configuration>
  277. ```
  278. Then set the matching external URL in `custom/conf/app.ini`:
  279. ```ini
  280. [server]
  281. EXTERNAL_URL = https://gogs.example.com/
  282. ```
  283. ## Native HTTPS
  284. If you are not using a reverse proxy, Gogs can serve HTTPS directly. Update the `[server]` section of `custom/conf/app.ini`:
  285. ```ini
  286. [server]
  287. PROTOCOL = https
  288. EXTERNAL_URL = https://gogs.example.com/
  289. CERT_FILE = custom/https/cert.pem
  290. KEY_FILE = custom/https/key.pem
  291. ```
  292. | Option | Description | Default |
  293. |--------|-------------|---------|
  294. | `PROTOCOL` | Set to `https` to enable native TLS. | `http` |
  295. | `CERT_FILE` | Path to the TLS certificate file (PEM format). | `custom/https/cert.pem` |
  296. | `KEY_FILE` | Path to the TLS private key file (PEM format). | `custom/https/key.pem` |
  297. | `TLS_MIN_VERSION` | Minimum TLS version. Options: `TLS10`, `TLS11`, `TLS12`, `TLS13`. | `TLS12` |