{"id":301947,"date":"2021-07-15T12:00:00","date_gmt":"2021-07-15T09:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-setup-basic-load-balancing-in-nginx-cloudsavvy-it\/"},"modified":"2021-07-15T12:00:00","modified_gmt":"2021-07-15T09:00:00","slug":"how-to-setup-basic-load-balancing-in-nginx-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-setup-basic-load-balancing-in-nginx-cloudsavvy-it\/","title":{"rendered":"#How to Setup Basic Load Balancing in NGINX \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a26ebe39b774\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a26ebe39b774\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-setup-basic-load-balancing-in-nginx-cloudsavvy-it\/#How_NGINX_Load_Balancing_Works\" >How NGINX Load Balancing Works<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-setup-basic-load-balancing-in-nginx-cloudsavvy-it\/#Proxying_to_the_Backend\" >Proxying to the Backend<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Setup Basic Load Balancing in NGINX \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 1676px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage wp-image-4029 size-full\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/7950f98f.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/7950f98f.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/7950f98f.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"NGINX Logo\" width=\"1676\" height=\"748\" data-crediturl=\"https:\/\/www.nginx.com\/\" data-credittext=\"NGINX\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.nginx.com\/\">NGINX<\/a><\/span><\/figcaption><\/figure>\n<p>NGINX is commonly used as a web server, but it also does a great job of acting as a reverse proxy and load balancer\u2014a network device designed to handle the bulk of your traffic and route requests to multiple different web servers.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_NGINX_Load_Balancing_Works\"><\/span>How NGINX Load Balancing Works<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The basic principle of a Load Balancer is that it sits between the user and a set of servers, and proxies requests for them. Usually this is done with two or more servers, so that traffic can be distributed more easily between them.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-5272 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/06\/31cd439f.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"An NGINX Load Balancer.\" width=\"700\" height=\"309\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Most of the configuration h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ens in how NGINX selects which server to route to. The default is round-robin, which will send requests to each server in order, ensuring an equal load distribution.<\/p>\n<p>However, it\u2019s not always that simple. Many web applications require some form of\u00a0<em>session persistence<\/em>, which means that the user must be accessing the same server for their whole session. For example, a shopping cart might be stored locally on one application server, and if the user switches servers mid-session, the application could glitch out. Of course, many of these scenarios can be fixed with better application infrastructure and centralized datastores, but session persistence is required by many people.<\/p>\n<p>In NGINX, the set of servers that you route to is known as an\u00a0<em>upstream<\/em>, and is configured like an enumerated list of addresses:<\/p>\n<pre><span class=\"kn\">upstream<\/span> <span class=\"s\">backend<\/span> <span class=\"p\">{<\/span>&#13;\n    <span class=\"kn\">server<\/span> <span class=\"s\">backend1.example.com<\/span> <span class=\"s\">weight=5<\/span><span class=\"p\">;<\/span>&#13;\n    <span class=\"kn\">server<\/span> <span class=\"s\">backend2.example.com<\/span><span class=\"p\">;<\/span>&#13;\n<span class=\"p\">}<\/span><\/pre>\n<p>These upstreams <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_upstream_module.html\">have a lot of options<\/a>; here, we\u2019ve set a weight, which will prioritize this server more often (particularly useful if you have different sizes). You can also set the max connnections and various timeouts. If you\u2019re using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.nginx.com\/products\/\">NGINX Plus<\/a>, you can also set up health checks so that connections don\u2019t get routed to unhealthy servers.<\/p>\n<p>The most basic form of session persistence is using an IP hash. NGINX will use the IP to identify users and then make sure that these users don\u2019t switch servers mid-session:<\/p>\n<pre>upstream <span class=\"s\">backend<\/span> {&#13;\n    ip_hash;&#13;\n\u00a0   <span class=\"kn\">server<\/span> <span class=\"s\">backend1.example.com;&#13;\n<\/span><span class=\"kn\">    server<\/span> <span class=\"s\">backend2.example.com<\/span><span class=\"p\">;<\/span>&#13;\n}<\/pre>\n<p>The IP hash is required for socket-based applications and anything requiring persistence. If you don\u2019t want to use the IP address, you can customize this hash:<\/p>\n<pre>upstream <span class=\"s\">backend <\/span>{&#13;\n    hash $scheme$request_uri consistent;&#13;\n \u00a0  <span class=\"kn\">server<\/span> <span class=\"s\">backend1.example.com;&#13;\n<\/span><span class=\"kn\">    server<\/span> <span class=\"s\">backend2.example.com<\/span><span class=\"p\">;<\/span>&#13;\n}<\/pre>\n<p>If you don\u2019t need any kind of session persistence, you can make the round-robin selection a little smarter by selecting which server has the least connections:<\/p>\n<pre>upstream <span class=\"s\">backend <\/span>{&#13;\n    least_conn;&#13;\n    <span class=\"kn\">server<\/span> <span class=\"s\">backend1.example.com;&#13;\n<\/span><span class=\"kn\">    server<\/span> <span class=\"s\">backend2.example.com<\/span><span class=\"p\">;<\/span>&#13;\n}<\/pre>\n<p>Or, based on which one is currently responding fastest:<\/p>\n<pre>upstream <span class=\"s\">backend <\/span>{&#13;\n\u00a0   least_time (header | last_byte);&#13;\n    <span class=\"kn\">server<\/span> <span class=\"s\">backend1.example.com;&#13;\n<\/span><span class=\"kn\">    server<\/span> <span class=\"s\">backend2.example.com<\/span><span class=\"p\">;<\/span>&#13;\n}<\/pre>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.nginx.com\/nginx\/admin-guide\/load-balancer\/http-load-balancer\/#enabling-session-persistence\">NGINX Plus has a few other forms of session persistence<\/a>, but IP hashing will work for most applications.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Proxying_to_the_Backend\"><\/span>Proxying to the Backend<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once you\u2019ve got your backend configured, you can send requests to it from within your location blocks, using <code>proxy_pass<\/code>\u00a0with a URI to the backend.<\/p>\n<pre>server {&#13;\n    listen 80;&#13;\n    server_name example.com;&#13;\n    location \/ {&#13;\n        proxy_pass http:\/\/backend;&#13;\n    }&#13;\n}<\/pre>\n<p>Of course, if you\u2019re using HTTPS, you\u2019ll need to send the request with HTTPS:<\/p>\n<pre><span class=\"kn\">server<\/span> <span class=\"p\">{<\/span>&#13;\n    <span class=\"kn\">listen<\/span>      <span class=\"mi\">443<\/span> <span class=\"s\">ssl<\/span><span class=\"p\">;<\/span>&#13;\n<span class=\"kn\">    server_name<\/span> <span class=\"s\">example.com<\/span><span class=\"p\">;<\/span>&#13;\n&#13;\n    <span class=\"kn\">ssl_certificate<\/span>        <span class=\"s\">\/etc\/ssl\/certs\/server.crt<\/span><span class=\"p\">;<\/span>&#13;\n    <span class=\"kn\">ssl_certificate_key<\/span>    <span class=\"s\">\/etc\/ssl\/certs\/server.key<\/span><span class=\"p\">;<\/span>&#13;\n    <span class=\"kn\">ssl_client_certificate<\/span> <span class=\"s\">\/etc\/ssl\/certs\/ca.crt<\/span><span class=\"p\">;<\/span>&#13;\n&#13;\n    <span class=\"kn\">location<\/span> <span class=\"s\">\/<\/span><span class=\"p\">{<\/span>&#13;\n       <span class=\"kn\">proxy_pass<\/span> <span class=\"s\">https:\/\/backend<\/span><span class=\"p\">;<\/span>&#13;\n    <span class=\"p\">}&#13;\n}<\/span><\/pre>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/5271\/how-to-configure-nginx-for-basic-load-balancing\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Setup Basic Load Balancing in NGINX \u2013 CloudSavvy IT&#8221; NGINX NGINX is commonly used as a web server, but it also does a great job of acting as a reverse proxy and load balancer\u2014a network device designed to handle the bulk of your traffic and route requests to multiple different web servers. How&#8230;<\/p>\n","protected":false},"author":1,"featured_media":301948,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/7950f98f.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-301947","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/301947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=301947"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/301947\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/301948"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=301947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=301947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=301947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}