You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reverse-proxies.en-us.md 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. ---
  2. date: "2018-05-22T11:00:00+00:00"
  3. title: "Usage: Reverse Proxies"
  4. slug: "reverse-proxies"
  5. weight: 17
  6. toc: false
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "usage"
  11. name: "Reverse Proxies"
  12. weight: 16
  13. identifier: "reverse-proxies"
  14. ---
  15. # Reverse Proxies
  16. **Table of Contents**
  17. {{< toc >}}
  18. ## Nginx
  19. If you want Nginx to serve your Gitea instance, add the following `server` section to the `http` section of `nginx.conf`:
  20. ```apacheconf
  21. server {
  22. listen 80;
  23. server_name git.example.com;
  24. location / {
  25. proxy_pass http://localhost:3000;
  26. proxy_set_header Host $host;
  27. proxy_set_header X-Real-IP $remote_addr;
  28. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  29. proxy_set_header X-Forwarded-Proto $scheme;
  30. }
  31. }
  32. ```
  33. ## Nginx with a sub-path
  34. In case you already have a site, and you want Gitea to share the domain name, you can setup Nginx to serve Gitea under a sub-path by adding the following `server` section inside the `http` section of `nginx.conf`:
  35. ```apacheconf
  36. server {
  37. listen 80;
  38. server_name git.example.com;
  39. # Note: Trailing slash
  40. location /git/ {
  41. # Note: Trailing slash
  42. proxy_pass http://localhost:3000/;
  43. proxy_set_header Host $host;
  44. proxy_set_header X-Real-IP $remote_addr;
  45. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  46. proxy_set_header X-Forwarded-Proto $scheme;
  47. }
  48. }
  49. ```
  50. Then you **MUST** set something like `[server] ROOT_URL = http://git.example.com/git/` correctly in your configuration.
  51. ## Nginx and serve static resources directly
  52. We can tune the performance in splitting requests into categories static and dynamic.
  53. CSS files, JavaScript files, images and web fonts are static content.
  54. The front page, a repository view or issue list is dynamic content.
  55. Nginx can serve static resources directly and proxy only the dynamic requests to Gitea.
  56. Nginx is optimized for serving static content, while the proxying of large responses might be the opposite of that
  57. (see [https://serverfault.com/q/587386](https://serverfault.com/q/587386)).
  58. Download a snapshot of the Gitea source repository to `/path/to/gitea/`.
  59. After this, run `make frontend` in the repository directory to generate the static resources. We are only interested in the `public/` directory for this task, so you can delete the rest.
  60. (You will need to have [Node with npm](https://nodejs.org/en/download/) and `make` installed to generate the static resources)
  61. Depending on the scale of your user base, you might want to split the traffic to two distinct servers,
  62. or use a cdn for the static files.
  63. ### Single node and single domain
  64. Set `[server] STATIC_URL_PREFIX = /_/static` in your configuration.
  65. ```apacheconf
  66. server {
  67. listen 80;
  68. server_name git.example.com;
  69. location /_/static/assets/ {
  70. alias /path/to/gitea/public/;
  71. }
  72. location / {
  73. proxy_pass http://localhost:3000;
  74. }
  75. }
  76. ```
  77. ### Two nodes and two domains
  78. Set `[server] STATIC_URL_PREFIX = http://cdn.example.com/gitea` in your configuration.
  79. ```apacheconf
  80. # application server running Gitea
  81. server {
  82. listen 80;
  83. server_name git.example.com;
  84. location / {
  85. proxy_pass http://localhost:3000;
  86. }
  87. }
  88. ```
  89. ```apacheconf
  90. # static content delivery server
  91. server {
  92. listen 80;
  93. server_name cdn.example.com;
  94. location /gitea/ {
  95. alias /path/to/gitea/public/;
  96. }
  97. location / {
  98. return 404;
  99. }
  100. }
  101. ```
  102. ## Resolving Error: 413 Request Entity Too Large
  103. This error indicates nginx is configured to restrict the file upload size.
  104. In your nginx config file containing your Gitea proxy directive, find the `location { ... }` block for Gitea and add the line
  105. `client_max_body_size 16M;` to set this limit to 16 megabytes or any other number of choice.
  106. If you use Git LFS, this will also limit the size of the largest file you will be able to push.
  107. ## Apache HTTPD
  108. If you want Apache HTTPD to serve your Gitea instance, you can add the following to your Apache HTTPD configuration (usually located at `/etc/apache2/httpd.conf` in Ubuntu):
  109. ```apacheconf
  110. <VirtualHost *:80>
  111. ...
  112. ProxyPreserveHost On
  113. ProxyRequests off
  114. AllowEncodedSlashes NoDecode
  115. ProxyPass / http://localhost:3000/ nocanon
  116. </VirtualHost>
  117. ```
  118. Note: The following Apache HTTPD mods must be enabled: `proxy`, `proxy_http`.
  119. If you wish to use Let's Encrypt with webroot validation, add the line `ProxyPass /.well-known !` before `ProxyPass` to disable proxying these requests to Gitea.
  120. ## Apache HTTPD with a sub-path
  121. In case you already have a site, and you want Gitea to share the domain name, you can setup Apache HTTPD to serve Gitea under a sub-path by adding the following to you Apache HTTPD configuration (usually located at `/etc/apache2/httpd.conf` in Ubuntu):
  122. ```apacheconf
  123. <VirtualHost *:80>
  124. ...
  125. <Proxy *>
  126. Order allow,deny
  127. Allow from all
  128. </Proxy>
  129. AllowEncodedSlashes NoDecode
  130. # Note: no trailing slash after either /git or port
  131. ProxyPass /git http://localhost:3000 nocanon
  132. </VirtualHost>
  133. ```
  134. Then you **MUST** set something like `[server] ROOT_URL = http://git.example.com/git/` correctly in your configuration.
  135. Note: The following Apache HTTPD mods must be enabled: `proxy`, `proxy_http`.
  136. ## Caddy
  137. If you want Caddy to serve your Gitea instance, you can add the following server block to your Caddyfile:
  138. ```apacheconf
  139. git.example.com {
  140. reverse_proxy localhost:3000
  141. }
  142. ```
  143. If you still use Caddy v1, use:
  144. ```apacheconf
  145. git.example.com {
  146. proxy / localhost:3000
  147. }
  148. ```
  149. ## Caddy with a sub-path
  150. In case you already have a site, and you want Gitea to share the domain name, you can setup Caddy to serve Gitea under a sub-path by adding the following to your server block in your Caddyfile:
  151. ```apacheconf
  152. git.example.com {
  153. route /git/* {
  154. uri strip_prefix /git
  155. reverse_proxy localhost:3000
  156. }
  157. }
  158. ```
  159. Or, for Caddy v1:
  160. ```apacheconf
  161. git.example.com {
  162. proxy /git/ localhost:3000
  163. }
  164. ```
  165. Then set `[server] ROOT_URL = http://git.example.com/git/` in your configuration.
  166. ## IIS
  167. If you wish to run Gitea with IIS. You will need to setup IIS with URL Rewrite as reverse proxy.
  168. 1. Setup an empty website in IIS, named let's say, `Gitea Proxy`.
  169. 2. Follow the first two steps in [Microsoft's Technical Community Guide to Setup IIS with URL Rewrite](https://techcommunity.microsoft.com/t5/iis-support-blog/setup-iis-with-url-rewrite-as-a-reverse-proxy-for-real-world/ba-p/846222#M343). That is:
  170. - Install Application Request Routing (ARR for short) either by using the Microsoft Web Platform Installer 5.1 (WebPI) or downloading the extension from [IIS.net](https://www.iis.net/downloads/microsoft/application-request-routing)
  171. - Once the module is installed in IIS, you will see a new Icon in the IIS Administration Console called URL Rewrite.
  172. - Open the IIS Manager Console and click on the `Gitea Proxy` Website from the tree view on the left. Select and double click the URL Rewrite Icon from the middle pane to load the URL Rewrite interface.
  173. - Choose the `Add Rule` action from the right pane of the management console and select the `Reverse Proxy Rule` from the `Inbound and Outbound Rules` category.
  174. - In the Inbound Rules section, set the server name to be the host that Gitea is running on with its port. e.g. if you are running Gitea on the localhost with port 3000, the following should work: `127.0.0.1:3000`
  175. - Enable SSL Offloading
  176. - In the Outbound Rules, ensure `Rewrite the domain names of the links in HTTP response` is set and set the `From:` field as above and the `To:` to your external hostname, say: `git.example.com`
  177. - Now edit the `web.config` for your website to match the following: (changing `127.0.0.1:3000` and `git.example.com` as appropriate)
  178. ```xml
  179. <?xml version="1.0" encoding="UTF-8"?>
  180. <configuration>
  181. <system.web>
  182. <httpRuntime requestPathInvalidCharacters="" />
  183. </system.web>
  184. <system.webServer>
  185. <security>
  186. <requestFiltering>
  187. <hiddenSegments>
  188. <clear />
  189. </hiddenSegments>
  190. <denyUrlSequences>
  191. <clear />
  192. </denyUrlSequences>
  193. <fileExtensions allowUnlisted="true">
  194. <clear />
  195. </fileExtensions>
  196. </requestFiltering>
  197. </security>
  198. <rewrite>
  199. <rules useOriginalURLEncoding="false">
  200. <rule name="ReverseProxyInboundRule1" stopProcessing="true">
  201. <match url="(.*)" />
  202. <action type="Rewrite" url="http://127.0.0.1:3000{UNENCODED_URL}" />
  203. <serverVariables>
  204. <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="HTTP_ACCEPT_ENCODING" />
  205. <set name="HTTP_ACCEPT_ENCODING" value="" />
  206. </serverVariables>
  207. </rule>
  208. </rules>
  209. <outboundRules>
  210. <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
  211. <!-- set the pattern correctly here - if you only want to accept http or https -->
  212. <!-- change the pattern and the action value as appropriate -->
  213. <match filterByTags="A, Form, Img" pattern="^http(s)?://127.0.0.1:3000/(.*)" />
  214. <action type="Rewrite" value="http{R:1}://git.example.com/{R:2}" />
  215. </rule>
  216. <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
  217. <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
  218. <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
  219. </rule>
  220. <preConditions>
  221. <preCondition name="ResponseIsHtml1">
  222. <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
  223. </preCondition>
  224. <preCondition name="NeedsRestoringAcceptEncoding">
  225. <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
  226. </preCondition>
  227. </preConditions>
  228. </outboundRules>
  229. </rewrite>
  230. <urlCompression doDynamicCompression="true" />
  231. <handlers>
  232. <clear />
  233. <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
  234. </handlers>
  235. <!-- Map all extensions to the same MIME type, so all files can be
  236. downloaded. -->
  237. <staticContent>
  238. <clear />
  239. <mimeMap fileExtension="*" mimeType="application/octet-stream" />
  240. </staticContent>
  241. </system.webServer>
  242. </configuration>
  243. ```
  244. ## HAProxy
  245. If you want HAProxy to serve your Gitea instance, you can add the following to your HAProxy configuration
  246. add an acl in the frontend section to redirect calls to gitea.example.com to the correct backend
  247. ```
  248. frontend http-in
  249. ...
  250. acl acl_gitea hdr(host) -i gitea.example.com
  251. use_backend gitea if acl_gitea
  252. ...
  253. ```
  254. add the previously defined backend section
  255. ```
  256. backend gitea
  257. server localhost:3000 check
  258. ```
  259. If you redirect the http content to https, the configuration work the same way, just remember that the connection between HAProxy and Gitea will be done via http so you do not have to enable https in Gitea's configuration.
  260. ## HAProxy with a sub-path
  261. In case you already have a site, and you want Gitea to share the domain name, you can setup HAProxy to serve Gitea under a sub-path by adding the following to you HAProxy configuration:
  262. ```
  263. frontend http-in
  264. ...
  265. acl acl_gitea path_beg /gitea
  266. use_backend gitea if acl_gitea
  267. ...
  268. ```
  269. With that configuration http://example.com/gitea/ will redirect to your Gitea instance.
  270. then for the backend section
  271. ```
  272. backend gitea
  273. http-request replace-path /gitea\/?(.*) \/\1
  274. server localhost:3000 check
  275. ```
  276. The added http-request will automatically add a trailing slash if needed and internally remove /gitea from the path to allow it to work correctly with Gitea by setting properly http://example.com/gitea as the root.
  277. Then you **MUST** set something like `[server] ROOT_URL = http://example.com/gitea/` correctly in your configuration.
  278. ## Traefik
  279. If you want traefik to serve your Gitea instance, you can add the following label section to your `docker-compose.yaml` (Assuming the provider is docker).
  280. ```yaml
  281. gitea:
  282. image: gitea/gitea
  283. ...
  284. labels:
  285. - "traefik.enable=true"
  286. - "traefik.http.routers.gitea.rule=Host(`example.com`)"
  287. - "traefik.http.services.gitea-websecure.loadbalancer.server.port=3000"
  288. ```
  289. This config assumes that you are handling HTTPS on the traefik side and using HTTP between Gitea and traefik.