萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Nginx proxy_set_header 與 sub_filter 實現騰訊企業郵箱反向代理與域名重寫

騰訊企業郵箱新註冊域名無法直接綁定,利用 Nginx 反向代理 exmail.qq.com 並重寫回應內容中的連結/資源路徑,實現自定義域名 mail.t4x.org 存取。需編譯 --with-http_sub_module。

完整 Nginx 配置


# HTTP 強制導向 HTTPS
server {
    listen 80;
    server_name mail.t4x.org;
    rewrite ^/(.*)$ https://mail.t4x.org/$1 permanent;

    location / {
        root /site/t4x.org/mail;
        index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /site/t4x.org/mail;
    }
}

# HTTPS 反向代理騰訊企業郵箱
server {
    listen 443 ssl spdy;
    server_name mail.t4x.org;

    ssl on;
    ssl_certificate mail.t4x.org_bundle.crt;
    ssl_certificate_key mail.t4x.org.key;
    ssl_session_cache shared:SSL:1m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  # 建議移除 SSLv2/SSLv3
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_set_header Accept-Encoding "";
        proxy_set_header Host mail.t4x.org;
        proxy_pass https://exmail.qq.com;

        sub_filter https://mail.t4x https://www.111cn.net;  # 重寫回應內容中的域名
        sub_filter_once off;                                # 替換所有匹配
    }
}

關鍵指令解析

  • proxy_set_header Accept-Encoding "":清除接受編碼,避免回應被壓縮導致 sub_filter 無法匹配。
  • proxy_set_header Host mail.t4x.org:向上游傳送自定義 Host,某些服務需驗證 Host。
  • sub_filter:將回應體中的字串替換 (舊 → 新),需 --with-http_sub_module。
  • sub_filter_once off:預設只替換第一次,off 替換全部。

編譯檢查


nginx -V 2>&1 | grep -o http_sub_module
# 有輸出表示已編入
安全提醒:代理郵箱服務涉及敏感帳號資料,請確保 TLS 設定完善、啟用 HSTS、限制存取來源 IP,並定期更新憑證。
copyright © 萬盛學電腦網 all rights reserved