> For the complete documentation index, see [llms.txt](https://docs.console.zenlayer.com/welcome/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.console.zenlayer.com/welcome/cn/elastic-compute/networking/01-overview-3/04-dnat.md).

# DNAT — 入站转发

DNAT（目的地址转换）使外部流量无需为私有实例分配公网 IP 即可访问该实例。您定义一个映射关系 — 网关公网侧的 EIP 和端口，以及内部的私有 IP 和端口 — 网关在转发每个匹配数据包之前改写目标地址。

## 典型使用场景

**远程管理访问。** 将公网端口（例如 `2222`）映射到私有实例上的 SSH 端口（`22`）。管理员通过公网端口连接到 EIP，网关将会话转发到实例。实例无需拥有公网 IP。

**暴露单个服务端点。** 需要被特定外部客户端访问的后端 API、内部控制台或单租户服务。与其为实例分配 EIP，不如创建一条 DNAT 条目，将该特定端口路由到实例。

**减少公网 IP 使用量。** 一个 EIP 配合多条 DNAT 条目可以为多个私有实例提供服务 — 每个实例使用不同的端口。例如，`EIP:2221` → `实例-A:22` 和 `EIP:2222` → `实例-B:22` 共享一个公网地址，同时保持实例完全独立。

**测试和预发布环境。** 临时暴露开发服务器或预发布端点，无需重构网络。需要时创建 DNAT 条目，用完后删除。

## 工作原理

当外部客户端连接到网关 EIP 上的某个已配置端口时：

1. 数据包到达 NAT 网关，通过 `EIP + 端口 + 协议` 匹配 DNAT 条目。
2. 网关将目标 IP 和端口改写为配置的私有 IP 和端口。
3. 数据包被传递到 VPC 内的私有实例。
4. 当实例回复时，网关将源地址改写回 EIP 和外部端口，然后将响应转发给客户端。

![DNAT 流程](/files/k406hyPH4Vf1QWiL8wn1)

## DNAT 条目

每条条目将一个公网 `EIP:端口` 映射到一个私有 `IP:端口`。Console 的 **NAT 规则**标签页列出每个条目及以下字段：

| 字段             | 说明                                            |
| -------------- | --------------------------------------------- |
| **DNAT 条目 ID** | 系统分配的条目标识符。                                   |
| **弹性 IPv4 地址** | 接收入站流量的 EIP。每条条目对应一个 EIP。                     |
| **公网端口**       | EIP 上供外部客户端连接的端口（例如 `2222`，或范围如 `9000-9009`）。 |
| **私有 IP 地址**   | 目标实例或服务的私有 IP。                                |
| **私有端口**       | 目标实例上的端口。其宽度必须与公网端口范围一致。                      |
| **协议**         | `tcp`、`udp` 或 `any`。选择 `any` 时，公网端口和私有端口必须为空。 |

多条条目可以引用同一 EIP，前提是每条条目使用不同的公网端口 + 协议组合。

## 端口范围

公网端口和私有端口可以是单个端口或等宽范围。每个公网端口映射到相同偏移位置的私有端口。

| 公网端口        | 私有端口        | 结果                          |
| ----------- | ----------- | --------------------------- |
| `8080`      | `80`        | 8080 端口入站 → 实例 80 端口        |
| `2222`      | `22`        | 2222 端口入站 → 实例 22 端口        |
| `9000-9009` | `3000-3009` | 9000–9009 端口分别映射到 3000–3009 |

## 选择协议

端口转发请使用 `tcp` 或 `udp`。仅当您希望不指定端口就转发所有协议时，才使用 `any`。

请避免对 SSH 或 RDP 等管理端口使用 `any`。这些服务使用 TCP，应使用明确的端口进行映射。


---

# 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:

```
GET https://docs.console.zenlayer.com/welcome/cn/elastic-compute/networking/01-overview-3/04-dnat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
