{"id":16344,"date":"2024-10-31T19:24:42","date_gmt":"2024-10-31T19:24:42","guid":{"rendered":"https:\/\/truehost.com\/support\/?post_type=docs&#038;p=16344"},"modified":"2024-11-02T04:21:36","modified_gmt":"2024-11-02T04:21:36","password":"","slug":"how-to-configure-a-load-balancer-on-nginx","status":"publish","type":"docs","link":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/","title":{"rendered":"How to Configure a Load Balancer on Nginx"},"content":{"rendered":"\n<p>Here\u2019s a step-by-step guide for setting up a load balancer on Nginx to efficiently distribute traffic across multiple backend servers. <\/p>\n\n\n\n<p>This setup is commonly used to improve application performance, ensure availability, and support redundancy.<\/p>\n\n\n\n<p>I will use an Ubuntu server for this article.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install Nginx<\/h3>\n\n\n\n<p>If you haven\u2019t installed Nginx, start by installing it on the server you\u2019ll use as your load balancer.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Update the System Packages<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Install Nginx<\/h4>\n\n\n\n<p>On Debian-based systems like Ubuntu, you can install Nginx with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y nginx<\/code><\/pre>\n\n\n\n<p>On RHEL-based systems, like Almalinux, CentOs use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dnf install -y nginx<\/code><\/pre>\n\n\n\n<p>After installation, start and enable Nginx:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start nginx\nsudo systemctl enable nginx<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Configure the Load Balancer<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Open the Nginx Configuration File<\/h4>\n\n\n\n<p>Edit the Nginx configuration file to define the load balancer settings. <\/p>\n\n\n\n<p>Use either the default configuration file at <code>\/etc\/nginx\/nginx.conf<\/code> or create a new one in the <code>\/etc\/nginx\/conf.d\/<\/code> directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/nginx\/conf.d\/load_balancer.conf<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Define the Upstream Servers<\/h4>\n\n\n\n<p>In the configuration file, define the backend servers (also called \u201cupstream servers\u201d) that will handle the load. Add the following section, replacing the IP addresses with those of your backend servers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>upstream backend_servers {<br>server 192.168.1.10; # Server 1<br>server 192.168.1.11; # Server 2<br>server 192.168.1.12; # Server 3<br>}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Set Up the Load Balancing Configuration<\/h4>\n\n\n\n<p>Now configure a server block to route incoming requests to the defined <code>upstream<\/code> servers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen 80;\n\n    location \/ {\n        proxy_pass http:\/\/backend_servers;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>This configuration instructs Nginx to listen on port 80 and forward all traffic to the servers defined in the <code>upstream backend_servers<\/code> block.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Load Balancing Algorithms<\/h3>\n\n\n\n<p>Nginx offers several load balancing algorithms. The default algorithm is <strong>Round Robin<\/strong>, but you can also configure it for <strong>Least Connections<\/strong> or <strong>IP Hash<\/strong>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Round Robin (default)<\/strong> \u2013 Distributes traffic equally among all servers:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>upstream backend_servers {<br>server 192.168.1.10;<br>server 192.168.1.11;<br>}<\/code><\/pre>\n\n\n\n<p> 2. <strong>Least Connections<\/strong> \u2013 Directs traffic to the server with the fewest active connections, which helps balance load dynamically:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>upstream backend_servers {<br>least_conn;<br>server 192.168.1.10;<br>server 192.168.1.11;<br>}<\/code><\/pre>\n\n\n\n<p>3. <strong>IP Hash<\/strong> \u2013 Sends requests from the same client IP to the same backend server, which can be useful for session persistence:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>upstream backend_servers {<br>ip_hash;<br>server 192.168.1.10;<br>server 192.168.1.11;<br>}<\/code><\/pre>\n\n\n\n<p>Choose the algorithm that best suits your needs and adjust the <code>upstream<\/code> block accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Enable Health Checks (Optional)<\/h3>\n\n\n\n<p>Nginx doesn\u2019t natively support active health checks in the free version, but you can set a passive health check by configuring <code>proxy_next_upstream<\/code> directives to route requests away from failed servers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>upstream backend_servers {\n    server 192.168.1.10;\n    server 192.168.1.11;\n    server 192.168.1.12;\n}\n\nserver {\n    listen 80;\n\n    location \/ {\n        proxy_pass http:\/\/backend_servers;\n        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>In this configuration, Nginx will skip to the next server if it encounters errors like a timeout or a 500-series HTTP error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Apply and Test the Configuration<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Check Nginx Syntax<\/h4>\n\n\n\n<p>Run the following command to check the syntax of your Nginx configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nginx -t<\/code><\/pre>\n\n\n\n<p>If the syntax is correct, restart Nginx to apply the changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart nginx<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Verify the Load Balancer<\/h4>\n\n\n\n<p>Access the IP address or domain of your Nginx server (the load balancer) from a web browser to confirm that traffic is correctly routed to the backend servers. You can also use tools like <code>curl<\/code> or <code>ab<\/code> (Apache Bench) to send multiple requests and confirm that they are distributed across servers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl http:\/\/your_load_balancer_ip<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Additional Configuration (Optional)<\/h3>\n\n\n\n<p>You can further customize the load balancer by:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Configuring SSL<\/strong> for secure connections<\/li>\n\n\n\n<li><strong>Setting a custom error page<\/strong> for when all backend servers are down<\/li>\n\n\n\n<li><strong>Adding rate limiting<\/strong> to protect your backend servers<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Author&#8217;s Final Word<\/h2>\n\n\n\n<p>This configuration gives you a robust Nginx load balancer setup to handle incoming requests and distribute them across multiple backend servers, improving your application\u2019s scalability and reliability<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a step-by-step guide for setting up a load balancer on Nginx to efficiently distribute traffic across multiple backend servers. This setup is commonly used to improve application performance, ensure availability, and support redundancy. I will use an Ubuntu server for this article. Step 1: Install Nginx If you haven\u2019t installed Nginx, start by installing [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_eb_attr":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"doc_category":[1820,1824,1879,2128],"doc_tag":[],"class_list":["post-16344","docs","type-docs","status-publish","hentry","doc_category-cloud-servers-in-kenya","doc_category-dedicated-servers","doc_category-servers","doc_category-vps-servers"],"year_month":"2026-07","word_count":675,"total_views":0,"reactions":{"happy":0,"normal":0,"sad":0},"author_info":{"name":"Eugene","author_nicename":"eugene","author_url":"https:\/\/truehost.com\/support\/author\/eugene\/"},"doc_category_info":[{"term_name":"Cloud servers in Kenya","term_url":"https:\/\/truehost.com\/support\/docs-category\/cloud-servers-in-kenya\/"},{"term_name":"dedicated servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/dedicated-servers\/"},{"term_name":"Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/servers\/"},{"term_name":"VPS-Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/vps-servers\/"}],"doc_tag_info":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Configure a Load Balancer on Nginx -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Configure a Load Balancer on Nginx -\" \/>\n<meta property=\"og:description\" content=\"Here\u2019s a step-by-step guide for setting up a load balancer on Nginx to efficiently distribute traffic across multiple backend servers. This setup is commonly used to improve application performance, ensure availability, and support redundancy. I will use an Ubuntu server for this article. Step 1: Install Nginx If you haven\u2019t installed Nginx, start by installing [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-02T04:21:36+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-configure-a-load-balancer-on-nginx\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-configure-a-load-balancer-on-nginx\\\/\",\"name\":\"How to Configure a Load Balancer on Nginx -\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\"},\"datePublished\":\"2024-10-31T19:24:42+00:00\",\"dateModified\":\"2024-11-02T04:21:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-configure-a-load-balancer-on-nginx\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-configure-a-load-balancer-on-nginx\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-configure-a-load-balancer-on-nginx\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/truehost.com\\\/support\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Configure a Load Balancer on Nginx\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"name\":\"\",\"description\":\"Help In a Click\",\"publisher\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/truehost.com\\\/support\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\",\"name\":\"Truehost Kenya\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"contentUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"width\":240,\"height\":48,\"caption\":\"Truehost Kenya\"},\"image\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Configure a Load Balancer on Nginx -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/","og_locale":"en_US","og_type":"article","og_title":"How to Configure a Load Balancer on Nginx -","og_description":"Here\u2019s a step-by-step guide for setting up a load balancer on Nginx to efficiently distribute traffic across multiple backend servers. This setup is commonly used to improve application performance, ensure availability, and support redundancy. I will use an Ubuntu server for this article. Step 1: Install Nginx If you haven\u2019t installed Nginx, start by installing [&hellip;]","og_url":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/","article_modified_time":"2024-11-02T04:21:36+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/","url":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/","name":"How to Configure a Load Balancer on Nginx -","isPartOf":{"@id":"https:\/\/truehost.com\/support\/#website"},"datePublished":"2024-10-31T19:24:42+00:00","dateModified":"2024-11-02T04:21:36+00:00","breadcrumb":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-configure-a-load-balancer-on-nginx\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/truehost.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Configure a Load Balancer on Nginx"}]},{"@type":"WebSite","@id":"https:\/\/truehost.com\/support\/#website","url":"https:\/\/truehost.com\/support\/","name":"","description":"Help In a Click","publisher":{"@id":"https:\/\/truehost.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/truehost.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/truehost.com\/support\/#organization","name":"Truehost Kenya","url":"https:\/\/truehost.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","contentUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","width":240,"height":48,"caption":"Truehost Kenya"},"image":{"@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/comments?post=16344"}],"version-history":[{"count":4,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16344\/revisions"}],"predecessor-version":[{"id":16539,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16344\/revisions\/16539"}],"wp:attachment":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/media?parent=16344"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_category?post=16344"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_tag?post=16344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}