<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: G∆UR∆V Sehrawat;</title>
    <description>The latest articles on DEV Community by G∆UR∆V Sehrawat; (@root3d).</description>
    <link>https://dev.to/root3d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F90954%2Fa9247a5e-9dd8-4f14-a7de-1063f3b24ddc.jpeg</url>
      <title>DEV Community: G∆UR∆V Sehrawat;</title>
      <link>https://dev.to/root3d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/root3d"/>
    <language>en</language>
    <item>
      <title>Localhost on the internet</title>
      <dc:creator>G∆UR∆V Sehrawat;</dc:creator>
      <pubDate>Wed, 05 Sep 2018 17:57:31 +0000</pubDate>
      <link>https://dev.to/root3d/localhost-on-the-internet-4jak</link>
      <guid>https://dev.to/root3d/localhost-on-the-internet-4jak</guid>
      <description>&lt;h1&gt;
  
  
  Your Localhost On The Internet And That Too SSL enabled With No Restrictions.
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;There are tons of service which provide you the way to expose your localhost on internet. We need it for testing 3rd party APIs or show casing current development server. For example the most popular ones are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://ngrok.com/" rel="noopener noreferrer"&gt;ngrok&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://localtunnel.github.io/www/" rel="noopener noreferrer"&gt;localtunnel&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pagekite.net/" rel="noopener noreferrer"&gt;pagekite&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So why reinvent the wheel? Because you gotta get to the roots and understand the simple complexity. It's no big deal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;So what all do we need?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A Server (I will using $5 &lt;a href="https://www.digitalocean.com/" rel="noopener noreferrer"&gt;DO&lt;/a&gt; server)&lt;/li&gt;
&lt;li&gt;A Domain&lt;/li&gt;
&lt;li&gt;Curiosity and Patience&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What are the benefits of this setup? You may ask.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Total Custom Domain and as many domains you want.&lt;/li&gt;
&lt;li&gt;No limits on usages&lt;/li&gt;
&lt;li&gt;More than other services' paid plans&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's get started, shall we?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's just a 4 steps setup&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setup the listening port using nginx on the server associating it with the subdomain.&lt;/li&gt;
&lt;li&gt;Add subdomain in DNS(We will be using awesome Digital Ocean DNS)&lt;/li&gt;
&lt;li&gt;Add SSL certificate provided by our favorite &lt;a href="https://letsencrypt.org/" rel="noopener noreferrer"&gt;Let's Encrypt&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Forward the traffic using amazing SSH command.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1 - Configuring server with nginx to serve traffic for the subdomain
&lt;/h2&gt;

&lt;p&gt;Please install &lt;code&gt;nginx&lt;/code&gt; on the server first.&lt;br&gt;
You need to add one config file for nginx in &lt;code&gt;sites-available&lt;/code&gt; folder of &lt;code&gt;/etc/nginx&lt;/code&gt; assuming you are using Ubutnu. The nginx config file looks like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    server_name 42.igauravsehrawat.com;

    location / {
        proxy_pass http://localhost:4242;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        # Enables WS support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

server {
    server_name www.42.igauravsehrawat.com;

    location / {
        proxy_pass http://localhost:4242;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        # Enables WS support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a look at config.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There is &lt;code&gt;server_name&lt;/code&gt; directive which tell what domain to serve the request, &lt;code&gt;listen&lt;/code&gt; directive for which port to listen on the domain.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;location&lt;/code&gt; directive is important here, here we are just proxing request from &lt;code&gt;localhost&lt;/code&gt; with port &lt;code&gt;4242&lt;/code&gt;. This port is important since we will be directing our localhost traffic to it. Then there is setting header on the request.&lt;/li&gt;
&lt;li&gt;Rest of directive is enabling websocket support on this setup by upgrading the connection. That's all.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note: You need to change &lt;code&gt;server_name&lt;/code&gt; as per your required sub-domain instead of &lt;code&gt;42.igauravsehrawat.com&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 - Configuring Subdomain on DNS
&lt;/h2&gt;

&lt;p&gt;Using &lt;a href="https://www.digitalocean.com/docs/networking/dns/how-to/manage-records/" rel="noopener noreferrer"&gt;Digital Ocean DNS Service&lt;/a&gt;, we will add a subdomain of our choice, it be 42.igauravsehrawat.com as done in step 1&lt;/p&gt;

&lt;p&gt;For adding a subdomain, we will create 3 records(A Record, AAAA record, CNAME record)&lt;br&gt;
A and AAAA records will redirect to our digital ocean server as shown below.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuzb9xpoiktanpwenh39o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuzb9xpoiktanpwenh39o.png" alt="A &amp;amp; AAAA records" width="640" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CNAME is just an alias of our A/AAAA record. All three will appear as shown below&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F216b4e2ep4vdhu9wa37x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F216b4e2ep4vdhu9wa37x.png" alt="All three" width="640" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So that's all here, you should be able to access your sub-domain over internet, showing you &lt;code&gt;502 Bad Gateway&lt;/code&gt; page, which is expected since nothing is being forwarded to port &lt;code&gt;4242&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  . Step 3 - Enabling SSL
&lt;/h2&gt;

&lt;p&gt;Enabling SSL is piece of cake, more easy than that.Follow the &lt;code&gt;certbot&lt;/code&gt; installation sets from &lt;a href="https://certbot.eff.org/lets-encrypt/ubuntuxenial-nginx" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then run the magic command for nginx&lt;br&gt;
&lt;code&gt;sudo certbot --nginx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will see something like below&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftycgqvwlgzpjau81ygkw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftycgqvwlgzpjau81ygkw.png" alt="certbot-run" width="640" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run it twice, one for sub-domain and one for CNAME(alias).&lt;/p&gt;

&lt;p&gt;Now you should see this we you go to your sub-domain &lt;a href="http://www.42.igauravsehrawat.com" rel="noopener noreferrer"&gt;www.42.igauravsehrawat.com&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F947m3j83i3u27f0ijwv3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F947m3j83i3u27f0ijwv3.png" alt="expected-bad-gateway" width="640" height="377"&gt;&lt;/a&gt;&lt;br&gt;
This is expected since nothing is running on port &lt;code&gt;4242&lt;/code&gt; of localhost at the server.&lt;/p&gt;

&lt;p&gt;Woof, we are just one step away from the happy dance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4 - SSH Magic
&lt;/h2&gt;

&lt;p&gt;Just one command to rule them all&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -N -R localhost:4242:localhost:3000 root@42.igauravsehrawat.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;-N tells "Do not execute a remote command.  This is useful for just forwarding ports."&lt;br&gt;
-R tells "Connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side."&lt;/p&gt;

&lt;p&gt;First localhost is of the server and second is of local machine, then the address of your server. If you set up the SSH keys(Recommended) it would be seamless, no password, nothing.&lt;/p&gt;

&lt;p&gt;That's it.&lt;/p&gt;

&lt;p&gt;Happy Dance as promised.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxg8vlft8h965my3lmuzo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxg8vlft8h965my3lmuzo.gif" alt="happy-dance" width="250" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Wait, wait, we haven't tested yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;For testing I will just &lt;a href="https://github.com/facebook/create-react-app" rel="noopener noreferrer"&gt;create a react&lt;/a&gt; app using &lt;code&gt;create-react-app&lt;/code&gt; and do &lt;code&gt;npm start&lt;/code&gt; to run it on port 3000(default).&lt;br&gt;
And then run the magic SSH command&lt;br&gt;
&lt;code&gt;ssh -N -R localhost:4242:localhost:3000 root@42.igauravsehrawat.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;See it in action&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rwwxqyd54xqp1v5ksk8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rwwxqyd54xqp1v5ksk8.gif" alt="localhost-on-internet" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With power of just one server, it is not that hard to have your own setup. You just have your own customized, SSL enable sub domain with no restrictions.&lt;/p&gt;

</description>
      <category>localhost</category>
      <category>internet</category>
      <category>ngrok</category>
      <category>localtunnel</category>
    </item>
  </channel>
</rss>
