Welcome intro etc
Demo setup
ubuntu@54.190.3.225
I have added your private key
Script files are in ~/webinar
http://www.snarketing.net (public DNS)
Explain these are in no particular order, but we’re starting with the only one that means messing about with the main context.
Explain file descriptors
You can see them in /proc for your process
The default limit is 1024 and you can blow through this
Explain how many you need
This is how to set them.
Run the 1.sh script
Show the /etc/nginx/nginx.conf not that this is artificiallcy set to create a files descriptor problem (as its hard to do in a demo)
Go to www.snarketing.net – show it works
Get the audience to go to www.snarketing.net
See it break – tail /var/log/nginx/error.log
Run 1fixed.sh
Show the /etc/nginx/nginx.conf
Repeat with the audience, see tit works and no file errors
This one can catch you out, and it’s a little counter intuitive lets take a look
Explain what a root directive and location block are
Here’s a very basic layout, now let’s take a look at our nginx.conf
Exolain the location / points to /usr/share/nginx and tha the other locations point to other document roots
Emphasse there are only /, /cat and /potato – and that something for say /ostrich would go to the / location
Walk through this example
But what If I just want to add a header for a particular location. The content is still in the / root, so no need to add that right?
Wrong, it will break because there is no root for the location to inherit
The solution is to have a root in the server context – or always declare a root in the location (but repeated lines are meh)
Demo instructions
Run 2.sh
Show the /etc/nginx/conf.d/default.conf
Check out http://www.snarketing.net/ostrich/. http://www.snarketing.net/cat http://www.snarketing.net/potato
Run 2broken.sh
Show the /etc/nginx/conf.d/default.conf
Explain that we have just added a particular header in the ostrich location
Check out http://www.snarketing.net/ostrich/
404 Baby!!!
Explain that this is because there is no location block in the main server{} context, and we don’t set one in the location{} block.
The fix is to add the doc root to the main server{} context
Run 2fixed.sh
Show the /etc/nginx/conf.d/default.conf note the documentroot in the server context
Check out http://www.snarketing.net/ostrich/
The bird is back!
I debated on putting this in, but if your organization has a whole article called “if is evil” you kind of have to
From the article itself
Tell them why
Here’s a simple example – don’t do this there are almost always a better way
Like this
Demo instructions
Run 3.sh
Show /etc/nginx/conf.d/default.conf
go to www.snarketing.net.fubar
It works but its ugly
Run 3fixed.sh
Show default.conf
Explain try files is better
go to www.snarketing.net.fubar
This can be confusing
Explain directive inheritance
Talk about array type of directives – i.e the ones that are additive – root is ony 1 value at time, but add_header can keep adding headers as much as you like – so you should be able to add more in a different context and inherit the upper levels?
No!
This builds and is fairly self explanatory – it shows what inherits and what replaces walk through it not that even if you wanted to redefine 1 header of many, you would need to redefine them all.
Demo instructions:
Run 4.sh
Show /etc/nginx/conf.d/defult.conf in a different terminal = – note that this includes the http{} context which is less usual but what we need for this demo
curl -is localhost:8080 note the headers
Explain that this is inheriting from the http{} context
curl -is localhost:8081 note the headers
For the server listening on port 8081, there is an add_header directive in the server{} block but not in its child location / block. The header defined in the server{} block overrides the two headers defined in the http{} context
curl -is localhost:8081/test
In the child location /test block, there is an add_header directive and it overrides both the header from its parent server{} block and the two headers from the http{} context:
curl -is localhost:8081/correct
If we want a location{} block to preserve the headers defined in its parent contexts along with any headers defined locally, we must redefine the parent headers within the location{} block. That’s what we’ve done in the location /correct block:
OK, here’s a performance and scalability one, and it’s easy to implement.
Without keepalives nginx will make a new connecton to an upstream server for every request
This may well lead to port exhaustion – because we need an ephemeral port for every connection, and we can run out. plus it’s an overhead to do a TCP handshake for every single http request
This problem was solved by keepalives over a decade ago (HTTP1-1.1)
Keepalives reuse the same connection for multiple requests and are standard at the front end, but need a coo
Keepalives reuse the same connection for multiple requests and are standard at the front end, but need a couple of settings to enable for the upstream servers – but first let's take a look at what happens without them
Demo instructions:
Run 5.sh
Show /etc/nginx/conf.d/default.conf
Run connections.sh
View output – lots of connections to the backend – and this is a small scale test
Return to presentation talk long enough for the connections in TIME_WAIT to go away
Enabling keepalives is easy!
Add the keepalive directive to the
Demo Instructions
Run 5fixed.sh
Show /etc/nginx/conf.d/default.conf
Run connections.sh
View output – fewer connections to the backend
Return to presentation