How to Get Started With NGINX

Nginx 962 views 36 slides Oct 20, 2021
Slide 1
Slide 1 of 36
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36

About This Presentation

On-Demand Recording: https://www.nginx.com/resources/webinars/how-to-get-started-with-nginx/

In this webinar, we help you get started using NGINX, the de facto standard building block for modern microservices-based architectures. During this practical workshop, we take you through installing and c...


Slide Content

How to Get Started
With NGINX
Install and configure a web server, reverse
proxy and load balancer
Jay Desai
Solutions Architect, Melbourne, Australia

| © 2019 F52
Today’s Host
Jay Desai
•Technical Solutions Architect,
Melbourne -Australia

| © 2019 F53
Agenda
1.
Introducing NGINX
2.
Install NGINX
3.
Configure Web Server
4.
Configure Reverse Proxy
5.
Configure Load Balancer
6.
Link to Resources

| © 2019 F54
NGINX –Evolution Map
2004
•NGINX 0.1
2007
•“Viable”
2011
•NGINX, Inc.
•NGINX 1.0
2013
•NGINX Plus R1
First Commercial
Offering
2018
•NGINX Unit 1.0
•Controller 1.0
2019
•Controller 2.0
(API mgmt.)
•NGINX Plus R18
•Acquired by F5
Networks
2020
•Controller 3.4
•NGINX Plus R22
•APP Protect

| © 2019 F55
#1450
million
Source: NetcraftMay 2020 Web Server Survey
“Most websites use NGINX”

| © 2019 F56
NGINX -Embracing a Multitude of Use Cases
Web
Server
Reverse
Proxy
Load
Balancer
Cache
Web
Application
Firewall
Internal
DDOS
Protection
API
Gateway
K8s
IC
Sidecar
Proxy

| © 2019 F57
Let’s workshop!

| © 2019 F58
Enterprise Architecture
Client /
Browser
Internet / WAN
NGINX:
Reverse
Proxy, Load
Balancer &
Web Server
Backend
Services /
Applications

| © 2019 F59
What we’re going to build
Client / Browser + Internet
NGINX:
Reverse
Proxy, Load
Balancer &
Web Server
Other
Services /
Applications
Some
Services /
Applications
+

| © 2019 F510
•Laptop
•Internet connection
•Linux host / VM / Docker
•NGINX already installed?
−$ nginx-v
Confidential –Do
Not Distribute
What will you need

| © 2019 F511 CONFIDENTIAL
Installing NGINX (simple)
CentOS / RHEL
•yum install nginx
Ubuntu / Debian
•apt-get install nginx
Docker
•docker pull nginx
MacOS / MacBook
•Use a VM or Docker
$ docker run --name mynginx-d -p 8080:80 nginx

| © 2019 F512 CONFIDENTIAL
What I will actually do
$ sudowgethttps://nginx.org/keys/nginx_signing.key
$ sudoapt-key add nginx_signing.key
$ sudovi /etc/apt/sources.list
deb https://nginx.org/packages/mainline/ubuntu/ bionic nginx
deb-srchttps://nginx.org/packages/mainline/ubuntu/ bionic nginx
$ sudoapt-get update
$ sudoapt-get install nginx
$ sudoservice nginxstart
$ nginx–v
nginxversion: nginx/1.19.0
$ curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.19.0
https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#prebuilt_ubuntu

| © 2019 F513 CONFIDENTIAL
Some Useful NGINX Commands
$ sudoservice nginx{start|stop|status|restart|reload|force-reload|upgrade|configtest|check-
reload}
$ sudonginx–v #version of NGINX
$ sudonginx–V #version & Enabled Modules
$ sudonginx–t #nginxconfiguration test
$ sudonginx–T #Full configuration dump

| © 2019 F514
Status:
Now we have successfully Installed
NGINX
Next:
Configure Web Server

| © 2019 F515
$ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginxweb server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

| © 2019 F516
Checking /etc/nginx/nginx.conf
Exists
Has http{}block
•Contains
include /etc/nginx/conf.d/*.conf;
Sample here →
/etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
user nginx;
worker_processesauto;
error_log/var/log/nginx/error.lognotice;
pid/var/run/nginx.pid;
events {
worker_connections1024;
}
http {
include /etc/nginx/mime.types;
default_typeapplication/octet-stream;
log_formatmain '$remote_addr-$remote_user[$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log/var/log/nginx/access.logmain;
sendfileon;
keepalive_timeout65;
include /etc/nginx/conf.d/*.conf; _
}

| © 2019 F517
Serving Content –Web Server
•Inspect default.conf
•Clean up default.conf
•Remove #commented out content
/etc/nginx/conf.d/default.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_namelocalhost;
location / {
root /usr/share/nginx/html;
index index.htmlindex.htm;
}
error_page500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
$ sudonginx–t
nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
$ sudonginx–s reload

| © 2019 F518 CONFIDENTIAL
Create index.htmlfile –APP 1
$ cd /opt
$ sudomkdirservices
$ cd services
$ sudomkdirApp1
$ sudomkdirApp2
$ cd App1
$ sudotouch index.html
$ sudovim index.html
Copy This →
SAVE
/opt/services/App1/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
151
6
17
18
<!doctype html>
<html lang="en-US">
<head>
<link rel="icon" type="image/png"
href="https://www.nginx.com/wp-
content/uploads/2019/10/favicon-48x48.ico" sizes="48x48">
<h1>This is my APP 1</h1>
<style>
body { background-color: #FF0000; }
</style>
<title>RED -APP 1</title>
</head>
</html>

| © 2019 F519 CONFIDENTIAL
Create index.htmlfile –APP 2
$ cd /opt/services/App2
$ sudotouch index.html
$ sudovim index.html
Copy This →
SAVE
/opt/services/App1/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
151
6
17
18
<!doctype html>
<html lang="en-US">
<head>
<link rel="icon" type="image/png"
href="https://www.nginx.com/wp-
content/uploads/2019/10/favicon-48x48.ico" sizes="48x48">
<h1>This is my APP 2</h1>
<style>
body { background-color: #00FF00; }
</style>
<title>RED -APP 2</title>
</head>
</html>

| © 2019 F520 CONFIDENTIAL
Editing –default.conf
•Inspect default.conf
$ cd /etc/nginx/conf.d
$ sudomv default.confb2b.conf
$ sudovim /etc/nginx/conf.d/b2b.conf
Copy This →
/etc/nginx/conf.d/b2b.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
151
6
17
18
19
20
21
22
23
24
25
26
server {
listen 8001 default_server;
server_namelocalhost;
location / {
root /opt/services/App1;
index index.htmlindex.htm;
}
}
server {
listen 8002 default_server;
server_namelocalhost;
location / {
root /opt/services/App2;
index index.htmlindex.htm;
}
}
$ sudonginx–t
nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
$ sudonginx–s reload

| © 2019 F521
$ curl http://localhost:8001
<!doctype html>
<html lang="en-US">
.
<h1>This is my APP 1</h1>
.
$ curl http://localhost:8002
<!doctype html>
<html lang="en-US">
.
<h1>This is my APP 2</h1>
.

| © 2019 F522
Status:
We are serving two
applications/services
Next:
Configure Reverse Proxy & Load Balancer

| © 2019 F523
Configuring upstream
/etc/nginx/conf.d/b2b.conf
upstream backend_servers{
zone backend_server_zone64k;
server localhost:8001;
server localhost:8002;
}
server {
listen 8080 default_server;
server_namelocalhost;
location / {
proxy_passhttp://backend_servers/;
}
}
server {
listen 8001 default_server;
server_namelocalhost;
index index.htmlindex.htm;
location / {
root /opt/services/App1;
index index.htmlindex.htm;
}
}
server {
listen 8002 default_server;
server_namelocalhost;
index index.htmlindex.htm;
location / {
root /opt/services/App2;
index index.htmlindex.htm;
}
}
$ sudovim
/etc/nginx/conf.d/b2b.conf
$ sudonginx–t
nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
$ sudonginx–s reload

| © 2019 F524
$ curl http://localhost:8080
<!doctype html>
<html lang="en-US">
.
<h1>This is my APP 1</h1>
.
$ curl http://localhost:8080
<!doctype html>
<html lang="en-US">
.
<h1>This is my APP 2</h1>
.

| © 2019 F525
Configuring upstream:
Adding Servers
/etc/nginx/conf.d/b2b.conf
upstream backend_servers{
zone backend_server_zone64k;
least_conn;
server localhost:8001;
server localhost:8002;
www.jdaus.net:9083;
}
server {
listen 8080 default_server;
server_namelocalhost;
location / {
proxy_passhttp://backend_servers/;
}
}
server {
listen 8001 default_server;
server_namelocalhost;
index index.htmlindex.htm;
location / {
root /opt/services/App1;
index index.htmlindex.htm;
}
}
server {
listen 8002 default_server;
server_namelocalhost;
index index.htmlindex.htm;
location / {
root /opt/services/App2;
index index.htmlindex.htm;
}
}
$ sudovim
/etc/nginx/conf.d/b2b.conf
$ sudonginx–t
nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
$ sudonginx–s reload

| © 2019 F526
$ curl http://localhost:8080
<!doctype html>
<html lang="en-US">
.
<h1>This is my APP 1</h1>
.
$ curl http://localhost:8080
<!doctype html>
<html lang="en-US">
.
<h1>This is my APP 2</h1>
.

| © 2019 F527
Status:
Now we have configured
Web Server, Reverse Proxy &
Load Balancer
Next:
Unique Features

| © 2019 F528
Shifting to NGINX PLUS

| © 2019 F529
Live Activity Monitoring
•Configuring the Dashboard
/etc/nginx/conf.d/b2b.conf
.
.
.
.
.
.
.
.
.
.
.
.
server {
listen 8005 default_server;
server_namelocalhost;
location /api/ {
apiwrite=on;
allow all;
#deny all;
}
location / {
root /usr/share/nginx/html;
index dashboard.html;
}
}
# End of file b2b.conf
NGINX Plus -ONLY
$ sudonginx–t
nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
$ sudonginx–s reload

| © 2019 F530
Active Health Check
•Actively monitors all upstream server
locations
•Default –5 seconds
health_checkinterval=10
fails=3 passes=2;
/etc/nginx/conf.d/b2b.conf
.
.
.
server {
listen 8080 default_server;
server_namelocalhost;
location / {
proxy_passhttp://backend_servers/;
health_check;
}
}
.
.
.
NGINX Plus -ONLY
$ sudonginx–t
nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
$ sudonginx–s reload

| © 2019 F531
Zero Downtime Config Reloads
•Adding a new upstream
•Zero Downtime for active streams
/etc/nginx/conf.d/b2b.conf
1
2
3
4
5
6
7
8
9
upstream backend_servers{
zone backend_server_zone64k;
least_conn;
server localhost:8001;
server localhost:8002;
www.jdaus.net:9083;
#INSERT THE FOLLOWING UPSTREAM
www.jdaus.net:9084;
}
NGINX Plus -ONLY
$ sudonginx–t
nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful
$ sudonginx–s reload

| © 2019 F532
Rate Limiting
Rate limit is configuredand monitored at
a global level
Limit is appliedwhere we want it
•Per API gateway
•Per API definition
•Per URI/route
/etc/nginx/conf.d/b2b.conf
limit_req_zone$remote_addrzone=perip:1m rate=2r/s;
upstream backend_servers{
zone backend_server_zone64k;
.
.
.
server {
listen 8000 default_server;
server_namelocalhost;
location / {
proxy_passhttp://backend_servers/;
health_check;
limit_reqzone=peripnodelay;
limit_req_status429;
}
}
.
.
.

| © 2019 F533
$ nginx-s reload
$ curl http://localhost:8000
<!doctype html>
<html lang="en-US">
<head>…
$ !!;!!;!!;!!;!!
{"status":429,"message":”Rate limit exceeded"}

| © 2019 F534
How did we do?
1.Installing NGINX
2.Configuring Webserver
3.Configure Reverse Proxy
& Load Balancer
4.Active Health Check
5.Zero Downtime
Configuration Reloads

| © 2019 F535
Resources
Official NGINX open source downloads
•http://nginx.org/en/linux_packages.html
NGINX Plus Trial License
•https://www.nginx.com/free-trial-request/
Getting Started with NGINX Guides
•https://www.nginx.com/resources/wiki/start/
•http://nginx.org/en/docs/beginners_guide.html