บันทึกนี้ปรับปรุงล่าสุดเมื่อวันที่ 4-10-2561
ดูแลโดย WIBOON
ขั้นตอน
1. ติดตั้งโปรแกรม haproxy
sudo apt install haproxy
2. ตั้งค่าโดยการแก้ไขไฟล์ /etc/default/haproxy ด้วยเอดิเตอร์ vi หรือ nano ก็ได้
sudo nano /etc/default/haproxy
เพิ่มบรรทัดนี้ต่อท้ายไฟล์
ENABLED=1
3. restart service
sudo systemctl restart haproxy sudo systemctl status haproxy
4. เก็บสำรองไฟล์ haproxy.cfg ไว้ด้วย
cd /etc/haproxy; sudo cp haproxy.cfg haproxy.cfg.orig
5. ตัวอย่าง การตั้งค่าเพื่อเปิด http สำหรับ tblog1.example.com
5.1 ที่ DNS Server เพิ่ม domain name ในไฟล์ /var/cache/bind/db.example.com
ip251.example.com. IN A 10.0.100.251 tblog1.example.com. IN CNAME ip251.example.com.
5.2 แก้ไข haproxy config file สำหรับ http://tblog1.example.com
sudo nano /etc/haproxy/haproxy.cfg
เพิ่มบรรทัดข้างล่างนี้
frontend www-http bind *:80 mode http acl host_tblog1 hdr_beg(host) -i tblog1 use_backend http-tblog1-backend if host_tblog1 backend http-tblog1-backend option forwardfor server tblog1 10.25.51.155:80
5.3 รีสตาร์ท haproxy
sudo systemctl restart haproxy.service sudo systemctl status haproxy.service tail -f /var/log/haproxy.log
5.4 ทดสอบ http://tblog1.example.com
6. ต่อไปเป็น ตัวอย่างการตั้งค่าเพื่อเปิด https สำหรับ tblog1.example.com
6.1 สร้างไฟล์ my.pem สำหรับเปิด https ใน haproxy.conf
sudo su - cd /etc/ssl/private/ cat ../certs/ssl-cert-snakeoil.pem ssl-cert-snakeoil.key > my.pem chgrp ssl-cert my.pem exit
6.2 แก้ไข haproxy config file สำหรับ https://tblog1.example.com
sudo nano /etc/haproxy/haproxy.cfg
เพิ่มบรรทัดข้างล่างนี้
frontend www-https bind *:443 ssl crt /etc/ssl/private/my.pem mode http acl host_tblog1 hdr_beg(host) -i tblog1 use_backend https-tblog1-backend if host_tblog1 backend https-tblog1-backend option forwardfor http-request set-header X-Forwarded-Port %[dst_port] http-request add-header X-Forwarded-Proto https if { ssl_fc } server tblog1 10.25.51.155:80
6.3 รีสตาร์ท haproxy
sudo systemctl restart haproxy.service sudo systemctl status haproxy.service tail -f /var/log/haproxy.log
6.4 ทดสอบ
เข้า browser เพื่อไปยัง https://tblog1.example.com
หมายเหตุ
เพิ่มบรรทัดนี้ใน "Global" section ของ /etc/haproxy/haproxy.cfg
tune.ssl.default-dh-param 2048
จะทำให้ warning ข้างล่างนี้ หายไป
Oct 4 13:25:36 ubuntu haproxy[4636]: [WARNING] 276/132536 (4636) : Setting tune.ssl.default-dh-param to 1024 by default, if your workload permits it you should set it to at least 2048. Please set a value >= 1024 to make this warning disappear.