让一个端口同时做两件事

让一个端口同时做两件事: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
即可运行。
好了,大家应该可以举一反三,起码也可以依葫芦画瓢吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值