# 【常見問題】通訊協定＆port 在呼叫上的差異

以下是針對你提到的三種不同 URL 呼叫方式：

```
1. https://a.b.com:3000
2. http://a.b.com:3000
3. http://a.b.com

```

---

## ✅ 一、三種呼叫差異總覽

<table id="bkmrk-%E5%91%BC%E5%8F%AB%E6%96%B9%E5%BC%8F-%E5%8D%94%E5%AE%9A-port-%E6%98%AF%E5%90%A6%E5%8A%A0%E5%AF%86-%E5%B0%8D-"><thead><tr><th>呼叫方式</th><th>協定</th><th>Port</th><th>是否加密</th><th>對 Nginx 的需求</th><th>常見用途</th></tr></thead><tbody><tr><td>`https://a.b.com:3000`</td><td>HTTPS</td><td>3000</td><td>✅ 是</td><td>`listen 3000 ssl;`</td><td>測試自訂 HTTPS port</td></tr><tr><td>`http://a.b.com:3000`</td><td>HTTP</td><td>3000</td><td>❌ 否</td><td>`listen 3000;`</td><td>自訂 HTTP port 開發</td></tr><tr><td>`http://a.b.com`</td><td>HTTP</td><td>80</td><td>❌ 否</td><td>`listen 80;`（預設）</td><td>一般公開網站</td></tr></tbody></table>

---

## ✅ 二、為什麼會有差異？

### 1. 協定不同（HTTP vs HTTPS）

- HTTP：明文傳輸，不需要 TLS 憑證。
- HTTPS：加密傳輸，需要 SSL 憑證，並透過 TLS 握手。

### 2. Port 不同（預設 vs 自訂）

- HTTP 預設是 80、HTTPS 預設是 443。
- 如果你指定其他 port（如 3000），伺服器端（如 Nginx）**必須額外開啟對應的 port 並配置好 listen 與憑證**。

---

## ✅ 三、Nginx 實際設定對應

### 1️⃣ 對應 `https://a.b.com:3000`

```nginx
server {
    listen 3000 ssl;
    server_name a.b.com;

    ssl_certificate     /etc/nginx/certs/a.b.com.crt;
    ssl_certificate_key /etc/nginx/certs/a.b.com.key;

    location / {
        proxy_pass http://localhost:4000;
    }
}

```

> 🔒 加密連線，必須設定 `ssl` 與有效憑證。

---

### 2️⃣ 對應 `http://a.b.com:3000`

```nginx
server {
    listen 3000;
    server_name a.b.com;

    location / {
        proxy_pass http://localhost:4000;
    }
}

```

> 🔓 明文傳輸，不需要 SSL 憑證。適用於測試環境或內網。

---

### 3️⃣ 對應 `http://a.b.com`

```nginx
server {
    listen 80;
    server_name a.b.com;

    location / {
        proxy_pass http://localhost:4000;
    }
}

```

> 🌐 最常見的網站 HTTP 配置。不需帶 port。

---

## ✅ 四、連線行為對照表

<table id="bkmrk-%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E7%B6%B2%E5%9D%80-%E7%80%8F%E8%A6%BD%E5%99%A8%E6%88%96-curl-%E7%99%BC%E5%87%BA"><thead><tr><th>使用者輸入網址</th><th>瀏覽器或 curl 發出的請求會去到哪裡？</th></tr></thead><tbody><tr><td>`https://a.b.com:3000`</td><td>`a.b.com` 的 **port 3000**（用 TLS）</td></tr><tr><td>`http://a.b.com:3000`</td><td>`a.b.com` 的 **port 3000**（無加密）</td></tr><tr><td>`http://a.b.com`</td><td>`a.b.com` 的 **port 80**</td></tr></tbody></table>

---

## ✅ 五、常見錯誤與排查

<table id="bkmrk-%E5%95%8F%E9%A1%8C-%E5%8E%9F%E5%9B%A0-%E8%A7%A3%E6%B3%95-curl-https%3A"><thead><tr><th>問題</th><th>原因</th><th>解法</th></tr></thead><tbody><tr><td>`curl https://a.b.com:3000` 報錯 TLS</td><td>你設定了 HTTPS，但 Nginx 未加上 `ssl` 或憑證有誤</td><td>加上 `ssl` 並設定憑證</td></tr><tr><td>只能用 `http://a.b.com`，其他都失敗</td><td>Nginx 只開 port 80，沒開 3000</td><td>增加對應 `listen 3000` 的設定</td></tr><tr><td>呼叫 `http://a.b.com:3000` 被拒絕</td><td>Server 未開啟該 port</td><td>檢查防火牆、Nginx、Docker 映射是否有允許 3000</td></tr></tbody></table>

---

## ✅ 六、總結：選擇哪一種？

<table id="bkmrk-%E4%BD%BF%E7%94%A8%E5%A0%B4%E6%99%AF-%E5%BB%BA%E8%AD%B0%E5%91%BC%E5%8F%AB%E6%96%B9%E5%BC%8F-%E5%85%A7%E9%83%A8%E9%96%8B%E7%99%BC%E6%B8%AC%E8%A9%A6-h"><thead><tr><th>使用場景</th><th>建議呼叫方式</th></tr></thead><tbody><tr><td>內部開發測試</td><td>`http://a.b.com:3000` 或 `https://a.b.com:3000`</td></tr><tr><td>正式站公開服務</td><td>`https://a.b.com`（預設 port 443）</td></tr><tr><td>Nginx 反向代理串接後端</td><td>視你要轉發的是 HTTP or HTTPS 及 port 而定</td></tr></tbody></table>

---

如你需要一份完整支援三種 port 的 `nginx.conf` 以及搭配 Express 的後端範例，我可以幫你整合好。需要嗎？