基于域名的7層轉(zhuǎn)發(fā)的實現(xiàn)(NAT+反向代理)
在公司的實際辦公網(wǎng)中,因為出口IP只有一個,要實現(xiàn)對外提供服務(wù)的話就必須得做端口映射,如果有多個服務(wù)要對外開放的話,這只能通過映射不同端口來區(qū)分,這在實際使用過程中非常的痛苦(記憶困難、一一對應(yīng)關(guān)系也沒有規(guī)律、訪問的時候還得加端口),這個痛苦的問題用表格的形式來形象的描述如下:
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 |
在需要對外開放的服務(wù)很多的情況下,NAT的方式雖然難用、難記,但至少還是能夠滿足需求的(可用端口要小于65535個),但如果A、B、C服務(wù)都想(或者必須)使用默認(rèn)的80、443端口的話,在只有一個公網(wǎng)IP的情況下是沒法滿足的,如果能有一種如下的實現(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 |
首先來分析一下,傳統(tǒng)NAT的話肯定是實現(xiàn)不了,因為NAT是3層ip加4層端口的方式做映射,而域名(如http header中)都屬于7層的內(nèi)容,要實現(xiàn)的話只能借助支持7層http協(xié)議解析的工具實現(xiàn),經(jīng)過一番研究發(fā)現(xiàn)反向代理可以實現(xiàn),那太好了,反響代理的工具一大堆:squid、apache、nginx、haproxy、mysql proxy等等,本文僅講基于http、https協(xié)議的實現(xiàn),其他協(xié)議暫不討論。
有了工具的支持,接下來就得考慮考慮如何部署的問題:
(1)域名解析到路由器的公網(wǎng)ip-->在路由器(pfsense)上安裝squid-->配置反向代理(開啟http、https反向代理、主機映射、域名正則匹配轉(zhuǎn)發(fā))-->成功實現(xiàn)(需要路由器支持);
(2)域名解析到路由器的公網(wǎng)ip-->在路由器上做傳統(tǒng)NAT,將80、443端口分別指向反向代理服務(wù)器-->配置反向代理服務(wù)器的-->成功實現(xiàn)(通用方法);
其中第一個方法我已經(jīng)很好的實現(xiàn)http的反向代理,但對于https,由于squid不支持SNI(server name Indication),僅能支持一個https站點,且很多公司用的路由器可能不支持安裝squid軟件,所以接下來我主要介紹通用的方法:通過在linux上安裝nginx來搭建反向代理服務(wù)來支持基于域名的7層轉(zhuǎn)發(fā)。
下載openssl庫
- 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,編譯時加入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就可以使用了,效果對客戶端是完全透明的
➜ ~ 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