mod_gnutls – 單一 IP 建置多重 SSL 網站

雖然 Apache 的文件上已明白說明單一IP無法建置多重 SSL 網站,不過這類需求與嘗試永遠存在。

有一陣子熱衷於尋找解法之際,曾看過 Everyday Work 的這篇解法,直接在 Certificate 中塞進多個 CN。最近則看到了國外提到用 mod_gnutls 這個 Apache Module 來實行,而且竟然又是個老早就有的專案。

mod_gnutls 是實做出 RFC3546 中的 Server Name Indication(SNI)解決方案,透過 TLS extension 達成 name-based 的 SSL 虛擬主機設定。mod_gnutls 的開發目的類似於 mod_ssl,但它並不依賴 OpenSSL,一方面是因為 OpenSSL 到目前 0.98 為止仍未支援 SNI,不過據說 0.99 會開始支援。

在下載並編譯後,將 libmod_gnutls.so 複製到 Apahce modules 目錄 (依平台而不同,如 /usr/lib/httpd/modules 或 /usr/local/libexec/apache2 等),並更名為 mod_gnutls.so,再將剛剛來源目錄中的 dhfile、rsafile 複製到 Apache 設定檔 httpd.conf 所在目錄。

接下來就是用你原本的方法產生每個網站的 Certificate,並參考官方說明文件編輯 Apache 設定檔:

### Main server configuration

# Load the module into Apache.
LoadModule gnutls_module modules/mod_gnutls.so

# Using 4 memcache servers to distribute the SSL Session Cache.
GnuTLSCache memcache "mc1.example.com mc2.example.com mc3.example.com mc4.example.com"

# With normal SSL Websites, you need one IP Address per-site.
Listen 1.2.3.1:443

其中 GnuTLSCache 是設定 SSL Session Cache,為了效能考量,不建議關閉(None),如果沒有採用分散架構,可以用 dbm 取代上面的 memcache。先自行建立一個用以存放 cache 資料的目錄,並將 Apache 執行帳號設為擁有者,回到剛剛編輯的 Apache 設定檔:

GnuTLSCache dbm "剛剛建立之絕對路徑"
# Set a timeout for the SSL Session Cache entries.
# Usually, this is set to 300 seconds:
GnuTLSCacheTimeout 300

最後就是虛擬主機設定部份:

### Virtual Hosts

NameVirtualHost 1.2.3.1:443

<VirtualHost 1.2.3.1:443>
    GnuTLSEnable on
    GnuTLSPriorities NORMAL
    DocumentRoot /www/site1.example.com/html
    ServerName site1.example.com:443
    GnuTLSCertificateFile conf/ssl/site1.crt
    GnuTLSKeyFile conf/ss/site1.key
</VirtualHost>
<VirtualHost 1.2.3.1:443>
    GnuTLSEnable on
    GnuTLSPriorities NORMAL
    DocumentRoot /www/site2.example.com/html
    ServerName site2.example.com:443
    GnuTLSCertificateFile conf/ssl/site2.crt
    GnuTLSKeyFile conf/ss/site2.key
</VirtualHost>

Written by gegewan on 一月 24th, 2009 with no comments.
Read more articles on FreeBSD/Linux.

Related articles

No comments

There are still no comments on this article.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .