我們實驗室在學校裡面有一台伺服器,帶一個公網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 check
frontend main
mode tcp
bind *:80
log global
option tcplog
log-format %ft\ %b/%s
tcp-request inspect-delay 3s
acl 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
#SSH
acl is_ssh req.payload(0,3) -m bin 535348
tcp-request content accept if is_http
tcp-request content accept if is_https
#tcp-request content accept if is_ssh
tcp-request content accept
use_backend https if is_https
use_backend http if is_http
#use_backend ssh if is_ssh
use_backend ssh
backend ssh
mode tcp
#server ssh01 127.0.0.1:22 maxconn 10 check inter 3s
timeout server 1h
server server-ssh :22
backend http
mode tcp
server ngx01 127.0.0.1:82 maxconn 10 check inter 3s
backend https
mode tcp
server ngx02 127.0.0.1:433 maxconn 10 check inter 3s