3

haproxy一个端口跑多个服务

 2 years ago
source link: https://bajie.dev/posts/20211110-haproxy_multiple_port/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Haproxy一个端口跑多个服务

2021-11-10 1 分钟阅读

我们选择 haproxy 1.8 版本以上的,编译安装到路径 /export/servers/haproxy

make TARGET=linux2628 PREFIX=/export/servers/haproxy USE_GETADDRINFO=1 USE_ZLIB=1 USE_REGPARM=1 USE_OPENSSL=1 \
  USE_SYSTEMD=1 USE_PCRE=1 USE_PCRE_JIT=1 USE_NS=1

make install PREFIX=/export/servers/haproxy

编辑 haproxy.conf 配置文件:

global 
  maxconn 5120  
  chroot /export/servers/haproxy   
  daemon 
  quiet 
  nbproc 2 
  pidfile /tmp/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

解释一下:我们在 8080 端口开了 http 服务,在 22 端口开了 ssh 服务,80端口由 haproxy 做代理转发,首先判断客户端请求是否是 http 请求,如果是就转发到 8080 端口,如果不是,就转发到 22 端口,这样就实现了 80 端口同时跑 http 和 ssh 两个服务。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK