Nginx Mastery | Static Files | Multiple Domain | Local DNS configuration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the nginx mastery series  my name is xander in this tutorial we're going to   be taking a look at serving static content if you  have missed any of the tutorials before this one   then you can go ahead and access all the tutorials  from the main channel go ahead and look for the   playlist and you're looking for nginx mastery  playlist this is the third tutorial in this series   if you have missed the first two then these are  introductory tutorials so it's well worth going   back and watching the first one and then moving  back over to this tutorial at a later point   however if you're only interested in this  tutorial no problem all the code is in the video   description and you can follow along so in the  first tutorial we took a look at utilizing nginx   with docker and we will continue utilizing  docker so if you haven't got docker desktop   go ahead onto google and download docker desktop  and get that ready in the second tutorial we   looked at what's happening behind the scenes  with nginx so now we're ready to start thinking   about configuring nginx to perform some sort  of action and in this tutorial we're going to   set things up further by configuring or having a  look at configuring nginx with multiple domains so one of the issues when working or practicing  utilizing nginx is once you start needing domain   names that's when we can come into some problems  because potentially you don't actually own any   domain names so when you're trying to configure  your configuration files it can make it difficult   to set up your domain name locally and you end  up potentially having to deploy to a server   buying a domain name and setting up all the dns  and so on so what we're going to try and do in   this tutorial is get things started so that we  can work locally with nginx so in order for us   to serve multiple domain names we're going to need  some domain names now normally like i said you'd   purchase domain names but what we're going to do  here is we're going to configure a lightweight   local dns server in a new container and that's  going to act as a dns server as if it was on the   internet but locally and that's going to allow us  to configure our nginx server with domain names   having a local dns server can be quite handy  when you're developing in certain situations so   this doesn't necessarily just correlate to  working with say python if you're working   with php or javascript frameworks and so on  so this can be a fairly useful thing to have   so it is going to be really lightweight it's  around about 24 meg the setup so it's quite simple   to set up now i am using a mac machine here so i  will be giving you instructions for using a mac   you should be able to translate that into windows  quite easily and i will give you guidance on   what to look for there but overall this is  a mac uh tutorial and there will be just   one or two slight changes that you'll  need to make if you're running windows so there is a little bit of a setup  here won't take too long there is a   link in the video description that would  take you to the nginx mastery series   part two what you need to do is just download  this and open it up in visual studio code   so you don't have to use visual studio code  of course but if you want to follow along   step by step and we are going to be utilizing  some extensions um interacting with docker so   it can be handy utilizing visual studio code for  this tutorial so once you've downloaded that code   and opened it up with visual studio code you'll  find there's going to be a folder and two files   so first of all we're just going to tidy this  up a bit and prepare it slightly differently   so that we can work with nginx and create a dns  server so first of all we're going to create a   new file folder sorry called nginx i'm just  going to place everything related to nginx   inside of that so here what we have is a the html  default kind of location if you like um or folder   where we serve static web pages from nginx  so we're just going to move that over to this   folder here the nginx folder so put that in there  and inside of this folder i'm going to create a   new folder called main because i'm going to have  multiple domain names eventually so i'm going to   have multiple domain names that's going to serve  multiple websites so i'm going to need multiple   folders so inside of the html folder here we've  got a main folder for the main domain and i'm   just going to drag the index page into that folder  there so we now have well that's not very clear we   have a folder here called main inside of that we  have the index file so that's just a a simple html   template there which is going to serve a simple  html page just to replicate a website that we   might want to serve so next up we're going to make  a few changes then so go into the docker compose   file from here and there's a few changes that  we're going to need to make let's just go in the   docker file first let's sort this out first so  we're going to potentially copy copy this file   over to the to the new image that we're going  to build and that's got a new structure now so   let's just go ahead and get rid of that so we're  just going to serve the html folder and everything   inside of it we're going to copy that over to the  new image and then container so we'll just get rid   of that so that should be absolutely fine now so  we don't need the trailing slash natural fact okay   so that just tidies up this dockerfile now we are  going to move this docker file now inside of our   nginx folder so i'm just going to drag that  over the nginx folder there so that should   now drop into the nginx folder so that's now  been placed there and of course now we need to   just update this context here so it loads the  docker file inside of our nginx folder right so   that's now nicely connected up so everything at  this point should probably work absolutely fine   um yeah so let's go ahead and actually build this  so assuming now you've got docker installed um   let's make sure you've got docker installed just  double check you haven't got anything open etc   okay so i've got a few things  here let me just get rid of   some of these things here well i can't do that  because i need to get rid of the container   so i've already downloaded the nginx default  or the um the nginx image from the docker   hub and that's the official image but if  you haven't got it it's okay it's going to   download in a second you can see it's quite it  can be quite large it might take a few seconds   to download in a second so that's all now  ready so let's go ahead and just check to   make sure everything is working okay so we're  going to do that by running docker compose   and then build that's going to  build a new build so again build   and that's going to build a new image so what's  going to happen now we're going to read this here   it reads the context it's then going to read  this file here it's going to download nginx which   we do already have the latest image and then it's  going to make some changes to that image and then   that's the image then created so now we can then  go ahead and run that now if you do have or if   you don't have the docker extension installed  i'm using the docker extension here in visual   studio code so go to extensions type in docker  install it and then you will then find docker is   you can then manage docker from this new icon  here so you can see that we created this new image   that's what we've just created so let's go ahead  and docker compose up and then we use the d flag   so that's just going to run behind the scenes  there and you can now see we have this container   up and running good right so let's test this  out so in the previous tutorial we've been   utilizing port 8000 uh and localhost so let's  just try this again and you can see that we have   forbidden right so there's there's probably three  things here the reason why this could be the case   it could just be a general uh configuration error  somewhere maybe um some type of typo potentially   secondly this could be because it cannot find the  default page the index page and that's probably   why it's uh producing a 403 here now  in addition to that in addition to not   having an index page a default page to serve it  might be that there isn't any uh files at all   uh or it might be looking in the wrong place where  there isn't an index page to serve and this is why   you're receiving a 403 forbidden now we could also  have issues here with permissions so it doesn't   have permissions to actually run the file so that  could also be a potential issue that you would   need to resolve and obviously it's hard to give  you instructions here exactly what that might be   but let's have a look at our configuration and  see why this might be forbidden so running the up   commander with the d flag we don't get any of the  output from the container that can be quite handy   in this case so what i'm going to do is i'm just  going to remove this container and just run it   again without the d flag in this case um so dock  and compose up and then without the d flag and you   can see that we get a little bit more information  here so i'm just going to run this again refresh   and this time we've got some information so it  looks like here it's looking for the directory for   some sort of file uh user share nginx and then  slash one so um we've got a host of localhost   8000 so maybe we're just using the wrong  port um or maybe not so let's have a look   in the files again so we are working on  port 8000 okay that's absolutely fine um   and let's just see we've got the volume here h  slash html so we're trying to copy this volume   over notice here this is the issue right so the  volume that we're copying across over to our   container or mirroring across to our container  is this folder here and it's completely empty   so in actual fact we need to update this and to  tell docker that our html folder now is in the   x folder and then html okay so i  can get rid of this folder now here and then hopefully what i  can do utilizing the docker extension here right click and i open  up a new shell here like you just saw   and then i can probably pass a new  command so um let's go for engine x   and then we're going to pass a reload command  over and then we're going to refresh and this time   we've got a 404 not found okay so we're getting  closer so let's go back into the shell here   and see what's happening so this time um it looks  like we're getting closer so localhost request   localhost 8000 um so we've still got potentially  an error here no such directory found so you can see what's happening here we're looking  in user share nginx html and then we're looking   for the index page so we're not actually looking  in the main page so it's kind of looking in the   wrong place at the moment so we're going to  need to create some new configurations for   that now at the moment what's happening here  is the default configuration can be found so   if we go into the docker if you've got the docker  extension now i can go into the files here on my   container we can have a look at the configuration  files and you're going to find those in etc   and then nginx and inside of here the comp d  and this is the default setting so i can open   this up and you can see what's happening  here is that the root location is the html   folder but that's not what we want we want it  to look into main the new main folder so this   is an important file that we're going to be  working with in this tutorial so it's probably   better for us to actually um mirror this across  using a volume over to our machine so we can work   with it directly rather than having to keep going  into this folder directory here so let's just um   make that change so we've made the the change  here to main so it looks in the main folder   not the html folder so let's go back into our  shell and just run the reload command again   apologies if i'm going too quick  here um i was assuming that you've   followed the last two tutorials so you have  a general idea of what we're looking at   um so i reloaded everything so if i refresh now  you can see we've still got the same issue if we   go back uh so still no such file um or directory  we still have that error so um we're looking okay   okay so we've got main here so not to worry what  we're going to do then is we're just going to   create a volume and copy this across over to our  machine so let's just do that first and we can   then sort out everything in a while so let's  go ahead and just shut everything down first and i'll just get rid of the image that  we created earlier so that we're starting   fresh again and then we get rid of the config  file so the idea is that we want to take that   config file and we want to make it available  here in this folder so we can easily access it   so what we're going to do first is create a  new folder here called conf dot d so that's the   folder name and then inside of here we're going to  create a new file it's going to be called default   now what we could have done was just copy and  paste the original file that we just saw over   but we're going to start from scratch and  i'll just go and take you through this step   by step so you have a better understanding  of what you're doing inside of this file right so now we have that folder in place when we  build we'll want to actually kind of make a volume   so that that kind of copies over to our container  so let's go back into composer we'll create a   second volume i've just pasted that in just for  speed but you can see what's happening here we're   going to copy engine engine x sorry the folder  comp d and then we're going to mirror that across   to our container etc nginx company that's where  we were right so anything inside of this folder   is going to be copied across at the moment it's  completely blank because we're going to work from   scratch here so with that in place uh let's go  ahead i think we're now ready to copy everything   over but before we do that in actual fact we're  going to start to utilize port 80 right so we're   going to get rid of port 8000 we're going to stop  using port 8000 we're going to use port 80 which   is the default port for http internet traffic  so uh make sure we've changed that too right   okay with that set then let's now go back into our  console here and then we're going to go ahead and   run the same commands as we did before so i'll  just press up to bring up the previous commands   docking compose build make a new  image and then up and we will yeah up   okay so that's that ready now the problem we've  got now is that in the container we've copied   across default which is completely blank so i  just want to take you through the basic steps   of configuring a web page a static web  page here in your configuration file   so let's think of this file as the work that we  want nginx to perform so we're going to define   here the work that we want nginx to actually  perform so one thing that we can do here is   define a server block and this is basically  a way of telling nginx that we want it to   basically listen or we're going to define or um  describe that we want it to listen for a port   and then when it finds uh requests on that port  it's going to perform some sort of action so we   know that internet traffic typically utilizes  port 80 so we're going to ask nginx to listen   on port 80 for any kind of incoming requests  now you might see in the original config file   to listen just described um and it looks a little  bit like this and what this is is basically an iv   version six set up so if you are utilizing ip  version six just check out the documentation   and utilize that so we are using ipv4 here so  we're just going to describe listen and then put   80. right second thing we have is the server name  so this is going to allow us to set up multiple   domain names so notice that this server will be  running from one ip address so we're going to   send all data to this one ip address so imagine  we have multiple domains we're going to send for   example domain1.com to this ip address and then  domain 2 to this ip address well that's okay   but how does the server know what files to serve  correlating to the domain name the users requested   well this is where we can use server name so if  we have for example a domain called d.com then   anything related to d.com that's been requested is  going to be served from this code block here and   what we can do is we can set up a separate domain  maybe e.com and everything that's been described   in here will be served from this server block  here so this is going to allow us to differentiate   differentiate different settings and  configurations for different domains   and we'll have a look at that later we'll just  get this started to begin with so we're just   going to use localhost to begin with and now  we can start to think about uh the location   so hellocation right so location here is  going to allow us just to define for example   uh the root directory for our website where  we're going to serve the static pages so   we know it's in user share nginx and then html  we've made a new folder called main so that's   where we want to serve it from don't forget  your semicolons there to end the statement   um so that's that in place and then we can  describe what page we want to load first so   that the the default page to load because the user  doesn't normally type in the page you know we just   type in google.com or bbc or amazon.com you don't  actually describe what page you want to open first   and that's where this comes in so our default  page is just going to be called index.html and   you can add as many as you like i'm just going to  add one because i know that i'm using index.html   so in the default settings you'll probably also  see the error pages set up so you can also set   up multiple error pages it's out of the scope of  this tutorial i'm just going to keep it nice and   simple for now and just um set this up and get it  running but here what we've done is we've defined   or we've asked the server to listen for port 80.  if it receives a message from port 18 it matches   this server name then essentially we're going to  serve this location and in this location we're   looking for a file called index.html and  we're going to serve that back to the user   okay so now we have a configuration file our  server is running so let's just is it running   no it's not running so let's  go ahead it's running so   let's go ahead and attach a shell so that we can  go ahead and reload with this new configuration   so from here i'm going to type  in nginx and then do a reload   okay so let's just see if that has any effect  um it doesn't seem to have any effect at all   okay so um let's just go ahead and shut this  down because we've made quite a few changes   sometimes it can just be more effective to start  from scratch again so i'm going to do exactly that okay especially when you're starting to begin  with this technology um sometimes there can be   cash and all sorts happening so it can be better  to start from scratch so um let's go ahead now and start a new container again and takes a couple  of seconds we're going to build a new container   and then we're going to bring the container  up and then we're going to give that a go so   now we're going to refresh and you can see  that safari cannot connect to this server   so if we go back into docker here the server is up so it looks like it's running  okay but potentially we've got some errors that   we need to resolve one thing that we did change  was the port number so we need to be careful of   that so here um localhost 8000 is no longer what  we need we can use to use the default logo host   now it will default to 80 that is a default port  so we don't actually need to type in the port 80.   so we press enter and there we go so that's the  html page that is now being served to the user   so now we've got everything up and running  eventually you're going to want to map across   your domain name to the server now typically that  includes you having to purchase a domain name   and then you set up the dns and then you go ahead  and point that to your server online and then you   install this onto your server and then that goes  ahead and serves your website now the problem here   is that we're just experimenting we're just  learning nginx so we don't want to go to the   expense of buying a domain and then setting  up a server online and then keep uploading   new configurations and so on we want to still work  locally so one of the hurdles of actually having   this type of setup locally is that the domain  name is being resolved by dns server online   so that would mean potentially if we wanted to run  this locally we would need to point the dns server   over to our ip address our local machine here and  then we would need to go to our router configure   port forwarding so it put forwards over to this  machine that we're working on and then it could   potentially work but again that's quite a big  faff so what we're going to do in this tutorial is   set up a dns server a local dns server and that's  going to resolve domain names for us locally   ultimately that means that we can use any domain  we want and then go ahead and continue configuring   our nginx server as if it is configured for online  use and potentially what we can then do is make an   easier step of integrating that onto our live  server online later on so let's just go for a   quick refresher for anyone who's forgotten uh the  general concepts of dns server and how that works   within this browser internet web web page  setup right so this is our machine here   uh depicted in this rectangle here and inside of  our machine we have a browser now normally our   computer is set up with an ip address but in  addition to that is also set up with a dns address   and that's normally then provided by your  internet service provider so that gets   configured straight away on your computer right  so what happens is you've typed in amazon.com   so the internet cannot root names so it doesn't  know anything about names the routers and switches   in the internet that passes your packets  and data forward through the internet over   to other resources it cannot read names it  can only read and understand ip addresses so   what needs to happen when you type in amazon.com a  message needs to be sent across to the dns server   now your dns server like i said has already been  hard coded or is already been configured in your   computer so a signal was sent to that address  and then goes online and goes to the dns server   so 888 it just happens to be one of google's dns  servers that you can configure your computer with   so that goes over to the dns server and the  role of the dns server is basically just to   one of the roles is to essentially translate the  name that you've selected over to an iep address   and what gets sent back from the dns server is  an ip address so bam we now have the ip address   of the resource that we want to access and that's  amazon.com so now we have that we can now go ahead   and send a new message out directly to the server  with the corresponding ip address so that goes   over to the server that might be running an nginx  server and then from there nginx can then like we   saw previously you can then select the resources  the web page and then send it back to the user   so that's kind of a general workflow  of when you try and access a web page   what we're essentially doing in this tutorial is  we are essentially just going to put everything   inside of our development environment so we're  going to be working with an nginx container   and now we're going to set up a dns server  container and that's basically going to serve   or translate domains that we select in our browser  over to the ip address of our nginx server on our   local in our local network here so in actual fact  all these ip addresses going to be the same we're   just going to be utilizing the local ip address  sorry local loopback address 127001 so we're   going to set up two domains so the first domain is  going to be called ns.main.com so we're going to   configure that shortly in our dns server and then  we're going to also then just configure a second   domain and you can call it whatever you like it's  very simple to change secondary.com so we're going   to set up two domains in our dns server and the  dns server is going to be accessed by 127.0.0.1 and then our engine x server like we've  already seen that's already configured on the   local loopback address okay so we're all using  the same ip address here so essentially what's   going to happen in our browser we're going to  type in ns.main.com it's going to send a signal   to our local dns server and then we then get the  ip address back and then we can then go ahead   and make a request to our nginx server and  access that resource so like i said there's   going to be two domain names so we're going to  set up two domains in our configuration file and   that's going to be pointed to different files to  different web pages so that's the setup that we're   going to now try and create so the next step is to  then configure our dns server so like i alluded to   earlier um here i'm using a mac and it's going to  be slightly different on windows so where this is   going to differentiate from mac to windows is that  like i said the dns address is already set in your   machine so we just need to override that with  our new dns settings which is going to be the   loop which is going to be the loopback address so  there may be some differences in how i do it here   than if you're doing it in windows but essence  is exactly the same you just need to find the   network settings and add in the new dns address  now that might also conflict with other systems   because we're going to be utilizing a local dns  server if you were to then use another service   then obviously it's going to try and use that and  of course if you're going to other web pages then   that won't work of course because our dns server  isn't serving those web pages it's only serving   us on the domains that we're setting up so we'll  get to that when we get to that but just to give   you a pre warning there all right so let's go  ahead and create a new folder and let's just get   rid of this folder here and we're going to create  a new folder and i'm going to call that dns and   inside of here we're going to need a few different  things but um let's just prepare the docker file   so we're going to create a  new docker file inside of here   so we're going to be utilizing alpine for this as  nice and lightweight so from our pine uh latest so this is a kind of a lightweight image that we  can utilize and then what we're going to do is   we're going to run apt add and we're going to run  something called bind and that essentially is the   dhcp server and then we're also going to need  to start that up so we're going to also need   open rc okay so we run that so we're  going to download this new image   we're going to run this command to install these  packages and then we go ahead and run this so rc   update you and then named that's  essentially going to run our dns server so that's pretty much all we need to do there so  to confirm we're going to download a new image   we're going to install the packages bind b in the  dhcp server open rc being kind of a tool that's   going to allow us to run our server so that's  what we're doing here um once we've done that   let's go now into our docker compose file we need  to make some changes here right so we're going to   need a new service so this service is going to  be called dns and inside of it we're going to   need a few different things so let's uh define our  build so we're going to say build and the context   say dot dns so that's where we're  going to find the docker file   and then once you've done that let's uh just  to find restart so this is a service that you   always want to restart so if it does fail we want  to try and make sure it kind of restarts right   so restart build sorry context let's bring this  back that's right so it's going to try and always   restart so ports then so now we're going to  define some ports so if you've used dhcp before   let's uh just define port 53. so if you're  wondering why we're using this port or indeed   if you're not familiar to ports at all i mentioned  before port 80 and just type it in so you can see   here that port 53 um it can run on tcp and udp um  and you can see that's for domain name services   so this is a specific port that automatically  sends information um on that port if you like   so this is why we need to map it across to our dns  server so the computer is going to try and resolve   the name automatically and it's going to  send that across and use port 453 so we're   going to need to listen out for port 53 in  our container so we can capture that request   so that's why we've mapped that across now  notice that it accepts tcp and udp so we can   also just kind of define our protocol that we want  to use so 53 53 and we're going to be using udp so   we've set that up now so that should listen out  for those requests if you like so now we can say   volumes and now we're going to set up some volumes  so uh we're going to need to set up two different   files so the first file that we're going to  need to set up is going to be called name   name.configuration okay so inside of  here we're going to define some zones   and zones are essentially containers if you  like uh zones are essentially describing   uh the domain names so for example we're gonna  have two zones here one from main.com and one   for secondary.com so for each domain there's a  zone and then from that we can essentially um   bind uh other instructions and commands so  it's a way of kind of uh controlling each   zone each domain if you like so we have that  in place and then secondly we're going to need   um a new folder then called zone so inside of  here in the zone folder we're going to have our   two zones uh so let's go for new file and we're  going to have main.com that's going to be our   first kind of zone and then secondly new file  we're going to call that second secondary.com   so these are two zones and again we're just  going to add in uh information related to   being able to translate in this case the name  into an ip address so there's going to be loads   of information in here which is going to help  perform that translation so these files are are   going to be mapped across to the container so that  we can work locally again this is why we're going   to set this up now in our docker compose file  the volumes so let's just go back in here and do   exactly that so let's map this across a dns slash  um and then we start with the name configuration name.name.config so that needs  to be mapped across to etc bind   name.config so we're binding that  across to an actual file apologies okay so this file here is going to be mapped  across to this location this file here on   our container so we can work with it locally  so that's the first thing and then secondly   is going to be dns zone i'm going to map that  across to etc bind zone i think that's correct   okay so once that's done we're going to  need a command to stick this all together   so command so we're going to say named so this  is referring to the name service the dns server   we're going to essentially just make sure that  it can find everything it needs so etc bind   name.configuration so it now knows where the  configuration is remember we're going to set   the configuration up here this is a configuration  right here we're going to map that across to our   container and then here we're saying basically  run the service and this is the configuration file   that we want to run okay so um with that in place  um i think we're closer to where we need to get to   right so next up then let's start configuring our  service so we'll start with names i guess so let's   go back into our um file called name.config right  so a few things here we need to do we need to set   up some zones like i said so zone this is going  to be main.com zone and then we're going to say in   so type is going to be a master now  there's a whole heap of things here   so this is just kind of a baseline here  right so file and i'm just going to point   to the file that we're going to create with  all the other settings so etc bind zone main dot com okay so that's referring to the  file here main.com right or the information about   main.com so that's our first zone um so with that  in place uh let's go ahead now and go to main.com   and let's now talk a little  bit about setting out our dns service for main.com so this is this  can be known as the dns resource record   this is going to describe the characteristics  of our zone we know that we've got the zone   here main.com and we pointed it to this file  so this is this file here is essentially   just describing the characteristics of our  zone resource records can have different   representations different formats um here  we're just going to try and make it as clear   and simple as possible for you to understand  so typically what you might find at the top   is a ttl time to live and that is basically a  32-bit value indicating how long the rr may be   cached so all cached so tdl uh tdl uh so let's  just set this up for uh eight six four zero zero   so here this is seconds so i'm defining  eighty six thousand four hundred seconds   so now we can configure our resource types so  the first type we're going to set up is s soa yep   so start of authority so we have it at and then  essentially we have the class in and then soa so   a few things here that we need to set up so this  basically defines a few things the zone name uh ns   dot uh was it main.com so that's our domain name  right so you can call that whatever you like i've   just just decided on that and then we can also  define the email address so uh hostmaster.main.com and so don't forget the trailing dot on both  okay so notice we don't need the at here the   format here is using dots because the at symbol  represents something else so that pretty much   sets up that and there are a range of different  configurations here that we can uh define in our   soa so uh for example let's go for in seconds  201 and then this will describe the kind of   cereal the cereal does play an important role  it's kind of out of the scope of this tutorial   and we don't necessarily need to know this but a  zone serial number is is a version number for the   soa record right so for example when the serial  number changes in the zone file this essentially   will alert secondary name servers and that  should then update their copies of the zone file   so it plays an important part so other  parameters for example the second here 600   uh say um so this here will be the refresh and  notice i'm using uh the semicolon that represents   a a comment in this case so the rest of the line  will be commented out so this is a refresh so this   is essentially describing the length of time  again in seconds the secondary server should   wait before asking this primary server for the  soa record to see if there's been any updates   and then it kind of continues  uh what we've got now is uh   the retry right so retry again the length of  time okay i think that's the server should wait   for asking an unresponsive primary name server  for an update again and then we've got expiry   which is probably going to be  normally a little bit longer   okay so that's uh if a secondary server does  not get a response from the primary server   for this amount of time it should stop  responding to queries for the zone   there's also i think a negative  cache ttl but let's just finish   that so we'll just uh finish that right  there so be careful where you put the uh   the ending parentheses so that's uh  that in place so let's uh move on   so we can go ahead and specify a name server  which essentially just defines the authoritative   name server for the domain um to find in our so a  record here um so let's go ahead and do that so at   um it's going to be in and this time we're  going to describe ns so our name server   and that's going to be uh ns.name.com okay  so last of all then we're going to need um   in we're going to need an a record okay so we are  using ipv version 4 here so if you're using ip   version 6 we would use a cool forays um so uh an  a record then basically an ip address for the host   so we just need to describe an ip address for the  host which is going to be the loopback address   and there we go so essentially what we're saying  here is that if we get a request for ns.main.com   then what we need to do is uh return the  ip address so this is the ip address of   ns main name ns main.com so that's pretty much it  really we've just described our ip our domain name   um how to contact the the administrator for that  domain and then where the actual kind of domain   should be ended in this case the loopback address  so at this point we can now go back into our nginx   folder look for the conf file or the default  configuration file we need to just change the   server name because remember now what we're  using is ns.main.com so that's going to be   our domain name so let's just make sure we make a  change there just before we load things up i was   just double checking all the files i went into  the name.config file here notice that i forgot   the trailing semicolon so let's not forget that  so that was in the dns name configuration file   and while we're looking at this we're also going  to need the semicolon at the end here as well   just take a look at the main.com file just to  double check this i noticed i typed a name here   and not main so that's another change that i need  to make so main main main and then finally i will   put some links in the video description so you  can have a read yourself but we're going to add   some options so if you go into the dns named  configuration we just need to add some options   here so let's uh get that done so options and  then we first of all let's go ahead and just   select the directory so basically we're just  defining the absolute path for the server here   right so directory and that's  going to be var slash bind okay so that's going to make sure that everyone  everything is using this kind of base directory   some other options of interest for example  allowed transfer so here you can define for   example ip addresses allowed to transfer or  copy the zone information from the server   so the default behavior is  allows the transfer to any host   so what we can do is we can say allow  transfer we want to turn that off   so let's go for um no so we don't want any  kind of domain transfer so we need to be   careful here the format so we're going to need  a semicolon and then a semicolon afterwards we can also for example utilize allow  query so this is basically going to define   uh or match ip addresses which are  allowed to issue queries to the server   so we can just set that as uh any there we go so in addition we can also for example change  the port that we're going to listen on so   we're not going to do that so let's  just say we're going to listen on   any port so again here this um allows  us to define the port or ip addresses   in which to bind will listen for incoming queries  obviously the deport to default port as we learned   is port 53 on all server interfaces so we don't  need to actually add anything there because the   default port is 53 and we're using port 53 but  that's something that you might want to configure   okay so before we build again just make sure  that we utilize all the semicolons in the right   place so at the end here and yeah just make sure  you've lined everything up if you're following it   step by step so we're now ready to go ahead  and build again so just make sure that you   stop any of the containers i've got rid of all the  images so we're just going to go for a build again   so let's give this a go so um compose build and  then up okay okay so it looks like everything   is working okay so we can double check so two  things we can do we can use an nslookup and   then type in our domain ns.main.com and you can  see that is being resolved the address 12701 so   that's utilizing our dns server to resolve that in  addition to that we can also utilize dig so we say   dig and then um at 127.001 and then ns.main.com  that's going to give us a little bit more   information but you can see we've have an answer  section here which is suggesting that we get an   answer from our dns server and we resolved that  at this ip address so everything looks absolutely   fine so this is where it's going to be slightly  different again windows mac right so if i were   to go to the web page and try out it's not going  to work um it's not happening nothing is happening   here so one thing that we're probably going  to need to do here is to configure the network   right so on my machine i go ahead and select the  the wireless icon and go to network preferences   and i can can't drag that down but one second  inside of here and we've got a few different   options so you start here and we go over to diness  dns and what we can do here in the mac is we can   just define our dns settings so you can see i've  got two here i've got the server dns server and   then we've got the google dns server so i'm just  going to get rid of that and make sure that i've   only got our our own dns server press ok and apply  and now that should be ready so if we go back   into the browser and try you can see it's not  resolving this there's a few issues so let's   go back to our settings here let's go into the  nginx file let's go to the default config file   and let's have a look at the server name so  here we can define a few different patterns   so for example let's define main.com ns main.com  and then we can also use for example the star   wildcard anything basically.main.com so let's try  that out so i'll go back into my nginx server i'm   just going to attach a shell and restart that  that's why it does that uh let's do that again   so let's go for nginx and then let's go for a  reload so let's just see if that is worked so   we'll refresh see we still got some issues so the  important bit here is that um that it when you dry   and completely look up and then when you  try and do dig for example you do get a   response so it's clearly it is resolving  so clearly our dns server is working okay   so in this case it's indicating that there  potentially is a problem with our browser   so i do have chrome installed so let's just open  up chrome and just give it a go here ns.test.com   oh ns.main.com and you can see that it looks like  potentially it's going to work and potentially   there's kind of a casual cache issue here so um  it's difficult one to say potentially if i reopen   um safari everything will work okay but let's  just double check to see if this isn't cached   and that is actually working as intended so what  we can do is if we go into the html index file   here let's just add a that's like on to the end  there so to indicate the fact that we've made a   change now that should instantly make a change  so let's go back into chrome and then refresh so clearly the web page is being served so we have  met our objectives or objective at the moment but   it looks like safari is having a problem  so i'm just going to restart safari   give this a go again so ns.main.com and it still has a problem so just going back to  my network configuration again i can see that i   still have the old dns record and i think this is  what the problem is um so for some reason when we   have dual dns servers it doesn't actually use  the first one it seems to skip with safari to   the second which is the google dns server so i'll  just try again and press apply so i definitely   have changed it there i'm going to close that to  make sure and then when i go back to safari you   can see now it's actually working in safari so  you can kind of see the quirky bits here um that   potentially you're going to have with this now  it this can be set up so um let's not forget that   i've only have one dns server set up at the moment  so let's just go for amazon.com and you can see   that it does work okay so potentially isn't going  to affect the system in terms of using other dns   services on your network um it should work in  hand and hand but you will find that some things   won't work for example if you try and run docker  with that kind of global dns i think docker will   try to utilize your loopback and it won't use  any other dns server so when you are trying to   do things like apt update or app get install  you're going to have hit issues and you're   going to need to put that um put the other dns  server back in now on windows i'm not entirely   sure but in windows what you're going to need to  do is go into the network settings and configure   your dns server similar to what we've done  here but utilizing the the windows systems   if you can't get windows to work i mean it as long  as you do like i said before as long as you've got   the ns look look up working and what you can do is  a curl again probably not on windows directly um   you can curl to it and return back the um the web  page so ns um or called ns main dot com and you   can see that will return the web page so that's  something else you can do just to confirm that   everything is working okay so at that point it's  just going to be a browser or your dns setting   issue so i probably am expecting some comments  here i can't get it working and so on and i do   apologize you can see that it is working here and  it's going to be a case of just following the code   or just making sure that you've set up your dns  correctly now one other thing that will throw   this off if you're using mac is if you're using  the new mac service the private relay service   so what you're going to need to do is go to a  system preference go to your id and turn off   that um relay because this won't work otherwise  okay so that's another thing that you'll need to   turn off if you're using mac and you're using that  new relay system so otherwise it should work okay   so now we've got one let's see if we can work out  how to now add a second domain okay so this should   be really simple in actual fact add in a second  domain so let's start off with nginx uh we're just   going to need to create a new folder in our html  folder here so we're going to call this secondary uh yeah secondary and then inside  here we're just going to copy this   index page here and we're going to call  this hello from i'm going to say secondary okay so now we know that's going to be the  secondary page or secondary website we're   going to serve let's go into our conf d default  configuration so let's just copy this down   uh port 80. so here we're going to use a second  secondary.com um ns.com secondary.com so you can   call whatever you like of course now we need to  be careful here we're now not using main folder   we're using the secondary folder so replace that  too otherwise everything is exactly the same there   so that's now that configured um now we  go into the name config of the dns server   so let's add a new zone um so we're going to call  this secondary.com that's going to be the name okay so that's the file uh so we're going to need  to make file we've already made the file here   so let's just uh copy this from the main  to the secondary and let's just change this   so secondary now it's well worth giving  a go type in google.com uh see if you   can get google.com working it  should be working okay second   dairy.com because it's trying to be served  from your local dns first so secondary.com so just change the name main to secondary so  the ip address is going to be the same again um   so that's that configured yes we've done  this so you can see that what's going to   happen here although we're using the same ip  address like i said earlier it's going to be   utilizing the server name to determine whether  to serve this page or this this index page here   so either this folder or this folder's index  page based upon the server name okay so with   that in place i think we've done the name.config  in the dns we've set up our um secondary.com zone   configuration um yeah i think we're ready to go so  let's go ahead and just start from scratch again   so just get rid of that and then  we'll just get rid of the images so let's go back and let's  uh go ahead and run docker build i just wish i kind of there we go build  there we go so we'll build that and   that should take a couple of seconds  and then we should be able to just   put that up and then hopefully  we've then got two domains okay so if you are working on the mac what  you're going to find now like i said earlier   and this is just a a proof of what i  said um what we're trying to do here   is run our docker compose our docker file  um in the engine engine x and notice here   we're trying to get an app update and install  now because we've set the network preferences   to utilize our loopback dns it's not going to  be able to download those resources so what   we're going to need to do which is a little bit  happy is we're going to need to just add back in   temporarily our dns server that we want to use  so we can then go ahead and run build again   and then it can go ahead and actually perform  these tasks so notice now that is working okay like i said there is a way around  this uh on different os's but we're   gonna stick with this just for now and so  once that's done we're going to go for up   and looks like everything is okay there so what  we're gonna gonna need to do again is just go   into our network preferences and just uh turn  that back off so there's a bit of a pain but let's remove that again apply okay  so let's just uh test this out   so first of all uh let's just uh go ahead and   run ns lookup so it looks like that's okay the  main so let's do the same thing for the secondary so it looks like that is now being  resolved too so there's no reason   why it shouldn't work so let's go  into chrome let's type in ns.um secondary.com and there we go hello  from secondary so clearly we are   now serving two websites or  two domains um from nginx okay and there we go hopefully that was uh of  some use too there might be a little bit of   playing around to get that right for your  particular operating system or your needs   but hopefully that gives you a baseline into  the world of enginex serving static content   of course there's a heap load more that we can go  through but i want to just make it short and sweet   to the point hopefully and i've given you enough  information to start thinking about utilizing it   in this way if that's what you're um is that if  that's what your goal is and if it isn't then this   is just good practice to start to understand  the basic of configuring a static web server   again i hope the idea of utilizing a local dns  server has where you wrap it so i think we're   going to keep moving this forward so we can really  start to build everything locally so that when we   go ahead and deploy it on our server there should  be very minimum amount of actual changes that we   need to make so that just leaves me to say thank  you to the channel members for your continuing   support continuing your continual support  supporting the channel if you are thinking   about supporting the channel further you can do  by clicking on the channel membership or following   us on patreon of course if you're not aware of  all the socials that we're on you can follow   us on twitter facebook tick tock and discord so  thank you very much for listening hopefully you   got this far if you have again thank you very  much and i hope to see you in the next tutorial
Info
Channel: Very Academy
Views: 18,683
Rating: undefined out of 5
Keywords: nginx, nginx static files, nginx dns, nginx multiple domains
Id: GlCloo47p_E
Channel Id: undefined
Length: 59min 15sec (3555 seconds)
Published: Thu Apr 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.