search
HomeComputer TutorialsComputer KnowledgeHow to configure HAProxy for WebSocket connections

WebSocket applications enable real-time two-way communication between client and server. Even though WebSocket connections are used, effective traffic management mechanisms are still required to avoid overloading the server, resulting in service interruption and unavailability.

HAProxy is a free and reliable load balancer that can also be used as a reverse proxy. By configuring HAProxy to support WebSocket connections, you can better utilize the real-time data transmission characteristics of WebSockets and reduce the server load. This article will describe the detailed steps to configure HAProxy to support WebSocket connections.

Step-by-step guide on how to configure HAProxy for WebSocket connections

Through the WebSocket connection, the communication between the server and the client is persistent until one of the parties actively closes the connection. To ensure that traffic can be evenly distributed to other servers when the server load is too high, a load balancer approach can be used. In this way, the client and server can maintain a continuous connection and enjoy a low-latency communication environment.

1. Install HAProxy

The first step in configuring HAProxy's WebSocket connection is to confirm that HAProxy is installed. If it is not installed yet, follow these steps to install it:

$sudo apt—get install haproxy

How to configure HAProxy for WebSocket connections

2. Edit HAProxy configuration

In order for WebSocket connections to pass through HAProxy, the HAProxy configuration needs to be adjusted to support WebSocket connections. This includes making changes to the "server" section and setting up the "frontend" and "backend" sections.

Use a text editor to open the HAProxy configuration as shown below:

$sudo nano/etc/haproxy/haproxy.cfg

In the "Delete" section, modify it as shown in the image below. First, we specify that we are using an HTTP connection and define where to send the log files. Next, we set timeouts for different activities. For example, a connection timeout is the maximum amount of time it takes to connect to a server. If the maximum time is reached, the connection is considered failed and can be retried.

For this case, we set the connection timeout to 5000 milliseconds. You can adjust the timeout based on your situation. Tweak them based on your application, keeping in mind the duration of WebSocket sessions and how long you expect them to live.

How to configure HAProxy for WebSocket connections

Next, we have to create the "frontend" part, where we bind the port used for the WebSocket connection and the referenced backend server. In this case, we specify port 80 and give the URL to access the “Stats” page to monitor our HAProxy. Finally, we define the backend parts that are referenced in load balancing.

How to configure HAProxy for WebSocket connections

In the "Backend" part, we use round-robin scheduling to distribute the load. "option forward for" specifies that HAProxy includes an "X-Forward-For" header when sending a connection request, which contains the client's IP address. This way the backend server will get the correct client IP.

"Option http_server_close" limits resources by eliminating any idle connections taking up server resources. Although WebSocket connections are long-lived, we must check if there are idle long-lived connections and close them to avoid unnecessary waste of resources.

Finally, add the backend servers that will be used to distribute traffic. Make sure to give the server the correct IP address and replace the IP address in the image below:

How to configure HAProxy for WebSocket connections

Your HAProxy is now configured for a WebSocket connection. Save changes and close file.

3. Validity test

Before restarting HAProxy, we can run a quick command to check that the file is valid and has no errors. To do this, run the following command:

$sudo haproxy-c-f/etc/haproxy/haproxy.cfg

How to configure HAProxy for WebSocket connections

4. Restart and test the WebSocket connection

After the file is valid, restart the HAProxy service.

How to configure HAProxy for WebSocket connections

That's it. You configured HAProxy for WebSocket connections. You can test the connection to ensure all WebSocket traffic is forwarded correctly.

in conclusion

Configuring HAProxy for WebSocket connections is a popular solution, especially among web administrators. This article elaborates on the process in detail, giving the steps and an example to ensure you are comfortable following and managing to implement the same process in your case. Work through each step to make sure you understand the concept and the process of implementing it.

The above is the detailed content of How to configure HAProxy for WebSocket connections. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
Download Hidester VPN/Proxy to Access Your Favorite Content - MiniToolDownload Hidester VPN/Proxy to Access Your Favorite Content - MiniToolApr 22, 2025 am 12:50 AM

Learn about Hidester VPN and Hidester proxy and download Hidester VPN for Windows, Mac, Android, and iOS to use this VPN service to view websites with no limit. For more useful free computer tools and troubleshooting tips, you may visit php.cn Softwa

Windows Keyboard Opening Shortcuts Instead of Typing [Fixed]Windows Keyboard Opening Shortcuts Instead of Typing [Fixed]Apr 22, 2025 am 12:48 AM

Have you ever encountered the trouble of “Windows keyboard opening shortcuts instead of typing”? In this post from php.cn, you will learn how to fix this issue.

Easy Ways to Add the Control Panel Icon to Desktop on Win 10 / 11Easy Ways to Add the Control Panel Icon to Desktop on Win 10 / 11Apr 22, 2025 am 12:46 AM

In this post, php.cn Software will introduce what Control Panel is and how to add the Control Panel icon to desktop on your Windows 10 or Windows 11 computer. You can also learn some related information about desktop icon settings.

Granblue Fantasy Relink Save File Location & Backup Save DataGranblue Fantasy Relink Save File Location & Backup Save DataApr 22, 2025 am 12:45 AM

If you play Granblue Fantasy: Relink on your PC, you may wonder where you can find its save file. In this post, php.cn introduces everything you want to know - Granblue Fantasy Relink save file location and how to back up the savegame of this game.

How to Fix Event ID 1104: The Security Log Is Now Full? - MiniToolHow to Fix Event ID 1104: The Security Log Is Now Full? - MiniToolApr 22, 2025 am 12:44 AM

Event Viewer keeps track of activity for better management. However, if the upper limit of the security log is reached, no more events can be logged. In this post on php.cn Website, we will show you how to deal with Event ID 1104 the security log is

Watch: How to Enable Secure Boot on Gigabyte Motherboard?Watch: How to Enable Secure Boot on Gigabyte Motherboard?Apr 22, 2025 am 12:43 AM

Secure Boot is a security standard that can prevent your computer from booting with untrustworthy software. Enabling it will add an extra layer of security to your device. In this post from php.cn Website, we will show you how to enable Secure Boot o

Windows 11 23H2 Release Date: September 26, 2023 - MiniToolWindows 11 23H2 Release Date: September 26, 2023 - MiniToolApr 22, 2025 am 12:42 AM

Coming to a new year, what Windows 11 users are looking forward to are not only the patch updates but also the annual major update for Windows 11. This post will talk about the Windows 11 23H2 release date. In addition, if you want to recover deleted

How to Turn off Bixby on Samsung Phone? See a Guide! - MiniToolHow to Turn off Bixby on Samsung Phone? See a Guide! - MiniToolApr 22, 2025 am 12:41 AM

Can you completely disable Bixby? How to turn off Bixby on Samsung phones? It is not hard to disable this voice assistant. In this post from php.cn, we will go to any length to help you find the method. Besides, a way to turn off “Hi, Bixby” is also

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.