让一个端口同时做两件事:http/https和ssh

相信很多人都在YY:能不能让80端口分析连接协议,如果是http协议就让服务器交给http服务程序(如Apache、Nginx等)处理,如果是ssh协议就交给ssh服务程序(如OpenSSH Server)处理呢?

答案显然是有的。

首先,配置http服务程序监听8080端口或者让https服务监听8443端口,配置ssh服务程序监听22端口。具体不再赘述,如果这都不懂就不用往下看了,因为肯定会搞不定的。

然后,安装一个叫haproxy的强大工具。步骤如下。

下载源代码:

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.16.tar.gz  

查看当前内核版本:

uname -r  

然后进入目录编译安装:

cd haproxy-1.4.16  make TARGET=linux26 PREFIX=/usr/local/blog.creke.net/haproxy  make install PREFIX=/usr/local/blog.creke.net/haproxy  

其中,第二行的“TARGET”参数要和内核版本一致。第二、三行的“PREFIX”是安装位置。

最后,配置haproxy。

如果要监听80端口,检测到http协议就转发给8080端口使用HTTP,否则转发给22端口使用ssh。配置如下:

#By http://blog.creke.net/  global      maxconn 5120      chroot /usr/local/blog.creke.net/haproxy      daemon      quiet      nbproc 2      pidfile /usr/local/blog.creke.net/haproxy/haproxy.pid  defaults      timeout connect 5s      timeout client 50s      timeout server 20s  listen http      bind :80      timeout client 1h      tcp-request inspect-delay 2s      acl is_http req_proto_http      tcp-request content accept if is_http      server server-http :8080      use_backend ssh if !is_http  backend ssh      mode tcp      timeout server 1h      server server-ssh :22  

如果还有监听443端口,检测到https协议就转发到8443端口使用HTTPS,否则转发给22端口使用ssh。则配置如下:

global      maxconn 5120      chroot /usr/local/blog.creke.net/haproxy      daemon      quiet      nbproc 2      pidfile /usr/local/blog.creke.net/haproxy/haproxy.pid  defaults      timeout connect 5s      timeout client 50s      timeout server 20s  listen https      bind :443      timeout client 1h      tcp-request inspect-delay 2s      acl is_ssl req_ssl_ver 2:3.1      tcp-request content accept if is_ssl      server server-https :8443      use_backend ssh if !is_ssl  backend ssh      mode tcp      timeout server 1h      server server-ssh :22  

把内容保存为“/usr/local/blog.creke.net/haproxy/etc/haproxy.conf”,执行命令:

/usr/local/blog.creke.net/haproxy/sbin/haproxy -f /usr/local/blog.creke.net/haproxy/etc/haproxy.conf  

即可运行。

好了,大家应该可以举一反三,起码也可以依葫芦画瓢吧。

原文出处:creke -> http://blog.creke.net/758.html

本站所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如果侵犯你的利益,请发送邮箱到 [email protected],我们会很快的为您处理。
超哥软件库 » 让一个端口同时做两件事:http/https和ssh