Learn Proper NGINX Configuration Context Logic

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is jai i'm a solution engineer solution architect for nginx based in melbourne australia in today's video we're going to look at how your nginx's configuration context is structured essentially dissecting an nginx conf file and seeing how the construct actually is created let's jump into it what we're going to cover is the basic commands which you use in your day-to-day nginx use we'd look at the default locations of your nginx configuration files and then we'll work with the nginx configuration essentially open it up see how the construct is created what the context and the blocks are and how you can utilize them to achieve your desired outcome and talking about desired outcomes i just like to highlight the fact that nginx with its single lightweight binary can be configured with all of these functionalities you see on the screen right now so a single instance of nginx can be configured as a web server reverse proxy load balance location content manager api gateway all in one if you wanted to however if you're running this in a production environment of course you probably want a few instances of nginx acting as a few of those functionalities in your playground or in your test environment knock yourself out you can configure all of these functionalities in a single instance nginx can run anywhere on any cloud provider bare metal on your laptop and nginx runs exactly the same whether you as a developer are running it on your laptop or someone's running it in a cloud provider a b c or d it doesn't matter it runs exactly the same so let's start by looking at the basic nginx commands which you use so the stock standard command you use with nginx are nginx v which would essentially print the version of nginx nginx t validates the syntax to ensure that there are no issues with the configuration you're about to push nginx that's capital t gives you the configuration currently which is implemented in your current instance of nginx uh nginx does s reload essentially takes the configuration and pushes it out to the existing instance so these are the basic commands a bit later on we'll run these commands in my running instance of nginx and see what sort of returns we get when we run it now the location of the configuration file so default location of your nginx.conf is slash at c slash nginx nginx.com similarly uh the way we try and configure your nginx instance is by putting all the personalized configurations into the config.d directory and you you use the include directive in your uh in your main engine x file to go out and access all those configurations what i mean by that we'll touch upon that a bit later on so before we jump in the demo let's try and understand from the slides how this construct actually looks like so let's look at the configuration context what you see on the screen uh is a configuration file and it consists of combinations of contexts and directives so contexts include main events http stream and others now look this is not this is not a complete list but these are the primary or the top level context the events http stream and within each of these contexts it's got a child context as well so what we have here is the main context and at the very highest level you declare the highest level directives like the number of worker processes the linux username or the location of the paid file or perhaps where you want to try and push out the log file the error or the access or whatever you wanted to configure and at what level events context is used to manage connection processing directives that is the number of connections assigned to each worker process http context defines how nginx processes the http and https connections say for example we can set a pool of backend servers or applications uh direct views used on http context are inherited by its children context that is the upstream the server and the location contexts the server context defines a virtual server so it's also known as virtual host uh which processes a given http request so virtual host is the terminology more familiar with the apache world the virtual server definition can can be a domain name it can be ip address or it can be a unix socket the location context further defines how the virtual server processes an http request based on the specific uri you've defined so location can point to a path on the file system it can also be determined by matching the uri request and matching it to a string defined in the context so for example you've got slash blogs that can go to a specific endpoint or specific file path on the local machine or you've got slash application slash something else it can be routed to a specific path defined within the location block similarly upstream context defines a group of backend applications application servers or web servers essentially to use in a load balancing use case the stream context defines how nginx handles essentially the layer 3 and layer 4 traffic such as tcp and udp directives now the directives are very very essential so use these directories to go out and achieve a specific functionality within your nginx configuration so a direct view is a single statement that controls a given nginx behavior so a block on the other hand what you see over here is is a block block is a grouping of directives encased in curly braces and what we see on the screen here is a listen directive is telling engine x to listen on port 80 and the root directive is essentially pointing to a path in the file system so from slide perspective this is all i had so let's jump to my uh command line terminal that's where i have uh instance of nginx install and let's have a look let's try and run those commands which we talked about earlier so we'll start with typing nginx dash v now this returns us nginx version uh as expected i've got nginx plus install on this box and that's what it's returning now let's go ahead and run nginx dash small t ah and it's giving me arrow so look sometimes when you're running these commands you may need to run them as sudo so if i go sudo engine x plus t what i get back is perfect this is what i expect it goes out and does a simple syntax text and this ensures that the configuration i have are are as expected and there's no syntax errors in it now let's look at our nginx configuration so default location for nginx is etsy nginx so if i go there and do a ls on there you'd see i've got certain files here and a confidently directory but for first let's jump in and have a look at my genex dot com file so cat nginx.com and let me scroll up a little and as you can see in the main context i've got the user which is nginx the worker process is defined in the events context i've got the number of worker connections defined this is default uh it is completely fine to go with default settings however if you uh if you're trying to achieve a specific performance related output you go out and tweak all of these parameters what i have what i have here in my http context are some directives which essentially go out and set my log it sets an access log keep a live timeout but the most important uh directive which i've got here is the include directive but i say most important in context of this discussion so include director essentially tells the nginx main file to look into the config.d directory for wildcard anything.nginx conf and all our custom logic which you're going to create is going to be contained within this confluently directory so let's get out of here jump into the computer directory and see what we have there so with this i'm going to do cd conf dot d and do a ls in the contour d directory so i can see i'm in my slash slash xc slash nginx slash config.d directory and i've got two files default and webconf now before i open up those files let me run one of that command one of those commands which which we talked about earlier so i'm going to run nginx that's capital t and what this command is going to give me as an output is everything what nginx sees as within its configuration so let me hit that hit enter ah sorry nginx t so we run that and it's given me a complete output of what my nginx conf is and what nginx actually reads so point to note here is that nginx reads the configurations included with the include parameter in alphabetical order so if your configuration have say for example certain variables or specific configuration which is dependent on a certain logic it might only be processed in that alphabetical order so uh your custom logic might not be processed if say the the configuration file you've got starts with a z and something else starts with a so on this output let's just scroll down and have a look so i've got my conf file called web.conf which is read the last and if i look at another file which is default.com that's right before it so this goes to show that how nginx reads all the include uh included files perfect so with that let's jump in mydefault.com i'm going to do cat default.conf and as you see uh this is my server context and within the server context i've got a listen directly essentially it's telling my nginx instance to listen on port 80. the server name directory i've got here as localhost we can look for more details on specific directives on nginx.org that's where a detailed explanation on all on what all these directories do with examples is provided also in this instance what it's doing it's listening on port 80 and the location is goes out to this specific route so that's where it's going to uh route my specific request so that's where it's going to try and find an index of index.html or index.htm so if at this stage i was to do curl local host um 80 you don't really need to do that and hit enter what you get is the default web page for engine x so this is from the location slash user slash share slash nginx now i've also got another file on the system here so i'm just gonna do ls to show you what that other file is now the file is web.conf so let's open up the web.conf and see what we have here so i've created a couple more servers or virtual hosts over here so it's listing on port 9001 and in this case the location is for slash i haven't given it to uh do a part on this specific machine it's just giving me a simple 200 return and a can message which i have populated over here similarly i've also created another server on port 9002 so at this stage if i was to do curl localhost 9001 and hit enter i get the message which i've populated over here so all it says is you have reached application one similarly if i go to 9002 it will give me this message back well the guys so look uh in terms of looking at the basics of uh nginx configuration be constructed deconstructing it and understanding the the various contexts of the config file uh this is all if you have any questions feel free to reach out to me or write your comments below and always happy to help with that thank you all for watching the video thanks bye you
Info
Channel: NGINX, Inc
Views: 2,658
Rating: undefined out of 5
Keywords: NGINX, NGINX Plus, NGINX Controller, NGINX Load Balancer, Load Balancer, NGINX API Management, NGINX Unit, NGINX Open Source, NGINX OSS
Id: C5kMgshNc6g
Channel Id: undefined
Length: 12min 55sec (775 seconds)
Published: Tue Jun 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.