WebSocket(node + socket.io)ãstunnel + HAProxyã§ãããã·ãã - ãã®2 - HAProxyã®è¨å®
Nginxã¨node.jsãHAProxy+stunnelã§ã¾ã¨ããæ¹æ³ã«ã¤ãã¦ã¾ã¨ãã¦ãã¾ãã
ãµã¼ãã¼ã®æ§æã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
ååã®è¨äºã¯HAProxyã®ã¤ã³ã¹ãã¼ã«ã¾ã§ç´¹ä»ãã¾ããã
http://d.hatena.ne.jp/hrendoh/20120328/1332917793
ä»åã¯ãHAProxyã§Nginxã¨node.jsã®ãµã¼ãã¼ã«æ¯ãåããè¨å®æ¹æ³ã¨ãå種ãã©ã¡ã¼ã¿ã«ã¤ãã¦å®éã®è¨å®ä¾ãåºã«ã¾ã¨ãã¦ã¿ã¾ãã
è¨å®ã®ãªãã¡ã¬ã³ã¹ã¯ãå ¬å¼ãµã¤ãã®Documentãã¼ã¸ã«ãªã³ã¯ãããããã¹ãã®ãã®ã¨ãGoogle codeã«ãåãå 容ã®ãã®ãã¾ã¨ã¾ã£ã¦ãã¾ãã
Google codeã®ããã¥ã¡ã³ãã®æ¹ãè¦ãããã®ã§ãè¦ãã§ãã
haproxy.cfgã®è¨å®
HAProxyã®è¨å®ã¯ /etc/haproxy/haproxy.cfg ã«è¨è¿°ãã¾ãã
ä»åã®ä¾ã¯ã以ä¸ã®ããã«ãªãã¾ãã
global maxconn 4096 daemon nproc 1 log 127.0.0.1 local1 debug defaults mode http option httplog log global frontend all 0.0.0.0:80 timeout client 86400000 default_backend www_backend acl is_websocket hdr(Upgrade) -i WebSocket acl is_websocket path_beg /socket.io/ use_backend socket_backend if is_websocket backend www_backend balance roundrobin option forwardfor # This sets X-Forwarded-For option httpclose timeout server 30000 timeout connect 4000 server server1 localhost:81 weight 1 maxconn 1024 check backend socket_backend balance roundrobin option forwardfor # This sets X-Forwarded-For option httpclose timeout queue 5000 timeout server 86400000 timeout connect 86400000 server server1 localhost:3000 weight 1 maxconn 1024 check
以ä¸ããããè¸ã¾ãã¦haproxy.confã®è¨å®ã«ã¤ãã¦èª¬æãã¦ããã¾ãã
è¨å®ãã¡ã¤ã«ã®æ§æ
ã¾ãã"global"ã»ã¯ã·ã§ã³ã«ãHAProxyããã»ã¹å ¨ä½ã§å ±éã®è¨å®ãè¨è¿°ããç¶ãã¦ã以ä¸ã®4ã¤ã®ãããã·è¨å®ã»ã¯ã·ã§ã³ã«ããªãã¹ã³ããåãã¼ãã®è¨å®ãããã¯ã¨ã³ããµã¼ãã¼ã¸ã®æ¯ãåãè¨å®ãªã©ãè¨è¿°ãã¦ããã¾ãã
- defaults: å¾è¿°ããfrontendãbackendãlistenã®ããã©ã«ããã©ã¡ã¼ã¿ãæå®ãã¾ãã
- frontend: ã¯ã©ã¤ã¢ã³ãããã®æ¥ç¶ãåãå ¥ããã½ã±ããã®ãã©ã¡ã¼ã¿ãæå®ãã¾ãã
- backend: ãããã·ãããµã¼ãã¼ã«é¢ãããã©ã¡ã¼ã¿ãæå®ãã¾ãã
- listen: frontendã¨backendãå®å ¨ã«ãããã·ããè¨å®ãè¨è¿°ãã¾ãã
ããã»ã¹è¨å®
è¨å®ä¾ã®"global"ã»ã¯ã·ã§ã³ã«è¨è¿°ããã¦ããåãã©ã¡ã¼ã¿ã¯ä»¥ä¸ã®éãã§ãã
ãããã·è¨å®
ãããã·ã»ã¯ã·ã§ã³ã«è¨å®ãããã©ã¡ã¼ã¿ã¯ã"defaults"ã"frontend"ã"backend"ã"listen"ã®ãã¥ãã®ã»ã¯ã·ã§ã³ã§ã使ç¨å¯è½ãªãã®ã¨ãå°ç¨ã®ãã®ãããã¾ãã
åãã©ã¡ã¼ã¿ã¨å©ç¨å¯è½ãªã»ã¯ã·ã§ã³ã®ä¸è¦§ãããã¥ã¡ã³ãã®ãProxy keywords matrixãã«ããã¾ãã
以ä¸ã大ã¾ãã«åãã¦ãã使ããã©ã¡ã¼ã¿ã«ã¤ãã¦èª¬æãã¦ãã¾ãã
Keep-Aliveã®è¨å®(ãã¹ã¦ã®ã»ã¯ã·ã§ã³)
- option httpclose: åãªã¯ã¨ã¹ãã§HTTPãããã¼"Connection: close"ããã§ãã¯ãããªããã°ã¬ã¹ãã³ã¹ãããã«è¿½å ãã¾ããã¤ã¾ããKeep-Aliveãç¡å¹ã«ãªãã¾ããHAProxyã¯ãããã©ã«ãã§ã¯HTTPæ¥ç¶ã®æåã®ãªã¯ã¨ã¹ãã®ã¿æ¤æ»ãããããHTTPãããã¼ã®æä½ãããããã¼ããã©ã¡ã¼ã¿ã«ãã£ã¦æ¯ãåããè¡ãå ´åã¯"httpclose"ãªãã·ã§ã³ãå¿ è¦ã«ãªãã¾ãã
- option http-server-close: ä¸è¨ã®"httpclose"ã¨ã®éãã¯ãã¯ã©ã¤ã¢ã³ãå´ã¯Keep-Aliveã®ã¾ã¾ããµã¼ãã¼å´ã®ã¿ãªã¯ã¨ã¹ããã¨ã«æ¥ç¶ãã¯ãã¼ãºããã¾ãã
æ¥ç¶ã¢ã¼ã(ãã¹ã¦ã®ã»ã¯ã·ã§ã³)
- mode: ãªãã¹ã³ãããããã³ã«ãæå®ãã¾ã
- tcp: TCPã¢ã¼ãã¯ãã¯ã©ã¤ã¢ã³ã-ãµã¼ãã¼éã®å ¨äºéæ¥ç¶ã確ç«ãã¾ãã ã¬ã¤ã¤ã¼7ã®ãã±ããã®å¦çãããªããSSL, SSH, SMTPãªã©ããããã·ããå ´åã«å©ç¨ã§ãã¾ãã
- http: HTTPã¢ã¼ããRFCã«æºæ ããªããªã¯ã¨ã¹ãã¯ç ´æ£ããã¾ããã¬ã¤ã¤ã¼7ã®æ å ±ã«åºã¥ããµã¼ãã¼æ¯ãåããªã©ãå¯è½ã«ãªãã¾ãã
- health: ã¢ãã¿ã¼ç¨ã®ã¢ã¼ãã§ãããç¾å¨éæ¨å¥¨ãªã®ã§ä½¿ç¨ãã¾ããã
backendã®é¸æ(defaults, frontend, listen)
- default_backup: ãããããuse_backendãç¡ãã£ãå ´åã«ä½¿ç¨ãããbackendãæå®ãã¾ãã
- use_backend:
ãã¼ããã©ã³ã¹(backend)
- balance: ãã¼ããã©ã³ã¹ã«ä½¿ç¨ããã¢ã«ã´ãªãºã ãæå®ãã¾ããHAProxyã¯é常ã«å¼·åãªãã¼ããã©ã³ã¹æ©è½ãæä¾ãã¦ãã¾ããæå®ããªãã£ãå ´åã®ããã©ã«ãã¯roundrobinã§ãã
- roundrobin: ã©ã¦ã³ãããã³ãéã¿ä»ãã®æ©è½ãããåç調æ´ãã(ãµã¼ãã¼ãã¦ã³ããå復ç´å¾ã«å¤§éãªã¯ã¨ã¹ããå²ãå²ãå½ã¦ãããªãããã«èª¿æ´ãã¾ã)ãåbackendã«ã¤ãã¦æ大4128ã®ã¢ã¯ãã£ããµã¼ãã¼ãæå®å¯è½ã
- static-rr: ã©ã¦ã³ãããã³ãéã¿ä»ãã®æ©è½ãããåç調æ´ãªããbackendãµã¼ãã¼ã®å¶éç¡ãã
- leastconn: ãã£ã¨ãæ¥ç¶æ°ãå°ãªããµã¼ãã¼ãå²ãå½ã¦ããã¾ãã
- source: IP Addressã®ããã·ã¥å¤ãå ã«æ¯ãåãã
- uri: URIã®'?'ã®åãããã·ã¥ãããã®å¤ãå
ã«æ¯ãåããã¤ã¾ãããµã¼ãã¼ãåæ¢/èµ·åãããªãéãã¯ãURIã®å¤ãåããªãåããµã¼ãã¼ã«æ¯ãåãããããããã·ã¥ããURIã®æ·±ããé·ããªã©è©³ç´°ãªè¨å®ãå¯è½ã balance uri [len
] [depth ] - url_param: URIã®ãã©ã¡ã¼ã¿ã«ããæ¯ãåããPOSTã®å ´åãcheck_postãæå®ããã¨POSTã®ããã£ã使ç¨ããã balance url_param [check_post [
]] - hdr(name): HTTPãããã¼ã«ããæ¯ãåã
- rdp-cookie, rdp-cookie(name): RDPã¯ããã¼ãããã·ã¥ããå¤ã«ããæ¯ãåããnameãæå®ããªãå ´åã¯"mstshash"ã使ç¨ãããã
- option forwardfor: backendãµã¼ãã¼ã¸éããããªã¯ã¨ã¹ãã«"X-Forwarded-For"ãããã¼ã追å ãã¾ããHAPorxyã¯ãªãã¼ã¹ãããã·ã¨ãã¦åä½ãããããåbackendãµã¼ãã¼ã¯HAProxyãµã¼ãã¼ã®IPã¢ãã¬ã¹ãã¯ã©ã¤ã¢ã³ãã¢ãã¬ã¹ã¨ãã¦èªèãã¦ãã¾ãã¾ããããã解決ããããã«æ¬ãªãã·ã§ã³ãå©ç¨ãã¾ãã
ã¿ã¤ã ã¢ã¦ã
- timeout client: ã¯ã©ã¤ã¢ã³ãã¨ã®æ¥ç¶ã¢ã¤ãã«ã¿ã¤ã ã¢ã¦ãæéãè¤éæ§ãæé¤ããããã«ã¯"timeout server"ã¨åãå¤ãæ¨å¥¨ããããfrontendå´ã§è¨å®
ä»ã«"timeout http-keep-alive"ã"timeout http-request"ãªã©ã®ãã©ã¡ã¼ã¿ããã
- timeout queue: æ¥ç¶æ°ãmaxconnã«éããå ´åããªã¯ã¨ã¹ãã¯ãã¥ã¼ã«è²¯ãããã¾ãããã®å¾ããã®å¤ã®æéãçµéããå ´å503ãè¿ããã¾ããæå®ããªãã£ãå ´åã¯"timeout connect"ã¨åãå¤ã使ç¨ããã¾ããbackendç¨
- timeout connection: backendãµã¼ãã¼ã«å¯¾ããæ¥ç¶ã確ç«ããã¾ã§ã®ã¿ã¤ã ã¢ã¦ãæéãæå®ããªãå ´åã¯ãç¡å¶éãbackendç¨
- timeout server: backendãµã¼ãã¼ã¸ã®æ¥ç¶ã®ã¢ã¤ãã«ã¿ã¤ã ã¢ã¦ãæéãbackendç¨
ACL
Access Control Lists (ACL)ã¯ãHAProxyã®èã¨ãªãéè¦ãªæ©è½ã§ãããã«ãã£ã¦æè»ãªãµã¼ãã¼æ§æãå¯è½ã«ãªãã¾ãã
ACLã®æå®ãã©ã¼ãããã¯ä»¥ä¸ã®ããã«ãªãã¾ã:
acl[flags] [operator] ...
ä»åã®ä¾ã§ã¯ã"Upgrade"ãããã¼ã«"WebSocket"ã¨ããå¤ãå«ã¾ãã¦ããå ´åã«is_websocketãtrueã«ãã¾ãã
acl is_websocket hdr(Upgrade) -i WebSocket
以ä¸ã®ããã°ãåèã«ãªãã¾ã:
HAProxyのACLとCriteria
ãã°åºå
è¨å®ä¾ããããã°åºåã«é¢ããç®æã ãæãåºãã¾ããã
global ... log 127.0.0.1 local1 debug defaults ... log global ...
logãã©ã¡ã¼ã¿ã®è¨å®ã¯ä»¥ä¸ã®ãã©ã¼ãããã§è¨è¿°ãã¾ãã
log <address> <facility> <level>
- address: ãã°ãéä¿¡ããIPã¢ãã¬ã¹ãæå®ãã¾ããè¨å®ä¾ã§ã¯ãã¼ã«ã«ã®rsyslogãã¼ã¢ã³ã«ãã°ãéä¿¡ãã¾ããIPv4ã¢ãã¬ã¹ãæå®ããå ´åã¯ãUDPã½ã±ããã§ãã°ãéä¿¡ãããã®ã§rsyslogå´ã§åããããããã«è¨å®ããå¿ è¦ãããã¾ããã¾ããUnixã½ã±ããã使ç¨ãããã¨ãã§ãã¾ãã
- facility: syslogã®ãã¡ã·ãªãã£ãæå®ãã¾ãã
- level: rsyslogdã«éä¿¡ãããã°ã¬ãã« "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" ãæå®ãã¾ããçç¥ããå ´åã¯ããã¹ã¦ã®ãã°ãéä¿¡ããã¾ãã
ãããã·ã»ã¯ã·ã§ã³ã§"log global"ã¨è¨è¿°ããå ´åã¯"global"ã»ã¯ã·ã§ã³ã®è¨å®ãå¼ãç¶ããã¾ãã
Ubuntu 10.04ã§ã®ãã°è¨å®ã以ä¸ã®ããã°ãåèã«ã¾ã¨ãã¦è¦ã¾ãã
HAProxy Logging in Ubuntu Lucid
$ sudo vi /etc/rsyslog.d/haproxy.conf
# .. otherwise consider putting these two in /etc/rsyslog.conf instead: $ModLoad imudp $UDPServerRun 514 $UDPServerAddress 127.0.0.1 # ..and in any case, put these two in /etc/rsyslog.d/haproxy.conf: local1.* -/var/log/haproxy_1.log & ~ # & ~ means not to put what matched in the above line anywhere else for the rest of the rules # http://serverfault.com/questions/214312/how-to-keep-haproxy-log-messages-out-of-var-log-syslog
$ sudo service rsyslog restart
ãã¼ãã空ããå¿ è¦ãããã®ã§ãreloadãããªãã¦restart
ãã°åºåã®ç¢ºèª
$ tail -f /var/log/haproxy_1.log Apr 20 15:15:54 localhost.localdomain haproxy[11220]: Connect from 127.0.0.1:57720 to 127.0.0.1:80 (all/HTTP) Apr 20 15:15:54 localhost.localdomain haproxy[11220]: Connect from 127.0.0.1:57721 to 127.0.0.1:80 (all/HTTP) Apr 20 15:15:54 localhost.localdomain haproxy[11220]: Connect from 127.0.0.1:57722 to 127.0.0.1:80 (all/HTTP) ...
ãã¼ãã¼ã·ã§ã³ãè¨å®ãã¦ããã¾ã
$ sudo vi /etc/logrotate.d/haproxy
/var/log/haproxy*.log { weekly missingok rotate 52 compress delaycompress notifempty create 640 root adm sharedscripts postrotate reload rsyslog >/dev/null 2>&1 || true endscript }
以ä¸ã大ã¾ãã«HAProxyã®è¨å®ã«ã¤ãã¦èª¬æãã¾ããã
次åã¯stunnelã®ã»ããã¢ããã«ã¤ãã¦èª¬æãã¾ãã