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

reverse-proxies.en-us.md 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ---
  2. date: "2018-05-22T11:00:00+00:00"
  3. title: "Usage: Reverse Proxies"
  4. slug: "reverse-proxies"
  5. weight: 17
  6. toc: true
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "usage"
  11. name: "Reverse Proxies"
  12. weight: 16
  13. identifier: "reverse-proxies"
  14. ---
  15. ## Using Nginx as a reverse proxy
  16. If you want Nginx to serve your Gitea instance, add the following `server` section to the `http` section of `nginx.conf`:
  17. ```
  18. server {
  19. listen 80;
  20. server_name git.example.com;
  21. location / {
  22. proxy_pass http://localhost:3000;
  23. }
  24. }
  25. ```
  26. ## Using Nginx with a sub-path as a reverse proxy
  27. 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`:
  28. ```
  29. server {
  30. listen 80;
  31. server_name git.example.com;
  32. location /git/ { # Note: Trailing slash
  33. proxy_pass http://localhost:3000/; # Note: Trailing slash
  34. }
  35. }
  36. ```
  37. Then set `[server] ROOT_URL = http://git.example.com/git/` in your configuration.
  38. ## Using Nginx as a reverse proxy and serve static resources directly
  39. We can tune the performance in splitting requests into categories static and dynamic.
  40. CSS files, JavaScript files, images and web fonts are static content.
  41. The front page, a repository view or issue list is dynamic content.
  42. Nginx can serve static resources directly and proxy only the dynamic requests to gitea.
  43. Nginx is optimized for serving static content, while the proxying of large responses might be the opposite of that
  44. (see https://serverfault.com/q/587386).
  45. Download a snapshot of the Gitea source repository to `/path/to/gitea/`.
  46. After this, run `make webpack` 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.
  47. (You will need to have [Node with npm](https://nodejs.org/en/download/) and `make` installed to generate the static resources)
  48. Depending on the scale of your user base, you might want to split the traffic to two distinct servers,
  49. or use a cdn for the static files.
  50. ### using a single node and a single domain
  51. Set `[server] STATIC_URL_PREFIX = /_/static` in your configuration.
  52. ```
  53. server {
  54. listen 80;
  55. server_name git.example.com;
  56. location /_/static {
  57. alias /path/to/gitea/public;
  58. }
  59. location / {
  60. proxy_pass http://localhost:3000;
  61. }
  62. }
  63. ```
  64. ### using two nodes and two domains
  65. Set `[server] STATIC_URL_PREFIX = http://cdn.example.com/gitea` in your configuration.
  66. ```
  67. # application server running gitea
  68. server {
  69. listen 80;
  70. server_name git.example.com;
  71. location / {
  72. proxy_pass http://localhost:3000;
  73. }
  74. }
  75. ```
  76. ```
  77. # static content delivery server
  78. server {
  79. listen 80;
  80. server_name cdn.example.com;
  81. location /gitea {
  82. alias /path/to/gitea/public;
  83. }
  84. location / {
  85. return 404;
  86. }
  87. }
  88. ```
  89. ## Using Apache HTTPD as a reverse proxy
  90. 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):
  91. ```
  92. <VirtualHost *:80>
  93. ...
  94. ProxyPreserveHost On
  95. ProxyRequests off
  96. AllowEncodedSlashes NoDecode
  97. ProxyPass / http://localhost:3000/ nocanon
  98. ProxyPassReverse / http://localhost:3000/
  99. </VirtualHost>
  100. ```
  101. Note: The following Apache HTTPD mods must be enabled: `proxy`, `proxy_http`
  102. 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.
  103. ## Using Apache HTTPD with a sub-path as a reverse proxy
  104. 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):
  105. ```
  106. <VirtualHost *:80>
  107. ...
  108. <Proxy *>
  109. Order allow,deny
  110. Allow from all
  111. </Proxy>
  112. AllowEncodedSlashes NoDecode
  113. # Note: no trailing slash after either /git or port
  114. ProxyPass /git http://localhost:3000 nocanon
  115. ProxyPassReverse /git http://localhost:3000
  116. </VirtualHost>
  117. ```
  118. Then set `[server] ROOT_URL = http://git.example.com/git/` in your configuration.
  119. Note: The following Apache HTTPD mods must be enabled: `proxy`, `proxy_http`
  120. ## Using Caddy as a reverse proxy
  121. If you want Caddy to serve your Gitea instance, you can add the following server block to your Caddyfile:
  122. ```
  123. git.example.com {
  124. proxy / http://localhost:3000
  125. }
  126. ```
  127. ## Using Caddy with a sub-path as a reverse proxy
  128. 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:
  129. ```
  130. git.example.com {
  131. proxy /git/ http://localhost:3000 # Note: Trailing Slash after /git/
  132. }
  133. ```
  134. Then set `[server] ROOT_URL = http://git.example.com/git/` in your configuration.
  135. ## Using IIS as a reverse proxy
  136. If you wish to run Gitea with IIS. You will need to setup IIS with URL Rewrite as reverse proxy.
  137. 1. Setup an empty website in IIS, named let's say, `Gitea Proxy`.
  138. 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:
  139. - 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)
  140. - Once the module is installed in IIS, you will see a new Icon in the IIS Administration Console called URL Rewrite.
  141. - 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.
  142. - 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.
  143. - 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`
  144. - Enable SSL Offloading
  145. - 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`
  146. - Now edit the `web.config` for your website to match the following: (changing `127.0.0.1:3000` and `git.example.com` as appropriate)
  147. ```xml
  148. <?xml version="1.0" encoding="UTF-8"?>
  149. <configuration>
  150. <system.webServer>
  151. <rewrite>
  152. <rules>
  153. <rule name="ReverseProxyInboundRule1" stopProcessing="true">
  154. <match url="(.*)" />
  155. <action type="Rewrite" url="http://127.0.0.1:3000/{R:1}" />
  156. <serverVariables>
  157. <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="HTTP_ACCEPT_ENCODING" />
  158. <set name="HTTP_ACCEPT_ENCODING" value="" />
  159. </serverVariables>
  160. </rule>
  161. </rules>
  162. <outboundRules>
  163. <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
  164. <!-- set the pattern correctly here - if you only want to accept http or https -->
  165. <!-- change the pattern and the action value as appropriate -->
  166. <match filterByTags="A, Form, Img" pattern="^http(s)?://127.0.0.1:3000/(.*)" />
  167. <action type="Rewrite" value="http{R:1}://git.example.com/{R:2}" />
  168. </rule>
  169. <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
  170. <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
  171. <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
  172. </rule>
  173. <preConditions>
  174. <preCondition name="ResponseIsHtml1">
  175. <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
  176. </preCondition>
  177. <preCondition name="NeedsRestoringAcceptEncoding">
  178. <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
  179. </preCondition>
  180. </preConditions>
  181. </outboundRules>
  182. </rewrite>
  183. <urlCompression doDynamicCompression="true" />
  184. </system.webServer>
  185. </configuration>
  186. ```