---
title: DNS与Cloudflare
description: 将您的域名解析至NexCMP服务器（可选通过Cloudflare），并使用正确的SSL模式获取有效证书。
url: https://nexcmp.com/zh-cn/docs/getting-started/dns-and-cloudflare/
applies_to: NexCMP 1.6.1
updated: 2026-11-02
---

# DNS与Cloudflare {#dns-and-cloudflare}

NexCMP需要一个DNS名称（即其**主域名**），并支持两种运行模式。

## 模式A：DNS直接指向服务器 {#mode-a-dns-points-straight-at-the-server}

1. 创建一条**A**记录`cloud.example.com → <server public IPv4>`（若服务器支持IPv6，还需创建一条**AAAA**记录）。
2. 可选配置（如果使用）：让`admin.cloud.example.com`和`partner.cloud.example.com`指向相同的IP。
3. 运行（或重新运行）[安装程序](https://nexcmp.com/zh-cn/docs/getting-started/installation/)。它会检测到该名称已解析至服务器自身的公网IP，并通过80端口的HTTP-01质询获取Let's Encrypt证书。

## 模式B：域名由Cloudflare代理 {#mode-b-the-domain-is-proxied-by-cloudflare}

创建相同的记录并开启橙色云朵图标（**Proxied**）。安装程序检测到该名称仅解析到Cloudflare地址后，会自动切换到Cloudflare模式：

- nginx仍会在您的服务器上监听80/443端口；
- 访客的真实IP是从 `CF-Connecting-IP` 请求头中读取的——**仅限**来自Cloudflare公开网段的连接，因此其他任何人无法伪造；
- 证书通过 Let's Encrypt 获取（Cloudflare会将验证请求转发给您的源站）；如果暂时无法实现，则会创建一个**自签名的源站证书**以确保“完全”模式能够工作，后续重新运行安装程序即可升级到 Let's Encrypt。

您可以通过 `--behind-cloudflare` 强制使用此模式。

::: important 在 Cloudflare 中设置 SSL/TLS 模式
在 Cloudflare 中前往 **SSL/TLS › Overview**，一旦安装程序报告获取到 Let's Encrypt 证书，请选择 **Full (strict)**。如果使用的是自签名源站证书，请使用 **Full**。**Flexible** 会导致重定向循环。
:::

::: tip
如果开启了“始终使用 HTTPS”（Always Use HTTPS），Cloudflare 会将验证请求发送到您源站的 443 端口。安装程序会通过先创建一个临时的自签名证书、然后重试 Let's Encrypt 来处理这种情况。
:::

## 检查它 {#check-it}

```bash
dig +short cloud.example.com        # A record (a Cloudflare address in mode B)
curl -I https://cloud.example.com/healthz
sudo certbot certificates           # certificate name, domains and expiry
```

证书会通过 certbot 的定时器自动续期。续期使用 webroot `/var/www/nexcmp-acme`，因此必须保持 80 端口可访问（直接访问或通过 Cloudflare）。

## 常见问题 {#common-problems}

| 症状 | 原因 | 解决办法 |
|---|---|---|
| 安装程序提示 "DNS … does not point to … (nor to Cloudflare) — skipping nginx/TLS" | DNS记录缺失、错误或未生效 | 修复记录，等待生效，然后重新运行安装程序。 |
| 浏览器在 Cloudflare 后面显示 `ERR_TOO_MANY_REDIRECTS` | SSL模式为 **Flexible** | 设置为 **Full (strict)**（或 **Full**）。 |
| Cloudflare 错误 525/526 | 源站证书被拒绝 | 配合自签名源站证书使用 **Full**，或配合 Let's Encrypt 使用 **Full (strict)**。 |
| 访问者的日志中全显 Cloudflare IP | 未处于 Cloudflare 模式 | 带 `--behind-cloudflare` 参数重新运行安装程序。 |

更多内容请参见 [Installation errors](https://nexcmp.com/zh-cn/docs/troubleshooting/installation-errors/)。
