CentOS 7安装SNI Proxy对HTTPS站点进行反向代理
导读
提到反向代理,可能最常见的就是Nginx了,如果使用Nginx反向代理HTTPS站点,且需要通过HTTPS访问的时候,则需要要在Nginx上配置SSL证书。而SNI Proxy则可以解决这个问题,我们无需在反代服务器上部署SSL证书,即可通过HTTPS访问。
举个例子
某一天你想访问https://www.google.com,你发现因为某Q的原因,发现根本无法打开。于是寻思有没有什么办法在不改变访问域名的情况下成功打开https://www.google.com,这一点SNI Proxy可以实现。
为了便于理解,我画了一个简单的示意图

安装SNI Proxy
先安装需要的依赖:
yum -y install autoconf automake curl gettext-devel libev-devel pcre-devel perl pkgconfig rpm-build udns-devel
访问:https://github.com/dlundquist/sniproxy/releases下载源码并解压,进入SNI Proxy执行编译命令:
./autogen.sh && ./configure && make dist
如果安装过程无报错,可执行命令sniproxy -V查看版本。

创建配置文件并启动
SNI Proxy默认配置文件位于/etc/sniproxy.conf,内容可参考:
user daemon
pidfile /tmp/sniproxy.pid
error_log {
    syslog daemon
    priority notice
}
listener 127.0.0.1:443 {
    protocol tls
    table TableName    # Specify a server to use if the initial client request doesn't contain
    # a hostname
    fallback 192.0.2.5:443}
table TableName {    # Match exact request hostnames
    example.com 192.0.2.10:4343
    # If port is not specified the listener port will be used
    example.net [2001:DB8::1:10]    # Or use regular expression to match
    .*\\.com    [2001:DB8::1:11]:443
    # Combining regular expression and wildcard will resolve the hostname
    # client requested and proxy to it
    .*\\.edu    *:443}最后输入命令sniproxy启动即可,可输入ps -ef|grep 'sniproxy'来查看进程是否启动。注意:防火墙也需要放行监听端口

访问测试
可通过修改hosts文件将域名指向到SNI Proxy服务器来测试是否可以访问。
最后
SNI Proxy相比nginx反向代理的好处是配置更简单,不需要在SNI Proxy服务上部署SSL证书,流量仅根据HTTP header中的HOST进行透传到目标服务器。由于场景不一样,并不是说比nginx反向代理更好,还要看具体用途。
另外xiaoz并未在高并发的情况下做过测试,不清楚SNI Proxy能否胜任高并发任务。另外SNI Proxy + 自建DNS有奇效,你懂的。
SNI Proxy项目地址:https://github.com/dlundquist/sniproxy

 
