> 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-tw/she-ding/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/e2e74756cb1f94830425ca73752b3fb2b4379e95" 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 無法啟動並顯示錯誤訊息 `Error starting userland proxy: listen tcp4 ... bind: address already in use` 請確保 `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
%% 使用者連線到本機 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-tw/she-ding/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.
