> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/on-premises/zh-cn/pei-zhi/overleaf-toolkit/tls-proxy.md).

# TLS 代理

一个可选的 TLS 代理，用于使用 NGINX 终止 HTTPS 连接。

运行 `bin/init --tls` 用于使用 NGINX 代理配置初始化本地配置，或将 NGINX 代理配置添加到现有本地配置中。一个 **示例** 私钥会被创建在 `config/nginx/certs/overleaf_key.pem` 和一个 **虚拟** 证书位于 `config/nginx/certs/overleaf_certificate.pem`。你可以将它们替换为你实际的私钥和证书，或者将 `TLS_PRIVATE_KEY_PATH` 和 `TLS_CERTIFICATE_PATH` 变量分别设置为你实际私钥和证书的路径。

NGINX 的默认配置提供在 `config/nginx/nginx.conf` 中，你可以根据需要进行自定义。配置文件的路径可以通过 `NGINX_CONFIG_PATH` 变量更改。

{% hint style="success" %}
如果你有一个 **docker-compose.yml** 基础的部署，或者你自己管理 NGINX 反向代理，可以查看一个示例 **nginx.conf** 文件 [这里](https://github.com/overleaf/toolkit/blob/master/lib/config-seed/nginx.conf).
{% endhint %}

将以下部分添加到你的 `config/overleaf.rc` 文件中，如果它还不存在：

```
# TLS 代理配置（可选）
NGINX_ENABLED=false
NGINX_CONFIG_PATH=config/nginx/nginx.conf
NGINX_HTTP_PORT=80

# 将这些 IP 地址替换为你主机的外部 IP 地址
NGINX_HTTP_LISTEN_IP=127.0.1.1 
NGINX_TLS_LISTEN_IP=127.0.1.1
TLS_PRIVATE_KEY_PATH=config/nginx/certs/overleaf_key.pem
TLS_CERTIFICATE_PATH=config/nginx/certs/overleaf_certificate.pem
TLS_PORT=443
```

{% hint style="danger" %}
如果你使用外部 TLS 代理（即不由 Overleaf Toolkit 管理），请确保 `OVERLEAF_TRUSTED_PROXY_IPS=loopback,<ip-of-your-tls-proxy>` 已设置在你的 `config/variables.env`中，例如 `OVERLEAF_TRUSTED_PROXY_IPS=loopback,192.168.13.37`.
{% endhint %}

{% hint style="danger" %}
如果你在本地网络中使用来自 `172.16.0.0/12` （Docker 网络的默认子网）的子网，则你需要设置 `OVERLEAF_TRUSTED_PROXY_IPS=loopback,<network>` 在你的 `config/variables.env`。其中 `<network>` 是 `IPAM -> Config -> Subnet` 中的值 `docker inspect overleaf_default`中，例如 `OVERLEAF_TRUSTED_PROXY_IPS=loopback,172.19.0.0/16`。这是为了防止 `X-Forwarded` 标头被伪造。
{% endhint %}

{% hint style="info" %}
如果 `OVERLEAF_TRUSTED_PROXY_IPS` 未手动设置，则默认为 `loopback`。如果手动设置，你必须确保包含 `loopback`, `localhost` 或 `127.0.0.1`，它会信任 **nginx** 实例，该实例运行在 **sharelatex** 容器中。
{% endhint %}

如果你已正确配置受信任代理 IP，你应该会在 `/user/sessions` 页面上看到你的公网 IP 地址，如下：

<figure><img src="/files/d5327889508c36d50ded0ad1f8828e46f0d6f39d" alt="" width="375"><figcaption></figcaption></figure>

如果上面显示的 IP 地址仍然类似于 `127.0.0.1` 或某个私有/本地网络 IP 地址，请检查你的受信任代理配置，尤其是 `OVERLEAF_TRUSTED_PROXY_IPS`.

为了运行代理，请更改 `NGINX_ENABLED` 在 `config/overleaf.rc` 中的值，从 `false` 改为 `true` 并重新运行 `bin/up`.

默认情况下，HTTPS 网页界面将可在 `https://127.0.1.1:443`上访问。到 `http://127.0.1.1:80` 的连接将被重定向到 `https://127.0.1.1:443`。若要更改 NGINX 监听的 IP 地址，请设置 `NGINX_HTTP_LISTEN_IP` 和 `NGINX_TLS_LISTEN_IP` 变量。端口可以通过 `NGINX_HTTP_PORT` 和 `TLS_PORT` 变量更改。

如果 NGINX 启动失败并出现错误信息 `启动用户空间代理时出错：监听 tcp4 ... bind：地址已在使用中` 请确保 `OVERLEAF_LISTEN_IP:OVERLEAF_PORT` 不与 `NGINX_HTTP_LISTEN_IP:NGINX_HTTP_PORT`.

```mermaid
sequenceDiagram
participant user as 用户
participant external as 外部主机
participant internal as 主机内部
participant nginx as nginx
participant sharelatex as sharelatex
participant git-bridge as git-bridge
%% 用户连接到外部主机 HTTP
user->>+ external: HTTP
note over external: NGINX_HTTP_LISTEN_IP:NGINX_HTTP_PORT
external->>+ nginx: HTTP
note over nginx: nginx:80
nginx-->>-external: 301
%% 用户连接到外部主机 HTTPS
user->>+ external: HTTPS
note over external: NGINX_TLS_LISTEN_IP:TLS_PORT
external->>+ nginx: HTTPS
note over nginx: nginx:443
nginx->>+ sharelatex: HTTP
note over sharelatex: sharlatex:80
%% 用户连接到 localhost HTTP
user->>+ internal: HTTP
note over internal: OVERLEAF_LISTEN_IP:OVERLEAF_PORT
internal->>+sharelatex: HTTP
note over sharelatex: sharlatex:80
%% sharelatex 连接到 git-bridge
sharelatex->>+git-bridge: HTTP /git/
note over git-bridge: git-bridge:8000
note over sharelatex: GIT_BRIDGE_HOST:GIT_BRIDGE_PORT
git-bridge->>+sharelatex: WEB/WEB-API:3000
git-bridge->>+sharelatex: HISTORY-V1:3100
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ayakaleaf-pro.ayaka.space/on-premises/zh-cn/pei-zhi/overleaf-toolkit/tls-proxy.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
