NGINX Linux Server | Common Configurations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to lino in today's video it's all about nginx i'm going to show you the overall configuration layout and give you an overview of how it's set up and configured so without further ado let's go ahead and jump right in [Music] for the purposes of this tutorial i went ahead and set up a brand new lynnode running ubuntu 2004 that i cleverly named nginx tutorial as you see here so what i'm going to do is grab the ip address i'll copy to the clipboard and then i'll go to my terminal and via ssh i will connect to the lynode and now i am connected to my linode via ssh i have zero updates that can be installed that's just something to keep in mind you should always make sure that your lynnote has all the latest patches and mine does and we should be good to go to get started so what i'm going to do is give you an overview of the configuration for nginx so obviously you will need to have nginx installed already so what i've done off camera is i went ahead and set up the official nginx repository and then i installed the nginx package so now we should be good to go so in order for nginx to give us any benefit whatsoever we have to make sure that it's actually running and for that we could run systemctl status nginx just like that and in my case it is running as you can see right here and since our linux is accessible from the public internet we can go back to our browser open up a new tab we could paste in the ip address right here for our linux and press enter and as you can see here we have the default nginx welcome page so we can see that it's working but how exactly does it even work to begin with well what i'll do is switch back over here to the terminal and then i'll show you the configuration now first and foremost if we go into the slash etsy nginx directory then list the storage so you can see that even an unconfigured nginx has a variety of configuration files installed by default i haven't actually configured nginx at all so what you're seeing here is the default installation now the first file that we are going to take a look at is the nginx.conf file so what i'll do is open that up in an editor now the first thing to know about nginx is that there are several contexts by default and the first four items we have user worker processes error log and the pid also referred to as the pid these four items here are part of the main context they're not part of any specific server block these are config lines that are actually for nginx itself so here we see the user is nginx so if i background this editor right here and show the contents of etsy password we can see there at the bottom the nginx user so when i installed nginx it created a user for itself as well and then if i run ps aux and then i grep for nginx we can see here that nginx the master process is running as the nginx user now the text has wrapped off the edge a little bit because i have increased the font size but as you can see here nginx is running as the nginx user back to the config file we understand now that the user that nginx runs as is the user also named nginx and then here we have worker processes it's currently set to one and as you might be able to guess this determines how many worker processes for nginx will be running in the background now a full description of how this works is beyond the scope of this tutorial but essentially the more worker processes you have the better performance you will get however if you set it to too many then you can actually suffer negative impacts like slower performance so this is something that you have to tweak to make sure that you have everything set up properly the nginx documentation recommends that you set this number to the number of cores that we have available so if i background this yet again and cap the contents of slash proc slash cpu info and then scroll up a little bit here we only have one core we have processor zero we see it on this line right here and if i scroll down we don't have another processor so we have a single core now you can also go to the lino dashboard and take a look at the plan that you have assigned your lino to see how many cpus you have but again according to the edge edginex documentation they recommend that you set the worker processes to the number of cpus that you have on your server for the error log you're basically choosing where the log file for nginx will be if you need to take a look at it and see if there's any errors so the first part we're setting the path to slash var slash log slash engine x slash error.log and then we are setting the log level to warn so if i take a look at that you can see that we have an access log and an error log as well the error log is currently zero bytes there's no errors right now then we also have the access log which is going to show output when our server is accessed so for example if i just go ahead and tail that log file center a few times here go back up to the browser i'll refresh it i'll do it again and why not just do it over and over again and back here at the terminal you can see that it did in fact update the access log every time i hit the refresh button so anyway back to the configuration file now the pid is just going to show where the pid file for nginx will be located there really shouldn't be any reason to change this so i'll go ahead and leave that alone now here we have the number of connections that are allowed per worker it's currently set to 1024 this is the total number of connections that a worker may be able to have so you want to take that into consideration if you are going to be serving a website that potentially will have a ton of visitors there's no magic here basically you want to go ahead and customize this as you see fit according to the use case and popularity of your server now i'm going to scroll down a bit here and we have this http block right here which is going to be probably where you'll spend the majority of time customizing your configuration now a full walkthrough of the configuration here is beyond the scope of this video but essentially when you are configuring the web server itself and how it's accessed then this is the block where you will add that configuration we also see the access log identified right here and if you want to go ahead and change that to a different name for example you may do that you can set things like the keep alive timeout and down here we actually have an include statement that is including everything in slash etsy slash nginx slash conf d that ends in a dot com file name so if we take a look at that we have the actual directory that it's referring to right here and then inside there we have a default configuration file so let's take a quick look at that one too so when you are dealing with virtual host which essentially means that you have more than one website running on your server then you may have more than one configuration file now this default configuration file is well a default configuration file so this is actually what is serving the welcome page that we saw earlier so back here this welcome page right here where it says welcome to engine x you can see where that's actually located so if i grab this here and paste it back into the terminal and then go ahead and type out the full path we can see the actual html page that is being served whenever we go and access our linux by the ip when nginx is installed and we haven't yet customized the configuration so we see the title welcome to engine x and then we have a paragraph tag right here which has the verbiage that we see right here so back here on the default config file we were looking at this location block right here which is setting the root path to this directory and that the index file is going to be either named index.html or index.htm and then here we have an error page setting that we are looking for errors of 500 502 503 or 504 and if one of those errors does come up then we are actually going to send them back to user share nginx html now this whole thing right here is known as a server block now one thing to keep in mind is that the directives and syntax here might change a little bit depending on how you've installed nginx now again i've installed nginx from the official nginx repositories if you went ahead and installed the package from the ubuntu repositories then you'll also have a sites available and sites enabled directory that can contain config files as well but anyway this is a server block and basically what this is doing for us is it's allowing us to configure the nginx server itself we could choose the port that it listens on and by default of course that's port 80. we have the server name right here and this is actually pretty important because if you have an actual domain name attached to your linux then you should put that domain name here in place of localhost and the way that would look i'll go ahead and back this out is if we had a domain maybe something like www.example.com we would put it there but what if somebody types example.com without the three w's at the beginning should also look for example.com as well without the www so we could go ahead and serve traffic that is looking for just the example.com without the www and serve traffic for that as well now i've already gone over the location block right here and then here we have the error page config which is just basically going to help us configure what happens if we have an error while something is attempting to be accessed if i scroll down a bit we have quite a few comments so i'll just go past all those and then we have the closing brace right here now back up here this server name field right here is basically allowing us to do name based virtual hosting now the situation might be that you may have more than one website running on your lynode and if you do then you'll want to have a different config file for each website so if i was to go ahead and set up a second one then i would maybe make a copy of this file right here i would change the path for the root so that it would be serving a different index file and then i would set the server name to something else so again right here we have the location block and this location block is set to a single forward slash what that means is that essentially if anything is being accessed at example.com or www.example.com basically no sub path at the end then this path and one of these files is going to be served to the user we can have more than one of these so for example i can go down here and type another one and this is just hypothetical i don't actually have a blog installed on this linux so what i've done is i've added a second hypothetical location block so that way if somebody accesses the domain slash blog then they will be shared files from this blog directory here if i did create it and then one of these two files now if you wanted to create a completely separate website then you basically want to grab this default.conf file right here just copy it to a new name change the paths and have it be its own file now it's important to understand that this is case sensitive by default so for example if somebody typed blog at the end of the domain name with a capital b this would not match and this content would not be served now there are ways that you can set up nginx to be case insensitive and if you're interested in seeing how that works then definitely check out the wiki page that matches this video for some examples for another example of how that works if i go into the user share nginx html directory look at the contents here we again have the index.html file and if you recall it's this page right here so essentially the way that this works if you take the domain maybe something like example.com and then you access index.html and what it's actually serving is the full path so user share engine x the path that we're in right now html index.html so essentially this url maps to this path on the file system of the lynode now you don't actually have to include the index.html so i could take that off because in the configuration file it's looking for that file name by default so that's why we are able to type simply www.example.com and then what the user sees is this file right here that is located in the user share nginx html directory and again this is that line right here that is telling nginx to look for files with those names if somebody is attempting to access the root domain and the single forward slash represents the root of the domain now what i'm going to do is close out of this file right here i'm not going to save any of the changes that i've made and we can go ahead and take a look at some examples of how to restart nginx so for example if we do make changes to the config file for nginx then we can restart nginx with ctl restart nginx just like that and then if we check the status we want to make sure that if we do make changes to the configs that nginx is actually running and didn't error out so what we want to see is active and running like we see here we can also see that it's running as of 8 seconds ago so it did successfully restart now the problem though is that if you restart nginx you could be dropping connections and that can make your users well fairly annoyed one thing you could also do is reload nginx so for that we could do systemctl reload nginx just like that and then if we go ahead and check the status again you can see that it's been running as of 43 seconds ago so it didn't restart the entire process but it did reload the config and we could see that right here reloading nginx so it was able to reload the config file without actually restarting the entire service then of course if you would like to stop engine x that's simply system ctl stop and then nginx just like that and if we check the status we see that it's not running it's inactive as we see right here and then if we refresh our website here it's unable to connect so nginx is stopped then of course if it's not already running you can go ahead and start it back up and now we are able to access the default web page once again so there you go i hope that was helpful and as always make sure you click on that subscribe button if you haven't already done so we have more content coming and if you need more information on nginx make sure you check out the documentation article that accompanies this video for even more information that you will definitely find helpful thanks for watching and we'll see you [Music] again [Music] you
Info
Channel: Linode
Views: 37,495
Rating: undefined out of 5
Keywords: nginx tutorial, linux tutorial, learn nginx, nginx tutorial linux, learn nginx by example, linux server, linode, linode server, linux nginx restart, linux nginx stop, linux nginx setup, linux nginx reload config, linux nginx test, nginx configuration, nginx configuration tutorial, nginx tutorial for beginners, nginx linux tutorial, configure nginx server, nginx setup, linode server setup, linode vps, linode nginx, nginx tutorial ubuntu, linode server configuration, ubuntu
Id: MP3Wm9dtHSQ
Channel Id: undefined
Length: 17min 27sec (1047 seconds)
Published: Wed Dec 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.