{"id":17845,"date":"2023-07-21T16:13:10","date_gmt":"2023-07-21T16:13:10","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=17845"},"modified":"2023-07-20T09:15:23","modified_gmt":"2023-07-20T09:15:23","slug":"how-to-setup-rate-limiting-with-haproxy","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-setup-rate-limiting-with-haproxy\/","title":{"rendered":"How to Setup Rate Limiting with HAProxy"},"content":{"rendered":"<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-1024x768.jpg\" alt=\"How to Setup Rate Limiting with HAProxy\" width=\"1024\" height=\"768\" class=\"alignnone size-large wp-image-17847 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-1024x768.jpg 1024w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-300x225.jpg 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-1536x1152.jpg 1536w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-2048x1536.jpg 2048w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-128x96.jpg 128w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-420x315.jpg 420w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-540x405.jpg 540w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-720x540.jpg 720w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-960x720.jpg 960w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-1140x855.jpg 1140w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-1320x990.jpg 1320w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-Rate-Limiting-with-HAProxy-1440x1080.jpg 1440w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/768;\" \/><\/p>\n<p>As a server administrator or webmaster, you may have encountered situations where your server resources are being excessively consumed by certain users or IP addresses. This could be due to a variety of reasons, such as a user running a script that makes too many requests, or even a malicious attempt to overwhelm your <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-are-web-servers-and-why-are-they-needed\/\">web server<\/a>. In such scenarios, rate limiting can be a lifesaver.<\/p>\n<p>Rate limiting is a technique for limiting network traffic. It sets a limit on how many requests a client can make to a server in a specified amount of time. <a href=\"https:\/\/webhostinggeeks.com\/blog\/haproxy-features-functions-benefits\/\">HAProxy<\/a>, a popular open-source <a href=\"https:\/\/webhostinggeeks.com\/best\/proxy-servers\/\">proxy software<\/a> that provides high availability, <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-load-balancing\/\">load balancing<\/a>, and proxy for TCP and HTTP-based applications, has built-in support for rate limiting.<\/p>\n<p>In this tutorial, we will guide you through the process of setting up rate limiting with HAProxy on your server, whether it&#8217;s a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS<\/a>, or <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-cloud-hosting\/\">cloud<\/a> machine. This will help you to protect your server resources, ensure fair usage, and maintain the quality of service for all users.<\/p>\n<p>Let&#8217;s get started!<\/p>\n<h2>Step 1: Installing HAProxy<\/h2>\n<p>The first step is to install HAProxy on your server. Depending on your server&#8217;s operating system, the installation command may vary. For Debian-based systems like Ubuntu, you can use the following command:<\/p>\n<pre>\r\nsudo apt-get update\r\nsudo apt-get install haproxy\r\n<\/pre>\n<p>For Red Hat-based systems like CentOS, you can use:<\/p>\n<pre>\r\nsudo yum update\r\nsudo yum install haproxy\r\n<\/pre>\n<p>After running these commands, HAProxy should be installed on your server.<\/p>\n<h2>Step 2: Configuring HAProxy for Rate Limiting<\/h2>\n<p>Once HAProxy is installed, the next step is to configure it for rate limiting. This involves editing the HAProxy configuration file, which is typically located at \/etc\/haproxy\/haproxy.cfg.<\/p>\n<p>Open the configuration file in a text editor:<\/p>\n<pre>\r\nsudo nano \/etc\/haproxy\/haproxy.cfg\r\n<\/pre>\n<p>In the configuration file, you will need to define a frontend that listens for incoming connections, a backend that forwards the connections to your web server, and the rate limiting rules.<\/p>\n<p>Here is an example of how to set up rate limiting to allow a maximum of 10 HTTP requests per minute from a single IP address:<\/p>\n<pre>\r\nfrontend http_front\r\n    bind *:80\r\n    stick-table type ip size 200k expire 1m store http_req_rate(60s)\r\n    tcp-request connection track-sc1 src\r\n    tcp-request connection reject if { src_get_gpc0(http_front) gt 10 }\r\n\r\nbackend http_back\r\n    server web1 192.168.1.2:80\r\n<\/pre>\n<p>In this configuration, the stick-table directive creates a table to track the rate of HTTP requests from each IP address. The tcp-request connection track-sc1 src directive tells HAProxy to track the source IP address of each connection. The tcp-request connection reject if { src_get_gpc0(http_front) gt 10 } directive tells HAProxy to reject any new connections if the IP address has made more than 10 requests in the past minute.<\/p>\n<p>After editing the configuration file, save and close it.<\/p>\n<h2>Step 3: Restarting HAProxy<\/h2>\n<p>After making changes to the HAProxy configuration file, you need to restart HAProxy for the changes to take effect. You can do this with the following command:<\/p>\n<pre>\r\nsudo systemctl restart haproxy\r\n<\/pre>\n<p>Now, HAProxy should be enforcing the rate limiting rules you defined.<\/p>\n<h2>Step 4: Verifying the Setup<\/h2>\n<p>To verify that rate limiting is working, you can try making more than 10 requests to your server from the same IP address within a minute. If rate limiting is working correctly, HAProxy should reject the excess requests.<\/p>\n<p>You can also check the HAProxy logs for entries indicating that connections have been rejected due to rate limiting. The logs are typically located at \/var\/log\/haproxy.log.<\/p>\n<pre>\r\nsudo tail \/var\/log\/haproxy.log\r\n<\/pre>\n<p>Look for log entries with the message &#8220;Connection rejected due to rate limiting&#8221;. This indicates that rate limiting is working as expected.<\/p>\n<h2>Step 5: Monitoring Rate Limiting<\/h2>\n<p>Monitoring is a crucial aspect of maintaining a healthy server environment. With HAProxy, you can monitor the rate limiting in real-time using the built-in statistics page. This page provides a wealth of information about the current state of your HAProxy instance, including the number of connections, the rate of connections, and more.<\/p>\n<p>To enable the statistics page, you need to add a &#8220;listen&#8221; section to your HAProxy configuration file:<\/p>\n<pre>\r\nlisten stats\r\n    bind *:8080\r\n    stats enable\r\n    stats uri \/stats\r\n    stats refresh 30s\r\n<\/pre>\n<p>In this configuration, HAProxy will serve the statistics page on port 8080, at the \/stats URL. The page will be refreshed every 30 seconds.<\/p>\n<p>After adding this configuration, save the file and restart HAProxy:<\/p>\n<pre>\r\nsudo systemctl restart haproxy\r\n<\/pre>\n<p>You can now access the statistics page by navigating to http:\/\/your_server_ip:8080\/stats in your web browser.<\/p>\n<h2>Step 6: Fine-Tuning Rate Limiting<\/h2>\n<p>The rate limiting configuration provided in this tutorial is a basic one, allowing a maximum of 10 HTTP requests per minute from a single IP address. Depending on your server&#8217;s capacity and your specific needs, you may need to adjust these numbers.<\/p>\n<p>For example, if your server is powerful and you&#8217;re not experiencing any issues with excessive requests, you might want to increase the limit. On the other hand, if your server is struggling to handle the current load, you might need to decrease the limit.<\/p>\n<p>To adjust the rate limit, simply change the number in the following line of the HAProxy configuration:<\/p>\n<pre>\r\ntcp-request connection reject if { src_get_gpc0(http_front) gt 10 }\r\n<\/pre>\n<p>Replace &#8220;10&#8221; with the desired maximum number of requests per minute.<\/p>\n<p>Remember to restart HAProxy after making any changes to the configuration:<\/p>\n<pre>\r\nsudo systemctl restart haproxy\r\n<\/pre>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">sudo apt-get update<\/span> \u2013 Updates the package lists for upgrades and new package installations<\/li>\n<li><span class=\"fw-bold\">sudo apt-get install haproxy<\/span> \u2013 Installs HAProxy on Debian-based systems<\/li>\n<li><span class=\"fw-bold\">sudo yum update<\/span> \u2013 Updates the package lists for upgrades and new package installations on Red Hat-based systems<\/li>\n<li><span class=\"fw-bold\">sudo yum install haproxy<\/span> \u2013 Installs HAProxy on Red Hat-based systems<\/li>\n<li><span class=\"fw-bold\">sudo nano \/etc\/haproxy\/haproxy.cfg<\/span> \u2013 Opens the HAProxy configuration file in a text editor<\/li>\n<li><span class=\"fw-bold\">sudo systemctl restart haproxy<\/span> \u2013 Restarts HAProxy to apply configuration changes<\/li>\n<li><span class=\"fw-bold\">sudo tail \/var\/log\/haproxy.log<\/span> \u2013 Displays the last few entries in the HAProxy log file<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we have walked you through the process of setting up rate limiting with HAProxy on your web server. By following these steps, you can protect your server resources, ensure fair usage, and maintain the quality of service for all users.<\/p>\n<p>Rate limiting is a powerful tool for managing server load and preventing abuse. However, it&#8217;s important to monitor your server&#8217;s performance and adjust the rate limits as needed to best serve your users.<\/p>\n<p>For more information on HAProxy and its features, you can visit the <a href=\"https:\/\/webhostinggeeks.com\/blog\/haproxy-features-functions-benefits\/\">HAProxy features, functions, and benefits<\/a> page on our website. If you&#8217;re interested in exploring other proxy server options, check out our list of the <a href=\"https:\/\/webhostinggeeks.com\/best\/proxy-servers\/\">best proxy servers<\/a>.<\/p>\n<p>I hope this tutorial has been helpful in guiding you through the process of setting up rate limiting with HAProxy.<\/p>\n<p>If you have any questions or run into any issues, please feel free to leave a comment below.<\/p>\n<h2>FAQ<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is rate limiting in HAProxy?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Rate limiting in HAProxy is a feature that allows you to limit the number of requests a client can make to your server in a specified amount of time. This can help protect your server resources, prevent abuse, and maintain quality of service.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I configure rate limiting in HAProxy?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can configure rate limiting in HAProxy by editing the HAProxy configuration file, typically located at \/etc\/haproxy\/haproxy.cfg. In the configuration file, you define a frontend that listens for incoming connections, a backend that forwards the connections to your web server, and the rate limiting rules.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I verify that rate limiting is working in HAProxy?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can verify that rate limiting is working in HAProxy by making more than the allowed number of requests to your server from the same IP address within the rate limit period. If rate limiting is working correctly, HAProxy should reject the excess requests. You can also check the HAProxy logs for entries indicating that connections have been rejected due to rate limiting.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I monitor rate limiting in HAProxy?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can monitor rate limiting in HAProxy using the built-in statistics page. This page provides information about the current state of your HAProxy instance, including the number of connections, the rate of connections, and more. You can enable the statistics page by adding a &#8220;listen&#8221; section to your HAProxy configuration file.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I fine-tune rate limiting in HAProxy?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can fine-tune rate limiting in HAProxy by adjusting the maximum number of requests allowed per minute from a single IP address. This is done by changing the number in the following line of the HAProxy configuration: tcp-request connection reject if { src_get_gpc0(http_front) gt 10 }. Replace &#8220;10&#8221; with the desired maximum number of requests per minute. Remember to restart HAProxy after making any changes to the configuration.<\/span>\n<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>As a server administrator or webmaster, you may have encountered situations where your server resources are being excessively consumed by certain users or IP addresses. This could be due to&#8230;<\/p>\n","protected":false},"author":6,"featured_media":17847,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[2134],"tags":[2135,2140],"class_list":["post-17845","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-haproxy","tag-haproxy","tag-rate-limiting"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/17845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=17845"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/17845\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/17847"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=17845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=17845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=17845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}