80端口复用:利用haproxy把http流量和ssh流量分别转发到web服务器和ssh服务器

来源:互联网 发布:苹果手机最新电影软件 编辑:程序博客网 时间:2024/05/16 01:32

    我们实验室在学校里面有一台服务器,带一个公网ip,但是只开了一个80端口,22端口竟然不给开!!!这让我很是不爽。之前一直待学校里,不给开我忍忍就算了,最近搬到了个没有校园网的偏远地方,连不上实验室的服务器实在是不能忍。于是重新折腾之前安装在服务器上的haproxy。

    安装教程网络上到处到有,我按照网上的教程安装好haproxy后,再按照知乎上的 https://www.zhihu.com/question/31528831 教程配置之后,发现只在80端口访问网站是可以,但是用svn客户端访问我服务器上的svn返回错误,好像是显示option选项不支持之类的,受到http://siukwan.sinaapp.com/?p=960 这篇文章的启发,我觉得是在http的acl里面配置少了option选项,于是一口气把http所有的头选项都回到匹配项里,像这样子:

#GET POS(T) PUT DEL(ETE) OPT(IONS) HEA(D) CON(NECT) TRA(CE) acl is_http req.payload(0,3) -m bin 474554 504f53 505554 44454c 4f5054 484541 434f4e 545241
    于是把svn的问题解决了。

    接下来是ssh的问题,直接在acl匹配字符串SSH,然后每次在本地连接80端口都是返回以下错误:

ssh_exchange_identification: Connection closed by remote host


    而直接连接22端口却没有问题。在网上搜索这个错误都说是黑名单之类的错误,但是我觉得这里并不是这个原因。后来抓包研究了下ssh协议,觉得并不是所有ssh数据包开头都是字符串SSH,所以acl简单粗暴地认为只有以字符串SSH开头的包才转发到22端口是不对的,应该更加简单粗暴地把http以外的数据包都转发到22端口,于是我把转发到22端口的配置语句的if条件去掉,问题得以解决。

以下是完整的配置文件/etc/haproxy/haproxy.cfg,其中http和ssh功能是测试过的,https没有测试,不知道能不能用。22端口是ssh服务器,82端口是http服务器,都是从外网的80端口进行访问。


#---------------------------------------------------------------------# Example configuration for a possible web application.  See the# full configuration options online.##   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt##---------------------------------------------------------------------#---------------------------------------------------------------------# Global settings#---------------------------------------------------------------------global    # to have these messages end up in /var/log/haproxy.log you will    # need to:    #    # 1) configure syslog to accept network log events.  This is done    #    by adding the '-r' option to the SYSLOGD_OPTIONS in    #    /etc/sysconfig/syslog    #    # 2) configure local2 events to go to the /var/log/haproxy.log    #   file. A line like the following can be added to    #   /etc/sysconfig/syslog    #    #    local2.*                       /var/log/haproxy.log    #    log         127.0.0.1 local0    log         127.0.0.1 local1 notice    chroot      /var/lib/haproxy    pidfile     /var/run/haproxy.pid    maxconn     4000    user        haproxy    group       haproxy    daemon    # turn on stats unix socket    stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------# common defaults that all the 'listen' and 'backend' sections will# use if not designated in their block#---------------------------------------------------------------------defaults    mode                    http    log                     global    option                  httplog    option                  dontlognull    option http-server-close    option forwardfor       except 127.0.0.0/8    option                  redispatch    retries                 3    timeout http-request    10s    timeout queue           1m    timeout connect         10s    timeout client          1m    timeout server          1m    timeout http-keep-alive 10s    timeout check           10s    maxconn                 3000#---------------------------------------------------------------------# main frontend which proxys to the backends#---------------------------------------------------------------------#frontend  main *:5000#    acl url_static       path_beg       -i /static /images /javascript /stylesheets#    acl url_static       path_end       -i .jpg .gif .png .css .js##    use_backend static          if url_static#    default_backend             app#---------------------------------------------------------------------# static backend for serving up images, stylesheets and such#---------------------------------------------------------------------#backend static#    balance     roundrobin#    server      static 127.0.0.1:4331 check#---------------------------------------------------------------------# round robin balancing between the various backends#---------------------------------------------------------------------#backend app#    balance     roundrobin#    server  app1 127.0.0.1:5001 check#    server  app2 127.0.0.1:5002 check#    server  app3 127.0.0.1:5003 check#    server  app4 127.0.0.1:5004 checkfrontend mainmode tcpbind *:80log globaloption tcploglog-format %ft\ %b/%stcp-request inspect-delay 3sacl is_https req.payload(0,3) -m bin 160301#GET POS(T) PUT DEL(ETE) OPT(IONS) HEA(D) CON(NECT) TRA(CE) acl is_http req.payload(0,3) -m bin 474554 504f53 505554 44454c 4f5054 484541 434f4e 545241#SSHacl is_ssh req.payload(0,3) -m bin 535348tcp-request content accept if is_httptcp-request content accept if is_https#tcp-request content accept if is_sshtcp-request content accept use_backend https if is_httpsuse_backend http if is_http#use_backend ssh if is_sshuse_backend ssh backend sshmode tcp#server ssh01 127.0.0.1:22 maxconn 10 check inter 3stimeout server 1hserver server-ssh :22backend httpmode tcpserver ngx01 127.0.0.1:82 maxconn 10 check inter 3sbackend httpsmode tcpserver ngx02 127.0.0.1:433 maxconn 10 check inter 3s



0 0
原创粉丝点击