SlideShare uma empresa Scribd logo
1 de 47
Nginx
High Performance Load Balancer, Web Server & Reverse Proxy
Techognite
Complete coverage on Tech
Agenda
• Introduction
• Architecture
• Configurations
• Server, Listener, Location
• Mime type
• gzip
• Reverse Proxy
• Load Balance
• LB Algorithms
Introduction
• Nginx is an Open-source web server and uses a non-threaded, event-
driven architecture.
• NGINX, pronounced like “engine-ex”
• Load Balancer
• Reverse Proxy Server
• Content Cache
Basic Commands
• apt-get install nginx #Install Nginx on Ubuntu
• service nginx start #Start Nginx server
• service nginx stop #Stop the server
• service nginx restart #Stop and start the server
• service nginx status #Return the current status
• nginx –t #Verify the configurations
Architecture
• Nginx architecture consists of a master and workers process.
• Master process performs the privileged operations such as reading
configuration and binding to ports.
• One master process can control multiple worker processes.
• The worker processes handle network connections, read and write content
to disk, and communicate with other servers.
• Each worker process is a single-threaded.
• https://cdn.nginx.com/wp-content/uploads/2015/06/Screen-Shot-2015-06-
08-at-12.36.30-PM.png
Architecture
• The worker process waits for events on the listen and connection sockets
• Any Event on listen socket creates a new connection.
• worker_processes directive Define the number of worker process should
run.
• worker_connections maximum number of simultaneous connections for
each worker process
• NGINX is single threaded, so best practice is total number of
worker_processes should equivalent to number of core in the system.
Understand Nginx Configurations
1. /etc/nginx/nginx.conf #Location of Nginx configuration file.
2. worker_processes auto # No of worker process
3. pid /run/nginx.pid; #Location of Process Id
4. worker_connections 768; #No of connections per Worker Process
5. access_log /var/log/nginx/access.log; # Access Log
6. error_log /var/log/nginx/error.log; # Error Log
7. gzip on; # compression
8. include /etc/nginx/conf.d/*.conf; # To include any file
Basic Nginx Configurations
server {
listen 80;
server_name _;
location / {
root /var/www/html/;
index index.html;
}
}
Server Context Example
server {
listen 80;
server_name tech.com;
return 200 "HELLO FROM TECH WORLD";
}
server {
listen 80 default_server;
server_name hello.com;
return 200 "HELLO WORLD!";
}
Server Context Configurations
• listen 80 default_server; #Server Port
• server_name _; #Server Name like tech.com
• root /var/www/html; #Root location of content
• index index.html; #Index file name in root location
• location / { } #specify action on specific location
• return 200 "HELLO WORLD!"; # Return response
Server Context listen Directive
• Listen 192.168.125.21:80; #from192.168.125.21 on 80 Port
• listen 127.0.0.1; # by default port :80 is used
• listen *:81; # All IP on 80 Port
• listen 81; # by default all ips are used
• listen localhost:80; # from Localhost
But hostname may not be resolved upon nginx's launch
• Listen [::]:80; # IPv6 addresses
• listen [::1]; # IPv6 addresses
Multi Server Context
In case Multi server configuration:
• Server listing on IP:port, with a matching server_name directive;
• Server listing on IP:port, with the default_server flag;
• Server listing on IP:port, first one defined;
• If there are no matches, refuse the connection
Multi Server Context: wildcard
When there is ambiguity, nginx uses the following order:
server_name abc.co, www.abc.co; # exact match
server_name *.abc.co; # wildcard matching
server_name abc.*; # wildcard matching
server_name ~^[0-9]*.abc.co$; # regexp matching
• Exact name;
• Longest wildcard name starting with an asterisk.
• Longest wildcard name ending with an asterisk.
• First matching regular expression (in the order of appearance in the
configuration file).
Multi Server Context: wildcard
Nginx will store 3 hash tables:
exact names, wildcards starting with an asterisk, and wildcards ending with an
asterisk. If the result is not in any of the tables, the regular expressions will be
tested sequentially.
So if define server name using wildcard, it will be a bit slower than the exact
match.
Location Context Example
location /tech {
root /var/www/html/;
index index.html;
}
location /hello{
root /var/www/website/;
index index.html;
}
Location Context Configurations
• root /var/www/html; #Root location of content
• index index.html; #Index file name in root location
• return 200 "HELLO WORLD!"; # Return response
• autoindex on; #Index the content
• default_type text/html; # define mime types
• try_files $uri /index.html =404; # Try multiple paths,
• proxy_pass http://_server; # used in Reverse Proxy, LB
Modifier Location Context Configurations
• = - Exact match location = /match { }
• ^~ - Preferential match location ^~ /match0 { }
• ~ && ~* - Regex match location ~* /match[0-9] { }
• no modifier - Prefix match location /match { }
• Nginx will first check for any exact matches.
• If it doesn't find any, it'll look for preferential ones.
• If this match also fails, regex matches will be tested
• If everything else fails, the last prefix match will be used.
Prefix Match Location Context
When no modifier is specified, the path is treated as prefix
location /tech {
# ...
}
Valid match:
• /tech
• /tech/java/index.html
• /tech123
• /techworld
Nginx Reverse proxy
• Sits between internal applications and external clients, forwarding client
requests to the appropriate server.
• Retrieves resources on behalf of a client from one or more servers.
• Does the request Routing
• Can prevent sever from DOS, Web based attacks.
Nginx Reverse proxy
Web
Browser
Web
Browser
Web
Browser
Internet
Nginx
47.30.208.205
AS:
192.168.124.21
External Client Internal Application
Reverse Proxy
AS:
192.168.124.22
AS:
192.168.124.23
Reverse proxy Example
location /admin{
proxy_pass http://192.168.124.22:8180/admin;
}
location /user{
proxy_pass http://192.168.124.21:8280/user;
}
http://47.30.208.205/user -> http://192.168.124.21:8280/user
http://47.30.208.205/admin -> http://192.168.124.22:8180/admin
Reverse proxy Configuration
• proxy_pass http://18.191.4.181:8180/admin;
#Pass the request to specified Server
• proxy_set_header X-Real-IP $remote_addr;
#Set the client IP in request header with name X-Real-IP
Client IP Nginx IP Application Server
220.220.220.1 220.220.220.100 Receive: 220.220.220.100
Reverse proxy Configuration
• proxy_set_header host $host;
#Set the host Name in request header with name host
Server Name (Domain Name) Nginx IP Application Server
www.admin.com 220.220.220.100 220.220.220.200
www.user.com 220.220.220.100 220.220.220.200
Reverse proxy Configuration
location /admin{
proxy_pass http://192.168.124.22:8180/admin;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header host $host;
}
http://47.30.208.205/admin -> http://192.168.124.22:8180/admin
with client Real IP and Host Name
Request Redirection Configurtation
server {
listen 80;
server_name domain_name;
return 301 https://$server_name$request_uri;
}
Every request coming to the this server on port 80 with http, will be
redirected to https with same server name
http://192.168.125.32 will redirected to https://192.168.125.32
MIME type Configurations
• MIME full form Multipurpose Internet Mail Extensions
• content-type response header parameter is used to store this information.
• Example: Content-type= text/html
• Browser use the content type value to open the file with the proper
extension/plugin
• include /etc/nginx/mime.types; #Include mime type files
• default_type application/octet-stream; # define default mime type
GZIP
• GZIP is used for response compression.
• Used for performance upgrade.
• gzip_comp_level, which is the level of compression and ranges from 1 to 10.
Generally,
• Value of gzip_comp_level should not be above 6 — the gain in terms of size
reduction is insignificant, as it needs a lot more CPU usage. gzip_types is a
list of response types that compression will be applied on.
Nginx Load Balancer
A load balancer is a server that distributes network or application traffic
across a cluster of servers. Load balancing improves responsiveness and
increases availability of applications.
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
100
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
200
Slow Response, Down
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
300
Application Server
100 Client
Application Server
100 Client
NGINX Load Balancer
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
300
Application Server
100 Client
Application Server
100 Client
NGINX Load Balancer
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
300
Application Server
100 Client
Application Server
100 Client
NGINX Load Balancer
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
300
Application Server
100 Client
Application Server
100 Client
NGINX Load Balancer
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
300
Application Server
100 Client
Application Server
100 Client
NGINX Load Balancer
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
300
Application Server
100 Client
Application Server
100 Client
NGINX Load Balancer
Load Balancer
Application Server
100 Client
Client 1 Client 2 Client 3 Client 4
Client
300
Application Server
100 Client
Application Server
100 Client
NGINX Load Balancer
Nginx Load Balancer Configuration
server {
listen 80;
server_name _;
location /admin {
proxy_pass http://remote_application_server;
}
}
upstream remote_application_server {
server 18.191.4.181:8180;
server 18.191.4.182:8180;
server 18.191.4.183:8180;
}
Weight: Nginx Load Balancer
When the available resources between pool of hosts are not equal, we define
weight to favour some servers over others.
upstream remote_application_server {
server 18.191.4.181:8180 weight=4;
server 18.191.4.182:8180 weight=1;
}
Health Check: Nginx Load Balancer
Nginx implement passive server health checks to know which servers are
available from pool of servers.
upstream remote_application_server {
server 18.191.4.181:8180 max_fails=3 fail_timeout=30s;
server 18.191.4.182:8180 max_fails=3 fail_timeout=30s;
}
Health Check: Nginx Load Balancer
• max_fails: Number of fail attempt after which server will assume down and
load will not share to this server.
• fail_timeout, which also defines how long the server should be considered
failed.
• If max_fails is set to a value greater than 1 then subsequent fails must
happen within a specific time frame for the fails to count.
• By default the fail_timeout is set to 10 seconds.
• Once Server is marked as failed, next request will not be sent to this server
• If Next health check start returning successful, the server is again marked
live and included in the load balancing as normal.
Round-robin Algorithm: Nginx Load Balancer
• Round-robin:
• Round-robin scheme each server is selected in turns according to the
order.
• Request goes to each server one by one.
• Load balancing with nginx uses a round-robin algorithm by default
Least connections Algorithm: Nginx Load Balancer
Least connections:
• This method directs the requests to the server with the least active
connections at that time
• Should used where requests takes longer to complete.
upstream remote_application_server {
least_conn;
server 18.191.4.181:8180;
server 18.191.4.182:8180;
}
IP hashing Algorithm: Nginx Load Balancer
IP hashing:
• IP Hashing techniques first select the sever and send all the subsequent
request to the same server coming from the same IP.
• Uses the visitors IP address as a key to determine which host should be
selected to fulfill the request
Condition for IP hashing.
• Client IP should same for next request.
• Server should be available, if server is down same request can be sent any
of the server mention in upstream pool.
IP hashing Algorithm: Nginx Load Balancer
Configuration:
upstream remote_application_server {
ip_hash;
server 18.191.4.181:8180;
server 18.191.4.182:8180;
server 18.191.4.183:8180;
}
Backup Server: Nginx Load Balancer
• We can mark server as down, while performing maintenance. This time
request will not send to this server.
• Marking a server backup will be used, when all other servers are
unavailable.
upstream remote_application_server {
server 18.191.4.181:8180;
server 18.191.4.182:8180 down;
server 18.191.4.183:8180 backup;
}
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy

Mais conteúdo relacionado

Mais procurados

Nginx internals
Nginx internalsNginx internals
Nginx internals
liqiang xu
 

Mais procurados (20)

NGINX: Basics and Best Practices EMEA
NGINX: Basics and Best Practices EMEANGINX: Basics and Best Practices EMEA
NGINX: Basics and Best Practices EMEA
 
NGINX: Basics and Best Practices
NGINX: Basics and Best PracticesNGINX: Basics and Best Practices
NGINX: Basics and Best Practices
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 
Introduction to Nginx
Introduction to NginxIntroduction to Nginx
Introduction to Nginx
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
 
Nginx
NginxNginx
Nginx
 
Using NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content CacheUsing NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content Cache
 
Nginx Essential
Nginx EssentialNginx Essential
Nginx Essential
 
NGINX ADC: Basics and Best Practices – EMEA
NGINX ADC: Basics and Best Practices – EMEANGINX ADC: Basics and Best Practices – EMEA
NGINX ADC: Basics and Best Practices – EMEA
 
NGINX Installation and Tuning
NGINX Installation and TuningNGINX Installation and Tuning
NGINX Installation and Tuning
 
How to Get Started With NGINX
How to Get Started With NGINXHow to Get Started With NGINX
How to Get Started With NGINX
 
Apache Server Tutorial
Apache Server TutorialApache Server Tutorial
Apache Server Tutorial
 
Ingress overview
Ingress overviewIngress overview
Ingress overview
 
2019.06.27 Intro to Ceph
2019.06.27 Intro to Ceph2019.06.27 Intro to Ceph
2019.06.27 Intro to Ceph
 
Introduction to Haproxy
Introduction to HaproxyIntroduction to Haproxy
Introduction to Haproxy
 
Ceph Performance and Sizing Guide
Ceph Performance and Sizing GuideCeph Performance and Sizing Guide
Ceph Performance and Sizing Guide
 
Ceph Introduction 2017
Ceph Introduction 2017  Ceph Introduction 2017
Ceph Introduction 2017
 
Apache ppt
Apache pptApache ppt
Apache ppt
 
HAProxy
HAProxy HAProxy
HAProxy
 
IPVS for Docker Containers
IPVS for Docker ContainersIPVS for Docker Containers
IPVS for Docker Containers
 

Semelhante a Nginx A High Performance Load Balancer, Web Server & Reverse Proxy

Using Apache as an Application Server
Using Apache as an Application ServerUsing Apache as an Application Server
Using Apache as an Application Server
Phil Windley
 

Semelhante a Nginx A High Performance Load Balancer, Web Server & Reverse Proxy (20)

ITB2019 NGINX Overview and Technical Aspects - Kevin Jones
ITB2019 NGINX Overview and Technical Aspects - Kevin JonesITB2019 NGINX Overview and Technical Aspects - Kevin Jones
ITB2019 NGINX Overview and Technical Aspects - Kevin Jones
 
NGINX 101 - now with more Docker
NGINX 101 - now with more DockerNGINX 101 - now with more Docker
NGINX 101 - now with more Docker
 
NGINX 101 - now with more Docker
NGINX 101 - now with more DockerNGINX 101 - now with more Docker
NGINX 101 - now with more Docker
 
NGINX: High Performance Load Balancing
NGINX: High Performance Load BalancingNGINX: High Performance Load Balancing
NGINX: High Performance Load Balancing
 
Introduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to ChefIntroduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to Chef
 
AAI-3218 Production Deployment Best Practices for WebSphere Liberty Profile
AAI-3218 Production Deployment Best Practices for WebSphere Liberty ProfileAAI-3218 Production Deployment Best Practices for WebSphere Liberty Profile
AAI-3218 Production Deployment Best Practices for WebSphere Liberty Profile
 
Nginx Deep Dive Kubernetes Ingress
Nginx Deep Dive Kubernetes IngressNginx Deep Dive Kubernetes Ingress
Nginx Deep Dive Kubernetes Ingress
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
Load Balancing Applications with NGINX in a CoreOS Cluster
Load Balancing Applications with NGINX in a CoreOS ClusterLoad Balancing Applications with NGINX in a CoreOS Cluster
Load Balancing Applications with NGINX in a CoreOS Cluster
 
CouchDB for Web Applications - Erlang Factory London 2009
CouchDB for Web Applications - Erlang Factory London 2009CouchDB for Web Applications - Erlang Factory London 2009
CouchDB for Web Applications - Erlang Factory London 2009
 
Automating Compliance with InSpec - AWS North Sydney
Automating Compliance with InSpec - AWS North SydneyAutomating Compliance with InSpec - AWS North Sydney
Automating Compliance with InSpec - AWS North Sydney
 
Architecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APIArchitecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web API
 
Applciation footprinting, discovery and enumeration
Applciation footprinting, discovery and enumerationApplciation footprinting, discovery and enumeration
Applciation footprinting, discovery and enumeration
 
UEMB200: Next Generation of Endpoint Management Architecture and Discovery Se...
UEMB200: Next Generation of Endpoint Management Architecture and Discovery Se...UEMB200: Next Generation of Endpoint Management Architecture and Discovery Se...
UEMB200: Next Generation of Endpoint Management Architecture and Discovery Se...
 
ITB2017 - Nginx Effective High Availability Content Caching
ITB2017 - Nginx Effective High Availability Content CachingITB2017 - Nginx Effective High Availability Content Caching
ITB2017 - Nginx Effective High Availability Content Caching
 
What's new in NGINX Plus R19
What's new in NGINX Plus R19What's new in NGINX Plus R19
What's new in NGINX Plus R19
 
Web server
Web serverWeb server
Web server
 
NGINX ADC: Basics and Best Practices
NGINX ADC: Basics and Best PracticesNGINX ADC: Basics and Best Practices
NGINX ADC: Basics and Best Practices
 
Using Apache as an Application Server
Using Apache as an Application ServerUsing Apache as an Application Server
Using Apache as an Application Server
 
Making Spinnaker Go @ Stitch Fix
Making Spinnaker Go @ Stitch FixMaking Spinnaker Go @ Stitch Fix
Making Spinnaker Go @ Stitch Fix
 

Último

biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
1301aanya
 
Human genetics..........................pptx
Human genetics..........................pptxHuman genetics..........................pptx
Human genetics..........................pptx
Silpa
 
Conjugation, transduction and transformation
Conjugation, transduction and transformationConjugation, transduction and transformation
Conjugation, transduction and transformation
Areesha Ahmad
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
NazaninKarimi6
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Sérgio Sacani
 

Último (20)

Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspects
 
Zoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdfZoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdf
 
biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
 
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptxClimate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
 
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIACURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)
 
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and SpectrometryFAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
 
Human genetics..........................pptx
Human genetics..........................pptxHuman genetics..........................pptx
Human genetics..........................pptx
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.
 
pumpkin fruit fly, water melon fruit fly, cucumber fruit fly
pumpkin fruit fly, water melon fruit fly, cucumber fruit flypumpkin fruit fly, water melon fruit fly, cucumber fruit fly
pumpkin fruit fly, water melon fruit fly, cucumber fruit fly
 
Use of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxUse of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptx
 
Conjugation, transduction and transformation
Conjugation, transduction and transformationConjugation, transduction and transformation
Conjugation, transduction and transformation
 
Proteomics: types, protein profiling steps etc.
Proteomics: types, protein profiling steps etc.Proteomics: types, protein profiling steps etc.
Proteomics: types, protein profiling steps etc.
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
 
Chemistry 5th semester paper 1st Notes.pdf
Chemistry 5th semester paper 1st Notes.pdfChemistry 5th semester paper 1st Notes.pdf
Chemistry 5th semester paper 1st Notes.pdf
 
PSYCHOSOCIAL NEEDS. in nursing II sem pptx
PSYCHOSOCIAL NEEDS. in nursing II sem pptxPSYCHOSOCIAL NEEDS. in nursing II sem pptx
PSYCHOSOCIAL NEEDS. in nursing II sem pptx
 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
 
An introduction on sequence tagged site mapping
An introduction on sequence tagged site mappingAn introduction on sequence tagged site mapping
An introduction on sequence tagged site mapping
 
Clean In Place(CIP).pptx .
Clean In Place(CIP).pptx                 .Clean In Place(CIP).pptx                 .
Clean In Place(CIP).pptx .
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 

Nginx A High Performance Load Balancer, Web Server & Reverse Proxy

  • 1. Nginx High Performance Load Balancer, Web Server & Reverse Proxy Techognite Complete coverage on Tech
  • 2. Agenda • Introduction • Architecture • Configurations • Server, Listener, Location • Mime type • gzip • Reverse Proxy • Load Balance • LB Algorithms
  • 3. Introduction • Nginx is an Open-source web server and uses a non-threaded, event- driven architecture. • NGINX, pronounced like “engine-ex” • Load Balancer • Reverse Proxy Server • Content Cache
  • 4. Basic Commands • apt-get install nginx #Install Nginx on Ubuntu • service nginx start #Start Nginx server • service nginx stop #Stop the server • service nginx restart #Stop and start the server • service nginx status #Return the current status • nginx –t #Verify the configurations
  • 5. Architecture • Nginx architecture consists of a master and workers process. • Master process performs the privileged operations such as reading configuration and binding to ports. • One master process can control multiple worker processes. • The worker processes handle network connections, read and write content to disk, and communicate with other servers. • Each worker process is a single-threaded. • https://cdn.nginx.com/wp-content/uploads/2015/06/Screen-Shot-2015-06- 08-at-12.36.30-PM.png
  • 6. Architecture • The worker process waits for events on the listen and connection sockets • Any Event on listen socket creates a new connection. • worker_processes directive Define the number of worker process should run. • worker_connections maximum number of simultaneous connections for each worker process • NGINX is single threaded, so best practice is total number of worker_processes should equivalent to number of core in the system.
  • 7. Understand Nginx Configurations 1. /etc/nginx/nginx.conf #Location of Nginx configuration file. 2. worker_processes auto # No of worker process 3. pid /run/nginx.pid; #Location of Process Id 4. worker_connections 768; #No of connections per Worker Process 5. access_log /var/log/nginx/access.log; # Access Log 6. error_log /var/log/nginx/error.log; # Error Log 7. gzip on; # compression 8. include /etc/nginx/conf.d/*.conf; # To include any file
  • 8. Basic Nginx Configurations server { listen 80; server_name _; location / { root /var/www/html/; index index.html; } }
  • 9. Server Context Example server { listen 80; server_name tech.com; return 200 "HELLO FROM TECH WORLD"; } server { listen 80 default_server; server_name hello.com; return 200 "HELLO WORLD!"; }
  • 10. Server Context Configurations • listen 80 default_server; #Server Port • server_name _; #Server Name like tech.com • root /var/www/html; #Root location of content • index index.html; #Index file name in root location • location / { } #specify action on specific location • return 200 "HELLO WORLD!"; # Return response
  • 11. Server Context listen Directive • Listen 192.168.125.21:80; #from192.168.125.21 on 80 Port • listen 127.0.0.1; # by default port :80 is used • listen *:81; # All IP on 80 Port • listen 81; # by default all ips are used • listen localhost:80; # from Localhost But hostname may not be resolved upon nginx's launch • Listen [::]:80; # IPv6 addresses • listen [::1]; # IPv6 addresses
  • 12. Multi Server Context In case Multi server configuration: • Server listing on IP:port, with a matching server_name directive; • Server listing on IP:port, with the default_server flag; • Server listing on IP:port, first one defined; • If there are no matches, refuse the connection
  • 13. Multi Server Context: wildcard When there is ambiguity, nginx uses the following order: server_name abc.co, www.abc.co; # exact match server_name *.abc.co; # wildcard matching server_name abc.*; # wildcard matching server_name ~^[0-9]*.abc.co$; # regexp matching • Exact name; • Longest wildcard name starting with an asterisk. • Longest wildcard name ending with an asterisk. • First matching regular expression (in the order of appearance in the configuration file).
  • 14. Multi Server Context: wildcard Nginx will store 3 hash tables: exact names, wildcards starting with an asterisk, and wildcards ending with an asterisk. If the result is not in any of the tables, the regular expressions will be tested sequentially. So if define server name using wildcard, it will be a bit slower than the exact match.
  • 15. Location Context Example location /tech { root /var/www/html/; index index.html; } location /hello{ root /var/www/website/; index index.html; }
  • 16. Location Context Configurations • root /var/www/html; #Root location of content • index index.html; #Index file name in root location • return 200 "HELLO WORLD!"; # Return response • autoindex on; #Index the content • default_type text/html; # define mime types • try_files $uri /index.html =404; # Try multiple paths, • proxy_pass http://_server; # used in Reverse Proxy, LB
  • 17. Modifier Location Context Configurations • = - Exact match location = /match { } • ^~ - Preferential match location ^~ /match0 { } • ~ && ~* - Regex match location ~* /match[0-9] { } • no modifier - Prefix match location /match { } • Nginx will first check for any exact matches. • If it doesn't find any, it'll look for preferential ones. • If this match also fails, regex matches will be tested • If everything else fails, the last prefix match will be used.
  • 18. Prefix Match Location Context When no modifier is specified, the path is treated as prefix location /tech { # ... } Valid match: • /tech • /tech/java/index.html • /tech123 • /techworld
  • 19. Nginx Reverse proxy • Sits between internal applications and external clients, forwarding client requests to the appropriate server. • Retrieves resources on behalf of a client from one or more servers. • Does the request Routing • Can prevent sever from DOS, Web based attacks.
  • 20. Nginx Reverse proxy Web Browser Web Browser Web Browser Internet Nginx 47.30.208.205 AS: 192.168.124.21 External Client Internal Application Reverse Proxy AS: 192.168.124.22 AS: 192.168.124.23
  • 21. Reverse proxy Example location /admin{ proxy_pass http://192.168.124.22:8180/admin; } location /user{ proxy_pass http://192.168.124.21:8280/user; } http://47.30.208.205/user -> http://192.168.124.21:8280/user http://47.30.208.205/admin -> http://192.168.124.22:8180/admin
  • 22. Reverse proxy Configuration • proxy_pass http://18.191.4.181:8180/admin; #Pass the request to specified Server • proxy_set_header X-Real-IP $remote_addr; #Set the client IP in request header with name X-Real-IP Client IP Nginx IP Application Server 220.220.220.1 220.220.220.100 Receive: 220.220.220.100
  • 23. Reverse proxy Configuration • proxy_set_header host $host; #Set the host Name in request header with name host Server Name (Domain Name) Nginx IP Application Server www.admin.com 220.220.220.100 220.220.220.200 www.user.com 220.220.220.100 220.220.220.200
  • 24. Reverse proxy Configuration location /admin{ proxy_pass http://192.168.124.22:8180/admin; proxy_set_header X-Real-IP $remote_addr; proxy_set_header host $host; } http://47.30.208.205/admin -> http://192.168.124.22:8180/admin with client Real IP and Host Name
  • 25. Request Redirection Configurtation server { listen 80; server_name domain_name; return 301 https://$server_name$request_uri; } Every request coming to the this server on port 80 with http, will be redirected to https with same server name http://192.168.125.32 will redirected to https://192.168.125.32
  • 26. MIME type Configurations • MIME full form Multipurpose Internet Mail Extensions • content-type response header parameter is used to store this information. • Example: Content-type= text/html • Browser use the content type value to open the file with the proper extension/plugin • include /etc/nginx/mime.types; #Include mime type files • default_type application/octet-stream; # define default mime type
  • 27. GZIP • GZIP is used for response compression. • Used for performance upgrade. • gzip_comp_level, which is the level of compression and ranges from 1 to 10. Generally, • Value of gzip_comp_level should not be above 6 — the gain in terms of size reduction is insignificant, as it needs a lot more CPU usage. gzip_types is a list of response types that compression will be applied on.
  • 28. Nginx Load Balancer A load balancer is a server that distributes network or application traffic across a cluster of servers. Load balancing improves responsiveness and increases availability of applications.
  • 29. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 100
  • 30. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 200 Slow Response, Down
  • 31. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 300 Application Server 100 Client Application Server 100 Client NGINX Load Balancer
  • 32. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 300 Application Server 100 Client Application Server 100 Client NGINX Load Balancer
  • 33. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 300 Application Server 100 Client Application Server 100 Client NGINX Load Balancer
  • 34. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 300 Application Server 100 Client Application Server 100 Client NGINX Load Balancer
  • 35. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 300 Application Server 100 Client Application Server 100 Client NGINX Load Balancer
  • 36. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 300 Application Server 100 Client Application Server 100 Client NGINX Load Balancer
  • 37. Load Balancer Application Server 100 Client Client 1 Client 2 Client 3 Client 4 Client 300 Application Server 100 Client Application Server 100 Client NGINX Load Balancer
  • 38. Nginx Load Balancer Configuration server { listen 80; server_name _; location /admin { proxy_pass http://remote_application_server; } } upstream remote_application_server { server 18.191.4.181:8180; server 18.191.4.182:8180; server 18.191.4.183:8180; }
  • 39. Weight: Nginx Load Balancer When the available resources between pool of hosts are not equal, we define weight to favour some servers over others. upstream remote_application_server { server 18.191.4.181:8180 weight=4; server 18.191.4.182:8180 weight=1; }
  • 40. Health Check: Nginx Load Balancer Nginx implement passive server health checks to know which servers are available from pool of servers. upstream remote_application_server { server 18.191.4.181:8180 max_fails=3 fail_timeout=30s; server 18.191.4.182:8180 max_fails=3 fail_timeout=30s; }
  • 41. Health Check: Nginx Load Balancer • max_fails: Number of fail attempt after which server will assume down and load will not share to this server. • fail_timeout, which also defines how long the server should be considered failed. • If max_fails is set to a value greater than 1 then subsequent fails must happen within a specific time frame for the fails to count. • By default the fail_timeout is set to 10 seconds. • Once Server is marked as failed, next request will not be sent to this server • If Next health check start returning successful, the server is again marked live and included in the load balancing as normal.
  • 42. Round-robin Algorithm: Nginx Load Balancer • Round-robin: • Round-robin scheme each server is selected in turns according to the order. • Request goes to each server one by one. • Load balancing with nginx uses a round-robin algorithm by default
  • 43. Least connections Algorithm: Nginx Load Balancer Least connections: • This method directs the requests to the server with the least active connections at that time • Should used where requests takes longer to complete. upstream remote_application_server { least_conn; server 18.191.4.181:8180; server 18.191.4.182:8180; }
  • 44. IP hashing Algorithm: Nginx Load Balancer IP hashing: • IP Hashing techniques first select the sever and send all the subsequent request to the same server coming from the same IP. • Uses the visitors IP address as a key to determine which host should be selected to fulfill the request Condition for IP hashing. • Client IP should same for next request. • Server should be available, if server is down same request can be sent any of the server mention in upstream pool.
  • 45. IP hashing Algorithm: Nginx Load Balancer Configuration: upstream remote_application_server { ip_hash; server 18.191.4.181:8180; server 18.191.4.182:8180; server 18.191.4.183:8180; }
  • 46. Backup Server: Nginx Load Balancer • We can mark server as down, while performing maintenance. This time request will not send to this server. • Marking a server backup will be used, when all other servers are unavailable. upstream remote_application_server { server 18.191.4.181:8180; server 18.191.4.182:8180 down; server 18.191.4.183:8180 backup; }