Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

IIS 與 Apache 共用 80 連接埠實戰:多 IP 綁定、Socket Pooling 停用與 httpcfg 配置完整指南

編輯:關於伺服器

同一台伺服器同時運行 IIS 與 Apache,最穩健的架構是多 IP 綁定:各自佔用獨立 IP 的 80/443 連接埠,避免連接埠衝突與 Socket 重疊。本文詳細說明 IIS 5/6/7+ 及 Apache 的綁定配置步驟,含 httpcfg、adsutil.vbs、Listen 指令實戰參數。

架構前提

  • 伺服器配置 兩張實體/虛擬網卡 或 單網卡多 IP (IP Aliasing)。
  • 範例 IP 規劃:
      • IIS:192.168.1.10
      • Apache:192.168.1.20

IIS 5.x (Windows 2000) 配置

cd C:\Inetpub\Adminscripts
cscript adsutil.vbs set w3svc/DisableSocketPooling TRUE
# 回顯:DisableSocketPooling : (BOOLEAN) True
iisreset
停用 Socket Pooling 後,IIS 只會監聽在「網站內容 → 網站標籤 → IP 位址」明確指定的 IP,而非 0.0.0.0 (所有 IP)。

IIS 6.0 (Windows Server 2003) 配置

  1. 從安裝光碟 SUPPORT\TOOLS\SUPPORT.CAB 解壓 httpcfg.exe 至 C:\Windows\System32\。
  2. 指定 IIS 監聽 IP:
    httpcfg set iplisten -i 192.168.1.10
  3. 查看綁定清單:
    httpcfg query iplisten
  4. 移除綁定 (如需調整):
    httpcfg delete iplisten -i 192.168.1.10
  5. 重啟服務驗證:
    net stop iisadmin /y && net start w3svc

IIS 7.0+ (Windows Server 2008/2012/2016/2019/2022) 配置

圖形介面:IIS 管理員 → 網站 → 繫結 → 編輯 → IP 位址選擇具體 IP (而非「所有未指派」)。
命令列 (appcmd):

appcmd set site "Default Web Site" /bindings:[protocol='http',bindingInformation='192.168.1.10:80:']
appcmd set site "Default Web Site" /bindings:[protocol='https',bindingInformation='192.168.1.10:443:' sslFlags='Sni']

Apache httpd.conf 配置

# 僅監聽指定 IP
Listen 192.168.1.20:80
Listen 192.168.1.20:443

# 虛擬主機綁定對應 IP
<VirtualHost 192.168.1.20:80>
    ServerName apache.example.com
    DocumentRoot "/var/www/html"
</VirtualHost>

<VirtualHost 192.168.1.20:443>
    ServerName apache.example.com
    SSLEngine on
    SSLCertificateFile "/etc/httpd/ssl/apache.crt"
    SSLCertificateKeyFile "/etc/httpd/ssl/apache.key"
</VirtualHost>

服務啟動順序關鍵 (避免競態衝突)

# 建議批次檔 startup_services.bat
net stop Apache2
net stop iisadmin /y
timeout /t 3
net start Apache2
net start w3svc
先停雙方 → 確保連接埠釋放 → 先啟 Apache (綁定具體 IP) → 再啟 IIS (已透過 httpcfg/繫結限制 IP)。

驗證與除錯

指令用途預期結果
netstat -ano \| findstr :80檢視連接埠佔用 PIDPID 對應 httpd.exe 與 w3wp.exe,IP 分別為 .10 與 .20
curl -I https://192.168.1.10測試 IIS 回應HTTP/1.1 200 OK / Server: Microsoft-IIS
curl -I https://192.168.1.20測試 Apache 回應HTTP/1.1 200 OK / Server: Apache
telnet 192.168.1.10 80手工驗證 TCP 連線連線成功,發送 GET / HTTP/1.0 得回應

單 IP 環境替代方案 (不推薦生產環境)

  • 連接埠錯開:IIS 用 80,Apache 用 8080/8000 → 需在 URL 帶連接埠,用戶體驗差、SEO 不利。
  • 反向代理鏈:IIS 做前端 (80/443) → ARR (Application Request Routing) 依 Host Header 轉發至 Apache (內網 127.0.0.1:8080)。
    優點:單一對外 IP、統一 SSL 終結、WAF 整合。
    缺點:增加一層躍點、ARR 效能開銷、除錯複雜度提升。

現代容器化替代架構

# docker-compose.yml 片段
services:
  iis:
    image: mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2022
    ports: ["192.168.1.10:80:80", "192.168.1.10:443:443"]
  apache:
    image: httpd:2.4
    ports: ["192.168.1.20:80:80", "192.168.1.20:443:443"]
容器化天然隔離網路命名空間,無需調整主機 Socket 設定,強烈建議新專案採用容器編排 (Kubernetes/Swarm/Nomad) 取代裸金屬共存部署。
Copyright © Windows教程網 All Rights Reserved