How to Set Up an NGINX Reverse Proxy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again everyone and welcome back to linode in today's video we're going to take a look at how to build an nginx proxy now nginx itself has all kinds of awesome features you can use it as a simple web server a load balancer or like we're going to do in this video a proxy and there's all kinds of different reasons on why you might want to create a proxy with nginx but typically it allows you to have something on the public end in front of an internal server that you don't want to have direct access to the public internet and of course you could use things like let's encrypt you could go ahead and encrypt an application with ssl that normally doesn't have the ability to do so but i'll leave it up to you as far as why you might want to do that in today's video what we're going to do is create a nodejs app and we're going to put an nginx proxy in front of it so let's go ahead and get started [Music] now what i've done off camera is i've created a brand new lynn note i called it nginx tutorial and we are going to use this as the example the note for this video so i'm going to copy the ip address to the clipboard and as usual we will use ssh to connect to it paste in the ip address type in my super secret password and now i'm connected so let's go ahead and get nginx installed so for this tutorial i am going to be installing the mainline engine x which is going to require the installation of a key and an extra repository so i'll paste in the appropriate command right here to download the key so basically i am just using wget to download the key right from the nginx website and now as you can see we have the nginx signing key downloaded to our linode and now that we have the key let's go ahead and install it so for that i will type apt hyphen key add and then the file that we've just downloaded i'll press enter so now that we have the key on our lynode we can add the repository so for that i'll use nano basically a text editor to edit slash etsy apt sources.list.d nginx.list just like that and here i've pasted in the line that we need for this repository to be added now pay special attention to the word in red right here where it says focal and that is the code name for the ubuntu 2004 release it's codenamed the focal fossa so if you are using a different distribution with a different code name you'll want to make sure that you change it accordingly but if you are following along with me and running on ubuntu 2004 then this should work just fine so i'll save the file i'll hold ctrl and press o that'll bring up the save dialog i'll press enter to save the file and then control x to exit out of the editor now that we have the new repository file we need to update the repository index for apt and to do that we will run after update and then press enter and as long as you have no errors you should be good to go i see no errors on my end so next we can actually install nginx and for that it's apt install nginx simple enough i'll press enter and now we have nginx installed we'll want to make sure that it's actually running so what we can do is type systemctl status nginx i'll press enter and as you can see it is not running it's inactive as it shows right here and it's actually enabled which is great because that means that it will start when our lin node starts so if i was to reboot the linode then that means nginx would come right up i'll press q to exit out so since nginx is not running as i mentioned it's inactive we can go ahead and start it up so for that systemctl start nginx and then if we check the status again we can see that it is now active and running and if it doesn't show enabled on your end then you can simply run system ctl enable and then engine x and that'll make sure that this service is enabled and will start up when your linux reboots so now that we have nginx installed we can make sure that we can actually reach it from the internet so back in the dashboard i'll copy the ip address just to make sure that that's what i have in my clipboard and then in a new tab paste it in right here and press enter and it says welcome to nginx so what that means is that nginx is working it's installed and it's ready to go now of course we'll want to configure nginx and the nature of today's video is we are going to set up a reverse proxy but already we can see that nginx is working and available from the public internet that's pretty cool so in order to set up a reverse proxy you'll need to have an application running that we can proxy too off camera i set up an express app in node.js which is running on port 3000 but this can be anything from a docker container to a snap package a java application or even another web host and what i'm going to do is change directory into the etsy nginx conf d directory and as you can see we have a default.com file right here so i'm going to create a new one i will just use nano i will call it nodeapp.com just like that and then here i will paste in the code for this configuration file which looks something like this now what you'll want to do is this line right here where it says server underscore name and then example.com you will want to replace example.com with your actual domain name or the ip address of your lynode so since i don't have a domain set up for this particular node i'll just erase all this right here and then paste in the ip address and that's basically it that's all we need to do to set up a reverse proxy now where the magic happens is this block where it begins with location and then slash we have the proxy pass directive which is basically proxying traffic to localhost colon 3000 and the proxy pass directive itself is what allows nginx to function as a proxy so what i'll do is save the file control o enter and then control x and then next we need to take care of the default.com file and what we can do is simply move it to a new name so that nginx can't find it so i'll just type mv for move we'll move the default file and we will move it to the same name dot disabled press enter and then we can actually test the configuration with this command right here engine x-t i'll press enter and awesome is telling me that the syntax is okay and the test is successful and now we should be able to reload nginx with nginx dash s and then reload that'll reload the nginx configuration so now i will go back to where i created the application and inside that directory i will run node and then the app.js file you can see that it is listening on port 3000 now here on the dashboard i'll grab the ip address paste it in and we should be able to go ahead and press enter right here because we're not going to tell it to go to port 3000 because if nginx is working properly then the standard port 80 should actually proxy us to port 3000 which means that this is working let's see if it is and well would you look at that it says hello world right there and that's coming directly from our node.js app that nginx is basically proxying us to which is awesome so now this app is accessible from the public internet so with this you can go ahead and develop your own node.js app and run it on your lynode and expose it to the public internet through the proxy now one of the main advantages here is that you can use let's encrypt certbot to create an ssl certificate to secure your lynnode that's beyond the scope of this video we already have a video on the channel that shows you how to do that but now that you have an nginx proxy you can now set up a let's encrypt certificate on your app that'll help it be more secure and that's all there is to it nginx is awesome setting up a proxy in front of an application is just one of many use cases that nginx can handle for you and a proxy is definitely a useful thing to have as you just saw we were able to put a proxy in front of a node.js app and that's pretty cool but i'll leave it up to you as far as what you might want to put nginx in front of as a proxy there's all kinds of different use cases out there and nginx is very flexible and i'm sure it'll facilitate your needs and i hope you enjoyed this video as always make sure you click that like button and that subscribe button we have more content coming very soon so stay tuned [Music] you
Info
Channel: Linode
Views: 54,282
Rating: undefined out of 5
Keywords: linode, linux, cloud computing, web server, nginx, nginx reverse, nginx reverse proxy, nginx reverse proxy ssl, nginx reverse proxy setup, nginx reverse proxy configuration examples, nginx reverse proxy configuration, reverse proxy, reverse proxy nginx, nginx tutorial ubuntu, nginx tutorial linux, nginx reverse proxy example, reverse proxy nginx tutorial, reverse proxy setup, tutorial, linux tutorial, vps nginx, nodejs, reverse proxy tutorial, nginx tutorial, sttp server, vps
Id: B62QSbPhh1s
Channel Id: undefined
Length: 9min 50sec (590 seconds)
Published: Wed Dec 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.