Skip to content

Commit c9f0ac7

Browse files
committed
Merge branch 'main' into feature/issue_23628
2 parents 02d1e01 + a519aac commit c9f0ac7

File tree

21 files changed

+487
-299
lines changed

21 files changed

+487
-299
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
date: "2023-04-09T11:00:00+02:00"
3+
title: "使用: HTTPS配置"
4+
slug: "https-setup"
5+
weight: 12
6+
toc: false
7+
draft: false
8+
menu:
9+
sidebar:
10+
parent: "administration"
11+
name: "HTTPS setup"
12+
weight: 12
13+
identifier: "https-setup"
14+
---
15+
16+
# HTTPS setup to encrypt connections to Gitea
17+
18+
**Table of Contents**
19+
20+
{{< toc >}}
21+
22+
## 使用内置服务器
23+
24+
在启用HTTPS之前,确保您拥有有效的SSL/TLS证书。
25+
建议在测试和评估情况下使用自签名证书,请运行 `gitea cert --host [HOST]` 以生成自签名证书
26+
27+
如果您在服务器上使用阿帕奇(Apache)或Nginx,建议参考 [反向代理指南]({{< relref "doc/administration/reverse-proxies.zh-cn.md" >}})。
28+
29+
要使用Gitea内置HTTPS支持,您必须编辑`app.ini`文件。
30+
31+
```ini
32+
[server]
33+
PROTOCOL = https
34+
ROOT_URL = https://git.example.com:3000/
35+
HTTP_PORT = 3000
36+
CERT_FILE = cert.pem
37+
KEY_FILE = key.pem
38+
```
39+
40+
请注意,如果您的证书由第三方证书颁发机构签名(即不是自签名的),则 cert.pem 应包含证书链。服务器证书必须是 cert.pem 中的第一个条目,后跟中介(如果有)。不必包含根证书,因为连接客户端必须已经拥有根证书才能建立信任关系。要了解有关配置值的更多信息,请查看 [配置备忘单](../config-cheat-sheet#server-server)
41+
42+
对于“CERT_FILE”或“KEY_FILE”字段,当文件路径是相对路径时,文件路径相对于“GITEA_CUSTOM”环境变量。它也可以是绝对路径。
43+
44+
### 设置HTTP重定向
45+
46+
Gitea服务器仅支持监听一个端口;要重定向HTTP请求致HTTPS端口,您需要启用HTTP重定向服务:
47+
48+
```ini
49+
[server]
50+
REDIRECT_OTHER_PORT = true
51+
; Port the redirection service should listen on
52+
PORT_TO_REDIRECT = 3080
53+
```
54+
55+
如果您使用Docker,确保端口已配置在 `docker-compose.yml` 文件
56+
57+
## 使用 ACME (默认: Let's Encrypt)
58+
59+
[ACME]https://tools.ietf.org/html/rfc8555) 是一种证书颁发机构标准协议,允许您自动请求和续订 SSL/TLS 证书。[Let`s Encrypt]https://letsencrypt.org/) 是使用此标准的免费公开信任的证书颁发机构服务器。仅实施“HTTP-01”和“TLS-ALPN-01”挑战。为了使 ACME 质询通过并验证您的域所有权,“80”端口(“HTTP-01”)或“443”端口(“TLS-ALPN-01”)上 gitea 域的外部流量必须由 gitea 实例提供服务。可能需要设置 [HTTP 重定向](#setting-up-http-redirection) 和端口转发才能正确路由外部流量。否则,到端口“80”的正常流量将自动重定向到 HTTPS。**您必须同意**ACME提供商的服务条款(默认为Let's Encrypt的[服务条款]https://letsencrypt.org/documents/LE-SA-v1.2-2017年11月15日.pdf))。
60+
61+
Minimum setup using the default Let's Encrypt:
62+
63+
```ini
64+
[server]
65+
PROTOCOL=https
66+
DOMAIN=git.example.com
67+
ENABLE_ACME=true
68+
ACME_ACCEPTTOS=true
69+
ACME_DIRECTORY=https
70+
;; Email can be omitted here and provided manually at first run, after which it is cached
71+
ACME_EMAIL=email@example.com
72+
```
73+
74+
小型配置请使用 [smallstep CA](https://github.com/smallstep/certificates), 点击 [教程](https://smallstep.com/docs/tutorials/acme-challenge) 了解更多信息。
75+
76+
```ini
77+
[server]
78+
PROTOCOL=https
79+
DOMAIN=git.example.com
80+
ENABLE_ACME=true
81+
ACME_ACCEPTTOS=true
82+
ACME_URL=https://ca.example.com/acme/acme/directory
83+
;; Can be omitted if using the system's trust is preferred
84+
;ACME_CA_ROOT=/path/to/root_ca.crt
85+
ACME_DIRECTORY=https
86+
ACME_EMAIL=email@example.com
87+
```
88+
89+
要了解关于配置, 请访问 [配置备忘单](../config-cheat-sheet#server-server)获取更多信息
90+
91+
## Using a reverse proxy
92+
93+
Setup up your reverse proxy as shown in the [reverse proxy guide](../reverse-proxies).
94+
95+
After that, enable HTTPS by following one of these guides:
96+
97+
- [nginx](https://nginx.org/en/docs/http/configuring_https_servers.html)
98+
- [apache2/httpd](https://httpd.apache.org/docs/2.4/ssl/ssl_howto.html)
99+
- [caddy](https://caddyserver.com/docs/tls)
100+
101+
Note: Enabling HTTPS only at the proxy level is referred as [TLS Termination Proxy](https://en.wikipedia.org/wiki/TLS_termination_proxy). The proxy server accepts incoming TLS connections, decrypts the contents, and passes the now unencrypted contents to Gitea. This is normally fine as long as both the proxy and Gitea instances are either on the same machine, or on different machines within private network (with the proxy is exposed to outside network). If your Gitea instance is separated from your proxy over a public network, or if you want full end-to-end encryption, you can also [enable HTTPS support directly in Gitea using built-in server](#使用内置服务器) and forward the connections over HTTPS instead.

modules/context/context.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747

4848
// Render represents a template render
4949
type Render interface {
50-
TemplateLookup(tmpl string) *template.Template
50+
TemplateLookup(tmpl string) (*template.Template, error)
5151
HTML(w io.Writer, status int, name string, data interface{}) error
5252
}
5353

@@ -228,7 +228,7 @@ func (ctx *Context) HTML(status int, name base.TplName) {
228228
}
229229
if err := ctx.Render.HTML(ctx.Resp, status, string(name), templates.BaseVars().Merge(ctx.Data)); err != nil {
230230
if status == http.StatusInternalServerError && name == base.TplName("status/500") {
231-
ctx.PlainText(http.StatusInternalServerError, "Unable to find status/500 template")
231+
ctx.PlainText(http.StatusInternalServerError, "Unable to find HTML templates, the template system is not initialized, or Gitea can't find your template files.")
232232
return
233233
}
234234
if execErr, ok := err.(texttemplate.ExecError); ok {
@@ -247,7 +247,7 @@ func (ctx *Context) HTML(status int, name base.TplName) {
247247
if errorTemplateName != string(name) {
248248
filename += " (subtemplate of " + string(name) + ")"
249249
}
250-
err = fmt.Errorf("%w\nin template file %s:\n%s", err, filename, templates.GetLineFromTemplate(errorTemplateName, line, target, pos))
250+
err = fmt.Errorf("failed to render %s, error: %w:\n%s", filename, err, templates.GetLineFromTemplate(errorTemplateName, line, target, pos))
251251
} else {
252252
filename, filenameErr := templates.GetAssetFilename("templates/" + execErr.Name + ".tmpl")
253253
if filenameErr != nil {
@@ -256,7 +256,7 @@ func (ctx *Context) HTML(status int, name base.TplName) {
256256
if execErr.Name != string(name) {
257257
filename += " (subtemplate of " + string(name) + ")"
258258
}
259-
err = fmt.Errorf("%w\nin template file %s", err, filename)
259+
err = fmt.Errorf("failed to render %s, error: %w", filename, err)
260260
}
261261
}
262262
ctx.ServerError("Render failed", err)

modules/templates/dynamic.go

-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@
66
package templates
77

88
import (
9-
"html/template"
109
"io/fs"
1110
"os"
1211
"path/filepath"
13-
texttmpl "text/template"
1412

1513
"code.gitea.io/gitea/modules/setting"
1614
)
1715

18-
var (
19-
subjectTemplates = texttmpl.New("")
20-
bodyTemplates = template.New("")
21-
)
22-
2316
// GetAsset returns asset content via name
2417
func GetAsset(name string) ([]byte, error) {
2518
bs, err := os.ReadFile(filepath.Join(setting.CustomPath, name))

0 commit comments

Comments
 (0)