Installing HAProxy 1.7 on Debian 9.1 (Stretch)

HAProxy is a network software application that offers high availability, load balancing, and proxying for TCP and HTTP network applications. It is suited for high traffic websites, and powers many popular sites across the web. This article will show you how to install and configure HAProxy on Debian 9.1.

Although HAProxy has several prominent features, this article focuses on how to setup HAProxy to "proxy" your web application.

Requirements

  • At least two Vultr servers (for load balancing functionality) with your website or web application deployed to both of them.

Installing HAProxy

Debian 9 already ships with HAProxy 1.7 (latest stable release at time of writing), and we can simply install it using apt-get:

# apt-get update
# apt-get install haproxy

If the previous commands were successful, then you have installed HAProxy and you can proceed to the next step.

Configuring HAProxy

The HAProxy configuration file is split up into two sections — "global" and "proxies". One deals with process-wide configuration, while the latter consists of default configuration, frontend, and backend sections.

Global Section

Using your favorite text editor, open /etc/haproxy/haproxy.cfg and notice the predefined sections: "global" and "defaults". The first thing that you may want to do is increase the maxconn to a reasonable size, as this affects the connections that HAProxy allows. Too many connections may cause your web service to crash due to many requests. You will need to adjust the size to see what works for you. In the global section, we have chosen a maxconn value of 3072.

global
    daemon
    maxconn 3072

In the default section, add the following line under mode http:

option forwardfor

This will add X-Forwarded-For headers to each request, which allows your backend servers to learn the original IP address of the user.

Also, add this line to enable HTTP connection-close mode on the server side while keeping the ability to support HTTP keep-alive on the client side. This reduces latency on the client side and helps conserve server resources:

option http-server-close

If you wish to use keep-alive on both the client and server sides, then you could use option http-keep-alive instead. This option is particularly useful when the cost of establishing a new connection to the server is significant compared to the cost of retrieving the requested resource.

Finally, the resulting config file will look something like this:

defaults
    mode http
    option forwardfor
    option http-server-close
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

Proxies Section

To set up your proxy, you will need to add two sections to the configuration file to define the two parts of the proxy: the frontend and the backend.

Frontend Configuration

The frontend will handle your HTTP connections. Add the following to the end of your haproxy.cfg file:

frontend http-frontend
    bind public_ip:80
    reqadd X-Forwarded-Proto:\ http
    default_backend wwwbackend

Be sure to replace public_ip with your server's public IP address or domain name.

Backend Configuration

Setup your backend by adding the following lines to the end of your configuration file:

backend wwwbackend
    server 1-www server1_ip:80 check
    server 2-www server2_ip:80 check
    server 3-www server3_ip:80 check

The backend configuration used here creates 3 connections named X-www. (X is 1, 2 or 3.) Each one of them corresponds to a serverX_ip:80 address. (Replace serverX_ip with your Vultr instances' IP addresses.) This will allow you to load balance between each server in the specified server set (assuming each IP address corresponds to a different server). The check option makes the load balancer perform health checks on the server.

Save the configuration file, and then restart HAProxy:

service haproxy restart

If everything is working, then you will be able to connect to http://public_ip/ (replacing it with your public IP or domain name as configured in the frontend step) and view your website.

Debugging Errors

If your HAProxy instance refuses to start after your modifications, chances are that you have an error somewhere in the configuration file. To get clear messages about the issue in the configuration file, you can try to start HAProxy manually using this command:

# haproxy -f /etc/haproxy/haproxy.cfg

For instance, if you see output like this:

[ALERT] 234/195612 (2561) : parsing [/etc/haproxy/haproxy.cfg:48] : server 1-www has neither service port nor check port nor tcp_check rule 'connect' with port information. Check has been disabled.
[ALERT] 234/195612 (2561) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg
[ALERT] 234/195612 (2561) : Fatal errors found in configuration.

Then, you have forgotten to specify the port number for the server 1-www.

Αφήστε ένα σχόλιο

The Rise of Machines: Real World Applications of AI

The Rise of Machines: Real World Applications of AI

Η Τεχνητή Νοημοσύνη δεν είναι στο μέλλον, είναι εδώ ακριβώς στο παρόν Σε αυτό το blog Διαβάστε πώς οι εφαρμογές Τεχνητής Νοημοσύνης έχουν επηρεάσει διάφορους τομείς.

Επιθέσεις DDOS: Μια σύντομη επισκόπηση

Επιθέσεις DDOS: Μια σύντομη επισκόπηση

Είστε επίσης θύμα των επιθέσεων DDOS και έχετε μπερδευτεί σχετικά με τις μεθόδους πρόληψης; Διαβάστε αυτό το άρθρο για να λύσετε τις απορίες σας.

Έχετε αναρωτηθεί ποτέ πώς κερδίζουν χρήματα οι χάκερ;

Έχετε αναρωτηθεί ποτέ πώς κερδίζουν χρήματα οι χάκερ;

Μπορεί να έχετε ακούσει ότι οι χάκερ κερδίζουν πολλά χρήματα, αλλά έχετε αναρωτηθεί ποτέ πώς κερδίζουν τέτοια χρήματα; ας συζητήσουμε.

Επαναστατικές εφευρέσεις από την Google που θα σας φέρουν εύκολη στη ζωή.

Επαναστατικές εφευρέσεις από την Google που θα σας φέρουν εύκολη στη ζωή.

Θέλετε να δείτε επαναστατικές εφευρέσεις της Google και πώς αυτές οι εφευρέσεις άλλαξαν τη ζωή κάθε ανθρώπου σήμερα; Στη συνέχεια, διαβάστε στο blog για να δείτε τις εφευρέσεις της Google.

Friday Essential: Τι συνέβη με τα αυτοκίνητα που κινούνται με AI;

Friday Essential: Τι συνέβη με τα αυτοκίνητα που κινούνται με AI;

Η ιδέα των αυτοκινούμενων αυτοκινήτων να βγαίνουν στους δρόμους με τη βοήθεια της τεχνητής νοημοσύνης είναι ένα όνειρο που έχουμε εδώ και καιρό. Όμως, παρά τις πολλές υποσχέσεις, δεν φαίνονται πουθενά. Διαβάστε αυτό το blog για να μάθετε περισσότερα…

Τεχνολογική μοναδικότητα: Ένα μακρινό μέλλον του ανθρώπινου πολιτισμού;

Τεχνολογική μοναδικότητα: Ένα μακρινό μέλλον του ανθρώπινου πολιτισμού;

Καθώς η Επιστήμη εξελίσσεται με γρήγορους ρυθμούς, αναλαμβάνοντας πολλές από τις προσπάθειές μας, αυξάνονται και οι κίνδυνοι να υποβάλλουμε τον εαυτό μας σε μια ανεξήγητη Ιδιότητα. Διαβάστε τι σημαίνει η μοναδικότητα για εμάς.

Λειτουργίες Αρχιτεκτονικών Επιπέδων Αναφοράς Μεγάλων Δεδομένων

Λειτουργίες Αρχιτεκτονικών Επιπέδων Αναφοράς Μεγάλων Δεδομένων

Διαβάστε το ιστολόγιο για να μάθετε διαφορετικά επίπεδα στην Αρχιτεκτονική Μεγάλων Δεδομένων και τις λειτουργίες τους με τον απλούστερο τρόπο.

Εξέλιξη της αποθήκευσης δεδομένων – Infographic

Εξέλιξη της αποθήκευσης δεδομένων – Infographic

Οι μέθοδοι αποθήκευσης των δεδομένων εξελίσσονται μπορεί να είναι από τη γέννηση των Δεδομένων. Αυτό το ιστολόγιο καλύπτει την εξέλιξη της αποθήκευσης δεδομένων με βάση ένα infographic.

6 εκπληκτικά οφέλη από την ύπαρξη έξυπνων οικιακών συσκευών στη ζωή μας

6 εκπληκτικά οφέλη από την ύπαρξη έξυπνων οικιακών συσκευών στη ζωή μας

Σε αυτόν τον ψηφιακό κόσμο, οι έξυπνες οικιακές συσκευές έχουν γίνει κρίσιμο μέρος της ζωής. Ακολουθούν μερικά εκπληκτικά οφέλη των έξυπνων οικιακών συσκευών σχετικά με το πώς κάνουν τη ζωή μας να αξίζει και να απλοποιείται.

Η ενημέρωση του συμπληρώματος macOS Catalina 10.15.4 προκαλεί περισσότερα προβλήματα από την επίλυση

Η ενημέρωση του συμπληρώματος macOS Catalina 10.15.4 προκαλεί περισσότερα προβλήματα από την επίλυση

Πρόσφατα, η Apple κυκλοφόρησε το macOS Catalina 10.15.4 μια συμπληρωματική ενημέρωση για την επιδιόρθωση προβλημάτων, αλλά φαίνεται ότι η ενημέρωση προκαλεί περισσότερα προβλήματα που οδηγούν σε bricking των μηχανών mac. Διαβάστε αυτό το άρθρο για να μάθετε περισσότερα