Skip to content

Instantly share code, notes, and snippets.

@bidipeppercrap
Last active July 28, 2020 16:15
Show Gist options
  • Select an option

  • Save bidipeppercrap/26c68b0df4dc8205df02dd2565b93c60 to your computer and use it in GitHub Desktop.

Select an option

Save bidipeppercrap/26c68b0df4dc8205df02dd2565b93c60 to your computer and use it in GitHub Desktop.
Expose localhost to remote server using SSH and Nginx

SSH Tunneling

Case

I want to expose my localhost:5000 to my remote server ip <ip>:3000 (Optionally with domain).

Answer

In terminal, run this command: ssh -f -N -T -R 5000:localhost:3000 user@<server-ip>
You must be seeing your pointer stuck in the terminal.

Login to your remote server, now try to make a request with curl localhost:3000
See if you get the expected response.

That's it, you're done!

This will run the ssh in your machine backgroundly (-f flag does this).
Of course you need to re-run this command everytime your computer restart.
If you want to run this automatically everytime your computer start, you can add startup task or something similar.

If you want to link it with your domain, proceed to Nginx Reverse Proxy (You need to install Nginx first).

Nginx Reverse Proxy

1. Configure Nginx

After you port forward your localhost:5000 to remote server's localhost:3000, you also want to access it with your own domain.

With this, you can access your local machine from the internet!

In your remote server, create a nginx configuration file:
sudo vi /etc/nginx/sites-available/your-domain.com (you can replace your-domain.com with your own domain or any name you want)

Then write these:

server {
        listen 80;
        listen [::]:80;

        server_name your-domain.com www.your-domain.com;

        location / {
                proxy_pass http://127.0.0.1:3000;
        }
}

Replace your-domain.com and www.your-domain.com with your own domain.

2. Apply Nginx configuration

Symlink the file to sites-enabled (Tells Nginx to enable this config):
sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/your-domain.com

Reload the Nginx service:
sudo systemctl restart nginx or any restart command depending on your OS.

3. Configure DNS

In your domain provider website (namecheap.com, godaddy.com, etc.), go to 'DNS Manager' or 'DNS Setting' or whatever it is depending on your provider.
We want to create an A Record for our remote server ip.

Add an A Record and specify the Host to @ and the Value to your remote server ip (Don't include http://).
Also create another A Record for the www host.

Record Host Value
A @ xxx.xxx.xxx.xxx
A www xxx.xxx.xxx.xxx

If you want to use subdomain instead, change the Host to whatever subdomain name you want without including the main domain name.

Host: mysub.your-domain.com is wrong! Just specify the subdomain name only, eg. Host: mysub

Also don't forget to create another A Record for Host: www.mysub.

Record Host Value
A mysub xxx.xxx.xxx.xxx
A www.mysub xxx.xxx.xxx.xxx

Save your DNS settings and wait for a brief moment to let it apply (Takes around seconds or minutes).

Finally

Now you can access your local machine port from your-domain.com, or mysub.your-domain.com if you prefer to use subdomain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment