<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kunal Badade on Medium]]></title>
        <description><![CDATA[Stories by Kunal Badade on Medium]]></description>
        <link>https://medium.com/@kunal.s.badade?source=rss-9df6d7fddb9a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_XaGeqpA5e9TO52d.jpg</url>
            <title>Stories by Kunal Badade on Medium</title>
            <link>https://medium.com/@kunal.s.badade?source=rss-9df6d7fddb9a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 20:39:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kunal.s.badade/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simple HTTP Load Balancing and Subrequest Authentication with NGINX]]></title>
            <link>https://medium.com/swlh/simple-http-load-balancing-and-subrequest-authentication-with-nginx-b3a6d9cfa6c0?source=rss-9df6d7fddb9a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3a6d9cfa6c0</guid>
            <category><![CDATA[http-authentication]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[load-balancing]]></category>
            <dc:creator><![CDATA[Kunal Badade]]></dc:creator>
            <pubDate>Thu, 23 May 2019 05:24:31 GMT</pubDate>
            <atom:updated>2019-06-09T18:25:55.720Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V_zjt5yeAHCTUtRf1I8yIg.png" /><figcaption>Source: <a href="https://www.nginx.com/">www.nginx.com</a></figcaption></figure><p>NGINX is a high-performance web server. We are going to see how we can use it as a load balancer. We will also see how we can implement authentication based on subrequest results. You can write as many configurations files as you want. All the config files can be found in the following directory:</p><pre>/etc/nginx/</pre><p>The master configuration file is <em>/etc/nginx/nginx.conf</em>. It is only used for server-wide behavior. However, if you want to configure an individual website, each configuration file for each website should be created in <em>/etc/nginx/sites-available</em>. The standard way is to symlink config files from <em>/etc/nginx/sites-available</em> to <em>/etc/nginx/sites-enabled</em>. Following command can be used in Linux to symlink:</p><pre>$ sudo ln -s /etc/nginx/sites-available/myconfigfile /etc/nginx/sites-enabled/</pre><p><strong>HTTP Load Balancing</strong></p><p>NGINX can load balance multiple application instances using one of the three supported mechanisms. Following are those mechanisms:</p><p>1. Round-robin — It is pretty much self-explanatory.</p><p>2. Least-connected — The request will be redirected to the server with the least number of active connections.</p><p>3. IP-hash — It depends on the client’s IP address. NGINX has defined a hash-function to decide which server should be selected.</p><p>If the mechanism is not mentioned, by default round-robin is considered.</p><p>Before I dig into technical details, I would like to take a moment to explain an example. I am going to take the example of a blog platform. Let’s consider 4 main sections of a blog platform viz. Articles, Comments, Users, and Tags. Suppose we have been able to refactor the one microservice into 4 different microservices (Each for each section). We are going to start 3 instances of each microservice. This can be done using Foreman. I am going to consider localhost, i.e., 127.0.0.1 as the server’s IP address. Foreman will assign the following port numbers to each microservice instance:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*JIx4mRuHMrfhWiOjqYyESw.png" /></figure><p>It’s time! Let’s talk about actual stuff. NGINX has the upstream module to define a pool of servers among which requests will be distributed. You can use upstream like this:</p><pre>http {<br>    upstream articlebackend {<br>        server 127.0.0.1:5000;<br>        server 127.0.0.1:5001;<br>        server 127.0.0.1:5002;<br>    }</pre><pre>server {<br>        listen 80;</pre><pre>location / {<br>            proxy_pass <a href="http://articlebackend/">http://articlebackend</a>;<br>        }<br>    }<br>}</pre><p>In the above example, 3 instances of the article microservice are being load balanced. As we haven’t mentioned the load balancing mechanism, round-robin will have defaulted. You just have to use “https” to configure load balancing for HTTPS.</p><p>To make use of the least connected mechanism, replace the upstream module with the following:</p><pre>upstream articlebackend {<br> least_conn;<br>        server 127.0.0.1:5000;<br>        server 127.0.0.1:5001;<br>        server 127.0.0.1:5002;<br>    }</pre><p>And, for IP-hash use,</p><pre>upstream articlebackend {<br> ip_hash;<br>        server 127.0.0.1:5000;<br>        server 127.0.0.1:5001;<br>        server 127.0.0.1:5002;<br>    }</pre><p>You can mention the URL in the location directive within the server block. Following is the syntax of the location directive.</p><pre>location [modifier] [URI] {<br>}</pre><p>The modifier is optional. Modifier includes the following:</p><p>1. =: The equal sign is used to match location block exactly against the requested URI</p><p>2. ~: The tilde sign is used against a requested URI for a case-sensitive regular expression match.</p><p>3. ~*: The tilde followed by the asterisk sign is used against a requested URI to match a case-insensitive regular expression.</p><p>4. <strong>^~: </strong>The carat followed by a tilde sign is used against the requested URI to perform the longest non-regular expression match.</p><p>Considering all four section, the config file will look like this:</p><pre>http {<br>    upstream articlebackend {<br>        server 127.0.0.1:5000;<br>        server 127.0.0.1:5001;<br>        server 127.0.0.1:5002;<br>    }</pre><pre>upstream commentbackend {<br>        server 127.0.0.1:5100;<br>        server 127.0.0.1:5101;<br>        server 127.0.0.1:5102;<br>    }</pre><pre>upstream userbackend {<br>        server 127.0.0.1:5200;<br>        server 127.0.0.1:5201;<br>        server 127.0.0.1:5202;<br>    }</pre><pre>upstream tagbackend {<br>        server 127.0.0.1:5300;<br>        server 127.0.0.1:5301;<br>        server 127.0.0.1:5302;<br>    }</pre><pre>server {<br>        listen 80;</pre><pre>location / {<br>            proxy_pass <a href="http://articlebackend/">http://articlebackend</a>;<br>        }<br>    }<br>}</pre><p><strong>HTTP Subrequest Authentication</strong></p><p><a href="https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-subrequest-authentication/">NGINX’s</a> official website states that,</p><blockquote>“To perform authentication, NGINX makes an HTTP subrequest to an external server where the subrequest is verified. If the subrequest returns a 2xx response code, the access is allowed, if it returns 401 or 403, the access is denied. Such type of authentication allows implementing various authentication schemes, such as multifactor authentication, or allows implementing LDAP or OAuth authentication.”</blockquote><p>We incorporate HTTP subrequest authentication we will use the following block. Here, you are simply defining a URI that should be hit to authenticate any user. In this below location directive, I am calling /auth-server API to authenticate the user.</p><pre>location = /auth {<br>            internal;<br>            proxy_method POST;<br>            proxy_pass <a href="http://userbackend/auth-server">http://userbackend/auth-server</a>;<br>            proxy_pass_request_body off;<br>            proxy_set_header Content-Length &quot;&quot;;<br>            proxy_set_header X-Original-URI $request_uri;<br>        }</pre><p>The request body is discarded for authentication subrequests thus you will need to set the <em>proxy_pass_request_body</em> directive to off and also set the Content-Length header to a null string.</p><p>Also, we can pass the complete original request with arguments with the <em>proxy_set_header</em> directive. This is an important part as we want to pass arguments like user credentials with the request to be authenticated.</p><p>Suppose we want to post a new article on our blog platform, and we want the user to be authenticated before he/she can post an article. In this case, we will use the following location directive:</p><pre>location /new_article {<br>            auth_request /auth;<br>            auth_request_set $auth_status $upstream_status;<br>            proxy_pass <a href="http://articlebackend/new_article">http://articlebackend/new_article</a>;<br>        }</pre><p>I have specified the <em>auth_request</em> directive in which an internal location is specified where an authorization subrequest will be forwarded to. Here, for each request to /new_article, a subrequest to the internal /auth location will be made.</p><p>You can set a variable value basing on the result of the subrequest with the <em>auth_request_set</em> directive.</p><p>A sample curl request is as follows:</p><pre>curl -u username:password --include --verbose --request POST --header &#39;Content-Type: application/json&#39; --data &#39;{&quot;article_content&quot;:&quot;Joker is awesome&quot;,&quot;article_title&quot;:&quot;The Dark Knight&quot;}&#39; <a href="http://localhost/new_article">http://localhost/new_article</a></pre><p>Complete Example:</p><pre>upstream articlebackend{<br>       server 127.0.0.1:5000;<br>       server 127.0.0.1:5001;<br>       server 127.0.0.1:5002;<br>    }<br>upstream userbackend{<br>       server 127.0.0.1:5200;<br>       server 127.0.0.1:5201;<br>       server 127.0.0.1:5202;<br>    }</pre><pre>server {<br> listen 80; <br>        <br> server_name localhost;</pre><pre>location /articles {<br>            proxy_pass <a href="http://articlebackend/articles">http://articlebackend/articles</a>;<br>        }</pre><pre>location /new_article {<br>            auth_request /auth;<br>            auth_request_set $auth_status $upstream_status;<br>            proxy_pass <a href="http://articlebackend/new_article">http://articlebackend/new_article</a>;<br>        }</pre><pre>location = /auth {<br>            internal;<br>            proxy_method POST;<br>            proxy_pass <a href="http://userbackend/auth-server">http://userbackend/auth-server</a>;<br>            proxy_pass_request_body off;<br>            proxy_set_header Content-Length &quot;&quot;;<br>            proxy_set_header X-Original-URI $request_uri;<br>      }</pre><pre>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3a6d9cfa6c0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/simple-http-load-balancing-and-subrequest-authentication-with-nginx-b3a6d9cfa6c0">Simple HTTP Load Balancing and Subrequest Authentication with NGINX</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>