<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Nginx on Techformist</title>
    <link>https://techformist.com/tags/nginx/</link>
    <description>Recent content in Nginx on Techformist</description>
    <image>
      <url>https://techformist.com/logo.svg</url>
      <link>https://techformist.com/logo.svg</link>
    </image>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 09 Sep 2020 06:30:00 +0000</lastBuildDate><atom:link href="https://techformist.com/tags/nginx/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Request Redirect for Nginx in SPAs</title>
      <link>https://techformist.com/request-redirect-nginx-spa/</link>
      <pubDate>Wed, 09 Sep 2020 06:30:00 +0000</pubDate>
      
      <guid>https://techformist.com/request-redirect-nginx-spa/</guid>
      <description>&lt;p&gt;Redirection can be easy enough on Nginx. Let&amp;rsquo;s see how we can utilise that for our single page applications coded in Vue, React, etc.&lt;/p&gt;
&lt;h2 id=&#34;nginx-and-redirection&#34;&gt;Nginx and Redirection&lt;/h2&gt;
&lt;p&gt;Nginx configuration is simple but powerful. All we need is a couple of lines to setup our server.&lt;/p&gt;
&lt;p&gt;Add the below lines to the Nginx configuration files (e.g. in &lt;code&gt;/etc/nginx/sites-enabled/domain.com.conf&lt;/code&gt; in Ubuntu).&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;server {
    listen        80;
    server_name   domain.com;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Start the Nginx server, and lo and behold - our web server is ready to serve magic at port 80. Go to &lt;code&gt;domain.com&lt;/code&gt; in browser and see your beautiful app.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Redirection can be easy enough on Nginx. Let&rsquo;s see how we can utilise that for our single page applications coded in Vue, React, etc.</p>
<h2 id="nginx-and-redirection">Nginx and Redirection</h2>
<p>Nginx configuration is simple but powerful. All we need is a couple of lines to setup our server.</p>
<p>Add the below lines to the Nginx configuration files (e.g. in <code>/etc/nginx/sites-enabled/domain.com.conf</code> in Ubuntu).</p>
<pre tabindex="0"><code>server {
    listen        80;
    server_name   domain.com;
}
</code></pre><p>Start the Nginx server, and lo and behold - our web server is ready to serve magic at port 80. Go to <code>domain.com</code> in browser and see your beautiful app.</p>
<p>We can add redirection in the mix with a single line.</p>
<pre tabindex="0"><code>server {
    listen        80;
    server_name   domain.com;
    return        301 http://other_domain.com$request_uri;
}
</code></pre><p>This is a simple <code>301</code> redirection that informs requester (i.e., browser) that the web page at <code>domain.com</code> is redirected to <code>other_domain.com</code> permanently. Any suffix included with the original domain (e.g. <code>https://domain.com/awesome-page/1</code>) is sent to the redirected domain by using <code>$request_uri</code>. You can redirect any page to any other page within or outside the domain. We have previously seen this applied to <a href="/create-404-pages-vue-app">manage 404 error in Vue in Nginx</a>.</p>
<p>Replace <code>301</code> in the <code>return</code> statement to <code>302</code>, and you have a temporary redirect. I don&rsquo;t quite find a use case for temporary redirections, and can&rsquo;t claim to know how Google or other search engines respond to them, but.. they exist.</p>
<p>Alternatively, you could use a <code>rewrite</code> -</p>
<pre tabindex="0"><code>server {
    listen      80;

    location / {
      rewrite     ^(.*)   https://other_domain.com permanent;
    }
}
</code></pre><p>This statement indicates that all requests to the domain that server is listening to have to be redirected to some other domain.</p>
<p><code>rewrite</code> allows us to use regex and handle complex scenarios for redirection within or outside the domain.</p>
<p>For example -</p>
<pre tabindex="0"><code>server {
    # ...
    rewrite ^(/download/.*).?.*$ files.domain.com/downloads/$2;

    #...
}
</code></pre><p>I don&rsquo;t quite use Nginx to its full capacity and have rather naive considerations on when to use <code>rewrite</code> vs <code>redirect</code>-</p>
<ul>
<li>Simple use cases can be addressed by <code>redirect</code></li>
<li><code>rewrite</code> handles only 301 and 302 redirects (more on this in a bit)</li>
<li>Redirects based on conditions (e.g. */download/* should be directed to <code>files.domain.com</code> URL) are possible only using <code>rewrite</code></li>
</ul>
<p>While complex conditions are candidates for <code>rewrite</code>, I typically manage &ldquo;all things&rdquo; in the SPA or using server routes. But, there&rsquo;s more.</p>
<h2 id="nginx-redirection-in-spas">Nginx Redirection in SPAs</h2>
<p>Single page applications handle all routing on the client (well, typically). You can have something like a <code>vue-router</code> installed, throw in the routes in a <code>route.js</code> file and the application happily does all the redirections without help from server.</p>
<p>All we need from server is -</p>
<pre tabindex="0"><code>server {
    server_name domain.com;
    listen 80;
    root /usr/www/domain;
    index index.html index.htm;

    location / {
            try_files $uri $uri/ /index.html;
    }
    location /api/ {
            proxy_pass http://localhost:5000;
            proxy_set_header Host $host;
    }
}
</code></pre><p>In Ubuntu -</p>
<ul>
<li>you can enter this configuration in <code>/etc/nginx/sites-available/domain.com.conf</code> and</li>
<li>setup a link in <code>/etc/nginx/sites-enabled/domain.com.conf</code></li>
</ul>
<p>We are achieving quite a bit with these few lines -</p>
<ul>
<li>use yet another redirection directive called <code>try_files</code> to redirect any request (exceptions - see below) to <code>$uri</code> to a corresponding file with the same name accessible on web server. Point the request to <code>/index.html</code> if there is no file at <code>$uri</code>. The request is then handled by the SPA router</li>
<li>redirect any requests with <code>domain.com/api/</code> to another port on the server. This sets up reverse proxy and opens up the backend server (at port 5000) to the world through our domain</li>
</ul>
<p>See <a href="https://www.nginx.com/blog/creating-nginx-rewrite-rules/">this rewrite rules post</a> for a more detailed comparison of what each of those directives can do.</p>
<p>The above configuration sets up a full-fledged web server for your SPA, but there&rsquo;s one thing missing. That is for <code>http</code> to <code>https</code> redirection.. which is as easily done.</p>
<pre tabindex="0"><code>server {
    server_name domain.com;
    listen 80;
    return 301 https://$host$request_uri;
}

server {
    server_name domain.com;
    listen 443 ssl;
    root /usr/www/domain;
    index index.html index.htm;

    location / {
            try_files $uri $uri/ /index.html;
    }
    location /api/ {
            proxy_pass http://localhost:5000;
            proxy_set_header Host $host;
    }
}
</code></pre><p>This is all you need to setup a simple web server serving your SPA with Nginx.</p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
