> ## Content Index
> Fetch the complete content index at: https://brotherhong.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# 网站、IP 与端口的关系
- URL: https://brotherhong.com/ip-port-basics/
- Published: 2025-11-05T00:00:00.000Z
- Updated: 2025-11-05T00:00:00.000Z
- Description: 看懂域名、IP、DNS、证书和端口之间的关系，避免部署时概念打架。
- Author: 洪哥
- Tags: 入门, 网络基础, IP, 端口, DNS

如果你刚接触服务器和建站，这篇会帮你把域名、IP、证书、端口和 Nginx 分发这些概念串起来。

## 1\. 服务器与 IP

每台服务器都绑定了一个 IP 地址。假如有两台服务器：

| 服务器   | IP 地址   |
| ----- | ------- |
| A 服务器 | 1.2.3.4 |
| B 服务器 | 1.2.3.5 |

## 2\. 域名与 IP

域名（包括子域名）本身和 IP 没有关联，需要通过 **DNS**（如 Cloudflare）把域名指向 IP：

- `vpn.tunecoder.com` → `1.2.3.4`
- `api.tunecoder.com` → `1.2.3.5`

## 3\. SSL 证书与域名 / IP

- SSL 证书**推荐绑定到域名**（如 `https://api.tunecoder.com`），可通过 Let's Encrypt 等机构免费申请，全球认可。
- 也可以通过**自签名证书**绑定到 IP，自己用没问题，但别人访问会提示"不安全"。

## 4\. 443 端口

HTTPS 默认走 **443 端口**，好处是访问时不需要在地址栏手动填端口号：

| 访问方式                          | 说明                |
| ----------------------------- | ----------------- |
| https://api.tunecoder.com     | 默认 443 端口，无需填写    |
| https://api.tunecoder.com:444 | 非默认端口，必须显式添加 :444 |

## 5\. Nginx 多站点部署

Nginx（或 Apache 等）可以在**一台服务器上部署多个网站**。例如在 A 服务器（`1.2.3.4`）上同时跑：

- `https://api.tunecoder.com`
- `https://vpn.tunecoder.com`

用 `ping` 命令看，两个网站的 IP 都是 `1.2.3.4`，端口也都是 `443`。

## 6\. 路由分发

两个网站访问的是**同一个 IP + 同一个端口**，但 Nginx 通过配置**区分域名**，把请求分别交给不同的网站处理。

> 这就是所谓的**基于域名的虚拟主机（Server Name Based Virtual Hosting）**。

## 7\. 多服务器使用同一域名

通过 Cloudflare，可以把不同子域名分配到不同服务器。例如：

- `newapi.tunecoder.com` → B 服务器（`1.2.3.5`）

---

## 最终效果总览

以下三个网站互不干扰：

| 网站   | 域名                           | IP      | 端口  |
| ---- | ---------------------------- | ------- | --- |
| 网站 1 | https://api.tunecoder.com    | 1.2.3.4 | 443 |
| 网站 2 | https://vpn.tunecoder.com    | 1.2.3.4 | 443 |
| 网站 3 | https://newapi.tunecoder.com | 1.2.3.5 | 443 |