基于域名的7層轉(zhuǎn)發(fā)的實(shí)現(xiàn)(NAT+反向代理)
在公司的實(shí)際辦公網(wǎng)中,因?yàn)槌隹贗P只有一個(gè),要實(shí)現(xiàn)對(duì)外提供服務(wù)的話就必須得做端口映射,如果有多個(gè)服務(wù)要對(duì)外開(kāi)放的話,這只能通過(guò)映射不同端口來(lái)區(qū)分,這在實(shí)際使用過(guò)程中非常的痛苦(記憶困難、一一對(duì)應(yīng)關(guān)系也沒(méi)有規(guī)律、訪問(wèn)的時(shí)候還得加端口),這個(gè)痛苦的問(wèn)題用表格的形式來(lái)形象的描述如下:
Public IP |
Public Port Number |
Internal IP |
Internal Port Number |
Note |
1.1.1.1 |
80 |
192.168.1.10 |
80 |
service A |
1.1.1.1 |
81 |
192.168.1.11 |
80 |
service B |
1.1.1.1 |
8080 |
192.168.1.25 |
80 |
service C |
1.1.1.1 |
443 |
192.168.1.26 |
443 |
service D |
1.1.1.1 |
444 |
192.168.1.35 |
443 |
service E |
在需要對(duì)外開(kāi)放的服務(wù)很多的情況下,NAT的方式雖然難用、難記,但至少還是能夠滿足需求的(可用端口要小于65535個(gè)),但如果A、B、C服務(wù)都想(或者必須)使用默認(rèn)的80、443端口的話,在只有一個(gè)公網(wǎng)IP的情況下是沒(méi)法滿足的,如果能有一種如下的實(shí)現(xiàn)方式,那就完美了:
Domain Name |
Public IP |
Public Port Number |
Internal IP |
Internal Port Number |
Note |
1.1.1.1 |
80 |
192.168.1.10 |
80 |
service A |
|
1.1.1.1 |
80 |
192.168.1.11 |
80 |
service B |
|
1.1.1.1 |
80 |
192.168.1.25 |
80 |
service C |
|
1.1.1.1 |
443 |
192.168.1.26 |
443 |
service D |
|
1.1.1.1 |
443 |
192.168.1.35 |
443 |
service E |
首先來(lái)分析一下,傳統(tǒng)NAT的話肯定是實(shí)現(xiàn)不了,因?yàn)镹AT是3層ip加4層端口的方式做映射,而域名(如http header中)都屬于7層的內(nèi)容,要實(shí)現(xiàn)的話只能借助支持7層http協(xié)議解析的工具實(shí)現(xiàn),經(jīng)過(guò)一番研究發(fā)現(xiàn)反向代理可以實(shí)現(xiàn),那太好了,反響代理的工具一大堆:squid、apache、nginx、haproxy、mysql proxy等等,本文僅講基于http、https協(xié)議的實(shí)現(xiàn),其他協(xié)議暫不討論。
有了工具的支持,接下來(lái)就得考慮考慮如何部署的問(wèn)題:
(1)域名解析到路由器的公網(wǎng)ip-->在路由器(pfsense)上安裝squid-->配置反向代理(開(kāi)啟http、https反向代理、主機(jī)映射、域名正則匹配轉(zhuǎn)發(fā))-->成功實(shí)現(xiàn)(需要路由器支持);
(2)域名解析到路由器的公網(wǎng)ip-->在路由器上做傳統(tǒng)NAT,將80、443端口分別指向反向代理服務(wù)器-->配置反向代理服務(wù)器的-->成功實(shí)現(xiàn)(通用方法);
其中第一個(gè)方法我已經(jīng)很好的實(shí)現(xiàn)http的反向代理,但對(duì)于https,由于squid不支持SNI(server name Indication),僅能支持一個(gè)https站點(diǎn),且很多公司用的路由器可能不支持安裝squid軟件,所以接下來(lái)我主要介紹通用的方法:通過(guò)在linux上安裝nginx來(lái)搭建反向代理服務(wù)來(lái)支持基于域名的7層轉(zhuǎn)發(fā)。
下載openssl庫(kù)
- wget http://www.openssl.org/source/openssl-1.0.1h.tar.gz
- tar xzvf openssl-1.0.1h.tar.gz
- mv openssl-1.0.1h /usr/local/openssl-1.0.1h/
下載nginx,編譯時(shí)加入SNI的支持
- yum install pcre pcre-devel
- yum install zlib zlib-devel
- wget http://nginx.org/download/nginx-1.6.0.tar.gz
- tar xzvf nginx-1.6.0.tar.gz
- cd nginx-1.6.0
- ./configure \
- --user=nginx \
- --group=nginx \
- --with-http_ssl_module \
- --with-openssl="/usr/local/openssl-1.0.1h/" \
- --with-openssl-opt="enable-tlsext" \
- --with-http_stub_status_module
- make
- make install
檢查nginx的安裝情況(關(guān)鍵TLS SNI support enabled):
- [root@svn ~]# /usr/local/nginx/sbin/nginx -V
- nginx version: nginx/1.6.0
- built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54)
- TLS SNI support enabled
- configure arguments: --user=nginx --group=nginx --with-http_ssl_module --with-openssl=/usr/local/openssl-1.0.1h/ --with-openssl-opt=enable-tlsext --with-http_stub_status_module
配置反向代理服務(wù)器:
- [root@svn ~]# more /usr/local/nginx/conf/nginx.conf
- ############https server revese proxy
- server {
- listen 10010 ssl;
- server_name app.wei.com;
- #Set up your cert paths
- ssl_certificate_key /usr/local/nginx/conf/ssl/app_wei.key;
- ssl_certificate /usr/local/nginx/conf/ssl/app_wei.crt;
- location / {
- proxy_pass https://192.168.100.123;
- }
- }
- server {
- listen 10010 ssl;
- server_name secure.wei.com;
- #Set up your cert paths
- ssl_certificate_key /usr/local/nginx/conf/ssl/mars-server.key;
- ssl_certificate /usr/local/nginx/conf/ssl/mars-server.crt;
- location / {
- proxy_pass https://192.168.100.177:443;
- }
- }
- ############http server revese proxy
- server {
- listen 10086 ;
- server_name secure.wei.com;
- location / {
- proxy_pass http://192.168.100.177;
- }
- }
- server {
- listen 10086 ;
- server_name dobby.wei.com;
- location / {
- proxy_pass http://192.168.100.148;
- }
- }
路由器NAT映射:1.1.1.1:80-->反向代理的10086;1.1.1.1:443-->反向代理的10010
重啟nginx就可以使用了,效果對(duì)客戶端是完全透明的
➜ ~ curl -I https://app.wei.com
HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Sat, 26 Jul 2014 01:48:14 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.8
參考:
Using the Nginx Web Server as a Reverse Proxy: Multiple SSL Sites with a Single IP Address
http://www.informit.com/articles/article.aspx?p=1994795
[squid-users] Reverse proxy with multiple SSL sites
http://www.squid-cache.org/mail-archive/squid-users/201406/0102.html
原文鏈接:http://blog.csdn.net/xuyaqun/article/details/38259169