원문 보기: https://dawoum.duckdns.org/wiki/HAProxy
HAProxy는 고-가용성 로드 밸런서 및 여러 서버에 걸쳐 요청을 분산시키는 TCP 및 HTTP-기반 응용 프로그램에 대해 프록시 서버를 제공하는 자유와 오픈-소스 소프트웨어입니다. 그것은 C로 작성되었고 프로세서 및 메모리 사용 측면에서 빠르고 효율적이라는 평판을 얻고 있습니다.
HAProxy는 GoDaddy, GitHub, Bitbucket, Stack Overflow, Reddit, Slack, Speedtest.net, Tumblr, Twitter 및 Tuenti를 포함하는 많은 유명 웹-사이트에서 사용되고 Amazon Web Services의 OpsWorks 제품에 사용됩니다.
Introduction
이 프로그램을 설치하게 된 동기는 Let's Encrypt를 설치해서 이 서버(다움 위키)를 HTTPS로 서비스하면서 시작되었습니다. 스퀴드를 거쳐 바니쉬를 http 가속기를 이용하고 있었지만, 스퀴드는 위키미디어 재단에서 더 이상 사용하고 있지 않기 때문에, 그 설정에 대한 자료를 찾는 것이 쉽지 않습니다. 이것저것을 통해, https에서 동작하도록 설정을 했지만, 과연 이것이 가속을 하고 있는지 확인하는 툴마저 찾기가 쉽지 않았습니다.
한편, 위키미디어 재단에서 현재 사용하고 있는 것으로 보이는 바니쉬는 https에 대해 지원하지 않습니다. 따라서 외부에서 HTTPS 접근할 때, 이것을 적절히 처리해서 바니쉬의 가속 능력을 이용하고 싶습니다. 처음에는 엔진엑스를 통해 SSL 리다이렉션을 시도했지만, 동작을 하는 듯 보이지만, 결국 제대로 된 설정을 할 수 없었습니다.
결국 HAProxy를 통해서 이 작업을 완료할 수 있었습니다.
Installation
데비안 저장소에서 설치할 수 있습니다:
- sudo apt install haproxy
PPA
보다 최신의 패키지는 아래에서 찾을 수 있습니다:
Configuration
설정은 HAproxy, 바니쉬, 엔진엑스에 걸쳐 수정을 해야 합니다.
먼저, HAproxy는 80번과 443번 포트를 전부 바인딩할 수 있지만, 만약 HAproxy에서 80번 바인딩을 이용을 하면, Parsoid를 통해 RESTBase로 접근이 되지 않는 문제가 생깁니다. 이럴 경우에, 전적으로 수식 처리를 외부 서버를 이용해야 합니다. 설정으로 이것이 가능한지 알려주세요!!
어쨌든, 80번 포트는 여전히 바니쉬에서 열어서 엔진엑스를 백엔드로 이용하면, Parsoid와 RESTBase가 이상없이 모든 서비스를 수행할 수 있습니다.
HAproxy
먼저, /etc/haproxy/haproxy.cfg는 다음과 같이 설정합니다.
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3 no-tlsv10
tune.ssl.default-dh-param 4096
defaults
log global
mode http
option httplog
option dontlognull
option forwardfor
http-reuse always
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend FRONTEND_NAME
# bind *:80
bind *:443 ssl crt /etc/ssl/private/dawoum.duckdns.org.pem
acl secure dst_port eq 443
redirect scheme https if !{ ssl_fc }
rspadd Strict-Transport-Security:\ max-age=31536000;\ includeSubDomains;\ preload
rsprep ^Set-Cookie:\ (.*) Set-Cookie:\ \1;\ Secure if secure
default_backend BACKEND_NAME
backend BACKEND_NAME
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
server vpsieprod 127.0.0.1:6081
FRONT_NAME, BACKEND_NAME의 이름을 원하는 것으로 바꾸고, dawoum.duckdns.org.pem 파일은 다음과 같이 만듭니다.
- cat privkey.pem fullchain.pem > dawoum.duckdns.org.pem
여기서 앞의 두 키는 Let's Encrypt에서 만들어진 것입니다. 이것도 자동으로 이루어지도록 크론을 이용해서 수행하는 것이 좋겠습니다.
Varnish
두 개의 파일을 수정합니다.
먼저, /etc/default/varnish를 수정합니다.
AEMON_OPTS="-a :80 \
-a 127.0.0.1:6081 \
-f /etc/varnish/default.vcl \
-T 127.0.0.1:6082 \
...
다음으로 systemd 스크립트, /lib/systemd/system/varnish.service를 수정합니다.
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -a localhost:6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256M
위의 두 파일에서는 포트 번호를 맞추어야 합니다.
마지막으로 /etc/varnish/default.vcl을 수정합니다. 이전 설정을 이용해도 됩니다. 꼭 바꾸실 필요는 없습니다.
if (req.http.host == "(dawoum|brajo|mowoum).duckdns.org") {
return (pipe);
}
set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
# set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
# set req.backend_hint= default;
이전 설정들을 확인하셔서 다른 부분을 수정하십시오.
Nginx
엔진엑스에서는 기존의 ssl 설정을 전부 걷어냅니다.
Service restart
모든 설정을 수정했으면, 다음을 수행합니다.
- sudo systemctl daemon-reload
- sudo systemctl restart haproxy varnish nginx
Features
HAProxy has the following features:
- Layer 4 (TCP) and Layer 7 (HTTP) load balancing
- URL rewriting
- Rate limiting
- SSL/TLS termination
- Gzip compression
- Proxy Protocol support
- Health checking
- Connection and HTTP message logging
- HTTP/2
- Multithreading
- Hitless Reloads
- gRPC Support
- Lua and SPOE Support
- API Support
- Layer 4 Retries
- Simplified circuit breaking
Performance
듀얼 코어 Opteron 또는 Xeon 프로세서가 장착된 서버는 일반적으로 초당 15000~40000 히트를 달성하고, 리눅스에서 2Gbit/sec 연결을 포화시키는 데 문제가 없습니다.
Troubleshootings
Systemd로 시작, 또는 재시작 안됨
주 버전이나 보조 버전이 바뀌면, 이전 설정이 동작하지 않을 수 있습니다. 이때, systemctl status 또는 journalctl -xe의 메시지가 불명확하여 오류를 수정하기 힘들 때에는, 다음과 같이 실행해서 오류가 발생하는 원인이 무엇인지 확인할 수 있습니다:
- sudo haproxy -f /etc/haproxy/haproxy.cfg -d
다음 메시지가 나옵니다.
stats socket will not work as expected in multi-process mode (nbproc > 1), you should force process binding globally using 'stats bind-process' or per socket using the 'process' attribute. 설정 파일에 다음을 추가하십시오.
stats bind-process 1
External links
댓글
댓글 쓰기