SlideShare a Scribd company logo
Best Practices for Getting
Started with NGINX Open
Source
Alessandro Fael Garcia
Senior Solutions Engineer – Community & Alliances
©2022 F5
2 Source: https://news.netcraft.com/archives/2022/06/30/june-2022-web-server-survey.html
©2022 F5
3
©2022 F5
4
Installing NGINX
Best practices
©2022 F5
5
Use the NGINX Open Source official repository!
https://nginx.org/en/linux_packages.html
©2022 F5
6
TIL
• nginx –t → Check if NGINX configuration is valid
• nginx –T → Dump full NGINX configuration
• nginx –v → Print NGINX version
• nginx –V → Print NGINX package config arguments
• nginx –s <start/stop/reload> → Start NGINX; stop (kill) NGINX; reload NGINX configuration (gracefully)
Key NGINX Commands
©2022 F5
7
/etc/nginx/nginx.conf
• Main NGINX configuration file
• Global settings
• Contains sensible defaults (when installing NGINX from our
official repositories)
• Avoid modifying unless you know what you are doing
(defaults will work out of the box for >80% of use cases)
• Includes HTTP block (adding a Stream block is one of the
few cases where you’d want to modify the file)
/etc/nginx/conf.d/*.conf
• Default directory for additional NGINX configuration files
• By default, files here are contained within the HTTP context
• default.conf includes sample configuration with the NGINX
default landing page
• Start with a single configuration file, split your configuration
into further files as necessary
Recommended NGINX Directory Structure
Defaults? What defaults?!
©2022 F5
8
Use Let’s Encrypt and Certbot for easy certs!
https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal
©2022 F5
9
Tuning NGINX
One step at a time
©2022 F5
10
nginx.conf
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
proxy_cache_lock on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
...
}
}
}
©2022 F5
11
worker_processes
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
proxy_cache_lock on;
...
}
}
}
Make sure you spawn one NGINX worker process per CPU
core (default: 1)
©2022 F5
12
worker_connections & worker_rlimit_nofile
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
proxy_cache_lock on;
...
}
}
}
a) Increase the worker connections to >1024 (default: 512)
b) Increase the limit on the maximum number of open files
to at least twice the number of worker connections
(default: system limit)
©2022 F5
13
access_log
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
proxy_cache_lock on;
...
}
}
}
• Turn off the access log for extra performance (default: on)
or
• Set a buffer or a time to only write logs at an interval
(default: off)
©2022 F5
14
keepalive
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
proxy_cache_lock on;
...
}
}
}
Use keepalives to keep connections to upstream servers
open (default: 0) → You will need to set HTTP to 1.1 and
rewrite the Connection header
©2022 F5
15
ssl_session_cache
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
proxy_cache_lock on;
...
}
}
}
Cache and share your SSL sessions between all your NGINX
processes (default: disabled)
©2022 F5
16
proxy_cache_lock
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
proxy_cache_lock on;
...
}
}
}
Send only one request to the upstream server when there
are multiple cache misses for the same file (default: off)
©2022 F5
17
Recap
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
28
29
30
31
...
worker_processes auto;
worker_rlimit_nofile 2048;
...
events {
worker_connections 1024;
}
http {
access_log off;
sendfile on;
tcp_nopush on;
...
upstream app {
server w.x.y.z;
keepalive 2;
...
}
server {
access_log /var/log/nginx/access.log main buffer=512k
flush=5m;
ssl_session_cache shared:SSL:10m;
...
location / {
proxy_http_version 1.1;
proxy_set_header Connection “”;
proxy_pass http://app;
proxy_cache_lock on;
...
}
}
}
• Make sure you spawn one NGINX worker process per
CPU core (default: 1)
• Increase the worker connections to >1024 (default: 512)
• Increase the limit on the maximum number of open files to
at least twice the number of worker connections (default:
system limit)
• Turn off the access log for extra performance (default: on)
• Set a buffer or a time to only write logs at an interval
(default: off)
• Use keepalives to keep connections to upstream servers
open (default: 0) → You will need to set HTTP to 1.1 and
rewrite the Connection header
• Cache and share your SSL sessions between all your
NGINX processes (default: disabled)
• Send only one request to the upstream server when there
are multiple cache misses for the same file (default: off)
©2022 F5
18
Common NGINX Mistakes
That we’ve all made at some stage
©2022 F5
19
error_log
nginx.conf
1
2
3
...
error_log off;
...
nginx.conf
1
2
3
...
error_log /dev/null emerg;
...
Creates an error log named off
Redirects error log data to /dev/null
©2022 F5
20
Directive inheritance is not additive
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
http {
add_header HTTP_HEADER;
...
server {
add_header HTTP_HEADER;
...
location / {
add_header HTTP_HEADER;
add_header LOCATION_HEADER:
...
}
}
}
Sets directive
Inherits directive
Overrides directive
©2022 F5
21
ip_hash
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
http {
...
upstream {
ip_hash;
server 10.10.20.105:8080;
server 10.10.20.106:8080;
server 10.10.20.108:8080;
}
server {
...
}
}
If all your traffic comes from the same CIDR block,
use hash or any other load balancing algorithm instead
©2022 F5
22
proxy_buffering
nginx.conf
1
2
3
4
http {
proxy_buffering off;
...
}
Avoiding buffers might speed up the initial response to your client,
but it might also saturate your open connections
©2022 F5
23
stub_status
nginx.conf
1
2
3
4
5
6
server {
...
location = /status {
stub_status;
}
}
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
server {
...
location = /status {
satisfy any;
auth_basic “closed site”;
auth_basic_user_file conf.d/.htpasswd;
allow 192.168.1.0/24;
deny all;
stub_status;
}
}
Everyone can access your data
Secure access to your data
©2022 F5
24
proxy_pass
nginx.conf
1
2
3
4
5
6
7
8
9
10
http {
...
server {
...
location / {
...
proxy_pass http://localhost:3000/;
}
}
}
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
http {
...
upstream node_backend {
zone upstreams 64K;
hash;
server 127.0.0.1:3000 max_fails=1 fail_timeout=2s;
server 127.0.0.1:5000 max_fails=1 fail_timeout=2s;
keepalive 4;
}
server {
...
location / {
...
proxy_next_upstream error timeout http_500;
proxy_pass http://node_backend/;
}
}
}
Proxy to an upstream server directly
• Load balance
• Upstream stats
• Keepalives
• Passive health checks
• Define behavior if the upstream servers go down
©2022 F5
25
If is Evil
Much Computationally Expensive!
Very Segfaults 😱
If only works as intended if you use return or rewrite inside your if block
©2022 F5
26
• error_log off != turn off the error log
• Directive inheritance is not additive
• ip_hash does not work for addresses under the same CIDR block
• proxy_buffering off might lead unexpected saturated connections
• Beware of not properly securing your stat locations
• It’s better to proxy_pass to upstream groups than directly to an upstream server
• If. Is. Evil.
Recap
©2022 F5
27
Thankyouforattending!
a.faelgarcia@f5.com
alessfg
@alessfg
Alessandro Fael Garcia
Best Practices for Getting Started with NGINX Open Source
©2022 F5
29
Further Resources
• Performance-Tuning NGINX https://www.youtube.com/watch?v=YEdhuC2muOE
• Best Practices for NGINX https://www.youtube.com/watch?v=pkHQCPXaimU
• Avoiding the Top 10 NGINX Configuration Mistakes https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes
• Tuning NGINX for Performance https://www.nginx.com/blog/tuning-nginx/

More Related Content

PPTX
How to Avoid the Top 5 NGINX Configuration Mistakes
PPTX
Successfully Implement Your API Strategy with NGINX
PDF
Kubernetes
PDF
NGINX: Basics and Best Practices EMEA
PPTX
Spring Cloud Config
PDF
Introduction to Docker Compose
PDF
Kubernetes: A Short Introduction (2019)
PPTX
Getting started with Jenkins
How to Avoid the Top 5 NGINX Configuration Mistakes
Successfully Implement Your API Strategy with NGINX
Kubernetes
NGINX: Basics and Best Practices EMEA
Spring Cloud Config
Introduction to Docker Compose
Kubernetes: A Short Introduction (2019)
Getting started with Jenkins

What's hot (20)

PDF
Docker swarm introduction
PPTX
Angular 4 and TypeScript
PDF
Hands-On Introduction to Kubernetes at LISA17
PDF
Vault
PDF
Kubernetes Networking | Kubernetes Services, Pods & Ingress Networks | Kubern...
PDF
Getting Started with Kubernetes
PDF
[KubeCon EU 2022] Running containerd and k3s on macOS
PPTX
Introduction to Ansible
PPTX
Web API authentication and authorization
PDF
Api Gateway
PDF
Istio : Service Mesh
PDF
Ansible
PDF
Kubernetes security
PDF
PPTX
Ansible presentation
ODP
Kubernetes Architecture
PDF
Microservices with Java, Spring Boot and Spring Cloud
PDF
DevOps with Ansible
PDF
Keycloak Single Sign-On
PDF
How to Deploy WSO2 Enterprise Integrator in Containers
Docker swarm introduction
Angular 4 and TypeScript
Hands-On Introduction to Kubernetes at LISA17
Vault
Kubernetes Networking | Kubernetes Services, Pods & Ingress Networks | Kubern...
Getting Started with Kubernetes
[KubeCon EU 2022] Running containerd and k3s on macOS
Introduction to Ansible
Web API authentication and authorization
Api Gateway
Istio : Service Mesh
Ansible
Kubernetes security
Ansible presentation
Kubernetes Architecture
Microservices with Java, Spring Boot and Spring Cloud
DevOps with Ansible
Keycloak Single Sign-On
How to Deploy WSO2 Enterprise Integrator in Containers
Ad

Similar to Best Practices for Getting Started with NGINX Open Source (20)

PPTX
How to Avoid the Top 5 NGINX Configuration Mistakes.pptx
PPTX
Warden @ Meet magento Romania 2021
PDF
Load Balancing Applications with NGINX in a CoreOS Cluster
PPTX
NGINX: Basics & Best Practices - EMEA Broadcast
PDF
Open Sourcing NGINX Agent and Demo
PPTX
NGINX Installation and Tuning
PDF
FaSilET² full end-to-end testing solution presented at OW2con'19, June 12-13,...
 
PDF
NGINX Unit: Rebooting our Universal Web App Server
PDF
NGINX ADC: Basics and Best Practices
PDF
Sprint 17
PDF
Kubernetes laravel and kubernetes
PDF
OSMC 2021 | Icinga-Installer – the easy way to your Icinga
PDF
Présentation "Docker + Kubernetes" @ Pastis.tech #2
PDF
NGiNX, VHOSTS & SSL (let's encrypt)
PDF
How to install nginx vs unicorn
PPTX
High Availability Content Caching with NGINX
PDF
NGINX ADC: Basics and Best Practices – EMEA
PDF
High Availability Content Caching with NGINX
PDF
PDF
Capistrano deploy Magento project in an efficient way
How to Avoid the Top 5 NGINX Configuration Mistakes.pptx
Warden @ Meet magento Romania 2021
Load Balancing Applications with NGINX in a CoreOS Cluster
NGINX: Basics & Best Practices - EMEA Broadcast
Open Sourcing NGINX Agent and Demo
NGINX Installation and Tuning
FaSilET² full end-to-end testing solution presented at OW2con'19, June 12-13,...
 
NGINX Unit: Rebooting our Universal Web App Server
NGINX ADC: Basics and Best Practices
Sprint 17
Kubernetes laravel and kubernetes
OSMC 2021 | Icinga-Installer – the easy way to your Icinga
Présentation "Docker + Kubernetes" @ Pastis.tech #2
NGiNX, VHOSTS & SSL (let's encrypt)
How to install nginx vs unicorn
High Availability Content Caching with NGINX
NGINX ADC: Basics and Best Practices – EMEA
High Availability Content Caching with NGINX
Capistrano deploy Magento project in an efficient way
Ad

More from NGINX, Inc. (20)

PDF
【NGINXセミナー】 Ingressを使ってマイクロサービスの運用を楽にする方法
PDF
【NGINXセミナー】 NGINXのWAFとは?その使い方と設定方法 解説セミナー
PDF
【NGINXセミナー】API ゲートウェイとしてのNGINX Plus活用方法
PPTX
Get Hands-On with NGINX and QUIC+HTTP/3
PPTX
Managing Kubernetes Cost and Performance with NGINX & Kubecost
PDF
Manage Microservices Chaos and Complexity with Observability
PDF
Accelerate Microservices Deployments with Automation
PDF
Unit 2: Microservices Secrets Management 101
PDF
Unit 1: Apply the Twelve-Factor App to Microservices Architectures
PDF
NGINX基本セミナー(セキュリティ編)~NGINXでセキュアなプラットフォームを実現する方法!
PDF
Easily View, Manage, and Scale Your App Security with F5 NGINX
PDF
NGINXセミナー(基本編)~いまさら聞けないNGINXコンフィグなど基本がわかる!
PDF
Keep Ahead of Evolving Cyberattacks with OPSWAT and F5 NGINX
PPTX
Install and Configure NGINX Unit, the Universal Application, Web, and Proxy S...
PPTX
Protecting Apps from Hacks in Kubernetes with NGINX
PPTX
NGINX Kubernetes API
PPTX
Installing and Configuring NGINX Open Source
PPTX
Shift Left for More Secure Apps with F5 NGINX
PDF
Kubernetes環境で実現するWebアプリケーションセキュリティ
PDF
Software Delivery and the Rube Goldberg Machine: What Is the Problem We Are T...
【NGINXセミナー】 Ingressを使ってマイクロサービスの運用を楽にする方法
【NGINXセミナー】 NGINXのWAFとは?その使い方と設定方法 解説セミナー
【NGINXセミナー】API ゲートウェイとしてのNGINX Plus活用方法
Get Hands-On with NGINX and QUIC+HTTP/3
Managing Kubernetes Cost and Performance with NGINX & Kubecost
Manage Microservices Chaos and Complexity with Observability
Accelerate Microservices Deployments with Automation
Unit 2: Microservices Secrets Management 101
Unit 1: Apply the Twelve-Factor App to Microservices Architectures
NGINX基本セミナー(セキュリティ編)~NGINXでセキュアなプラットフォームを実現する方法!
Easily View, Manage, and Scale Your App Security with F5 NGINX
NGINXセミナー(基本編)~いまさら聞けないNGINXコンフィグなど基本がわかる!
Keep Ahead of Evolving Cyberattacks with OPSWAT and F5 NGINX
Install and Configure NGINX Unit, the Universal Application, Web, and Proxy S...
Protecting Apps from Hacks in Kubernetes with NGINX
NGINX Kubernetes API
Installing and Configuring NGINX Open Source
Shift Left for More Secure Apps with F5 NGINX
Kubernetes環境で実現するWebアプリケーションセキュリティ
Software Delivery and the Rube Goldberg Machine: What Is the Problem We Are T...

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administration Chapter 2
PDF
AI in Product Development-omnex systems
PDF
Digital Strategies for Manufacturing Companies
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Nekopoi APK 2025 free lastest update
PDF
medical staffing services at VALiNTRY
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Transform Your Business with a Software ERP System
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
history of c programming in notes for students .pptx
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2
AI in Product Development-omnex systems
Digital Strategies for Manufacturing Companies
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Softaken Excel to vCard Converter Software.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
medical staffing services at VALiNTRY
Online Work Permit System for Fast Permit Processing
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Transform Your Business with a Software ERP System
CHAPTER 2 - PM Management and IT Context
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Operating system designcfffgfgggggggvggggggggg
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
history of c programming in notes for students .pptx

Best Practices for Getting Started with NGINX Open Source

  • 1. Best Practices for Getting Started with NGINX Open Source Alessandro Fael Garcia Senior Solutions Engineer – Community & Alliances
  • 2. ©2022 F5 2 Source: https://news.netcraft.com/archives/2022/06/30/june-2022-web-server-survey.html
  • 5. ©2022 F5 5 Use the NGINX Open Source official repository! https://nginx.org/en/linux_packages.html
  • 6. ©2022 F5 6 TIL • nginx –t → Check if NGINX configuration is valid • nginx –T → Dump full NGINX configuration • nginx –v → Print NGINX version • nginx –V → Print NGINX package config arguments • nginx –s <start/stop/reload> → Start NGINX; stop (kill) NGINX; reload NGINX configuration (gracefully) Key NGINX Commands
  • 7. ©2022 F5 7 /etc/nginx/nginx.conf • Main NGINX configuration file • Global settings • Contains sensible defaults (when installing NGINX from our official repositories) • Avoid modifying unless you know what you are doing (defaults will work out of the box for >80% of use cases) • Includes HTTP block (adding a Stream block is one of the few cases where you’d want to modify the file) /etc/nginx/conf.d/*.conf • Default directory for additional NGINX configuration files • By default, files here are contained within the HTTP context • default.conf includes sample configuration with the NGINX default landing page • Start with a single configuration file, split your configuration into further files as necessary Recommended NGINX Directory Structure Defaults? What defaults?!
  • 8. ©2022 F5 8 Use Let’s Encrypt and Certbot for easy certs! https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal
  • 10. ©2022 F5 10 nginx.conf 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; proxy_cache_lock on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; ... } } }
  • 11. ©2022 F5 11 worker_processes 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; proxy_cache_lock on; ... } } } Make sure you spawn one NGINX worker process per CPU core (default: 1)
  • 12. ©2022 F5 12 worker_connections & worker_rlimit_nofile 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; proxy_cache_lock on; ... } } } a) Increase the worker connections to >1024 (default: 512) b) Increase the limit on the maximum number of open files to at least twice the number of worker connections (default: system limit)
  • 13. ©2022 F5 13 access_log 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; proxy_cache_lock on; ... } } } • Turn off the access log for extra performance (default: on) or • Set a buffer or a time to only write logs at an interval (default: off)
  • 14. ©2022 F5 14 keepalive 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; proxy_cache_lock on; ... } } } Use keepalives to keep connections to upstream servers open (default: 0) → You will need to set HTTP to 1.1 and rewrite the Connection header
  • 15. ©2022 F5 15 ssl_session_cache 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; proxy_cache_lock on; ... } } } Cache and share your SSL sessions between all your NGINX processes (default: disabled)
  • 16. ©2022 F5 16 proxy_cache_lock 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; proxy_cache_lock on; ... } } } Send only one request to the upstream server when there are multiple cache misses for the same file (default: off)
  • 17. ©2022 F5 17 Recap 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 28 29 30 31 ... worker_processes auto; worker_rlimit_nofile 2048; ... events { worker_connections 1024; } http { access_log off; sendfile on; tcp_nopush on; ... upstream app { server w.x.y.z; keepalive 2; ... } server { access_log /var/log/nginx/access.log main buffer=512k flush=5m; ssl_session_cache shared:SSL:10m; ... location / { proxy_http_version 1.1; proxy_set_header Connection “”; proxy_pass http://app; proxy_cache_lock on; ... } } } • Make sure you spawn one NGINX worker process per CPU core (default: 1) • Increase the worker connections to >1024 (default: 512) • Increase the limit on the maximum number of open files to at least twice the number of worker connections (default: system limit) • Turn off the access log for extra performance (default: on) • Set a buffer or a time to only write logs at an interval (default: off) • Use keepalives to keep connections to upstream servers open (default: 0) → You will need to set HTTP to 1.1 and rewrite the Connection header • Cache and share your SSL sessions between all your NGINX processes (default: disabled) • Send only one request to the upstream server when there are multiple cache misses for the same file (default: off)
  • 18. ©2022 F5 18 Common NGINX Mistakes That we’ve all made at some stage
  • 19. ©2022 F5 19 error_log nginx.conf 1 2 3 ... error_log off; ... nginx.conf 1 2 3 ... error_log /dev/null emerg; ... Creates an error log named off Redirects error log data to /dev/null
  • 20. ©2022 F5 20 Directive inheritance is not additive nginx.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 http { add_header HTTP_HEADER; ... server { add_header HTTP_HEADER; ... location / { add_header HTTP_HEADER; add_header LOCATION_HEADER: ... } } } Sets directive Inherits directive Overrides directive
  • 21. ©2022 F5 21 ip_hash nginx.conf 1 2 3 4 5 6 7 8 9 10 11 12 http { ... upstream { ip_hash; server 10.10.20.105:8080; server 10.10.20.106:8080; server 10.10.20.108:8080; } server { ... } } If all your traffic comes from the same CIDR block, use hash or any other load balancing algorithm instead
  • 22. ©2022 F5 22 proxy_buffering nginx.conf 1 2 3 4 http { proxy_buffering off; ... } Avoiding buffers might speed up the initial response to your client, but it might also saturate your open connections
  • 23. ©2022 F5 23 stub_status nginx.conf 1 2 3 4 5 6 server { ... location = /status { stub_status; } } nginx.conf 1 2 3 4 5 6 7 8 9 10 11 server { ... location = /status { satisfy any; auth_basic “closed site”; auth_basic_user_file conf.d/.htpasswd; allow 192.168.1.0/24; deny all; stub_status; } } Everyone can access your data Secure access to your data
  • 24. ©2022 F5 24 proxy_pass nginx.conf 1 2 3 4 5 6 7 8 9 10 http { ... server { ... location / { ... proxy_pass http://localhost:3000/; } } } nginx.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 http { ... upstream node_backend { zone upstreams 64K; hash; server 127.0.0.1:3000 max_fails=1 fail_timeout=2s; server 127.0.0.1:5000 max_fails=1 fail_timeout=2s; keepalive 4; } server { ... location / { ... proxy_next_upstream error timeout http_500; proxy_pass http://node_backend/; } } } Proxy to an upstream server directly • Load balance • Upstream stats • Keepalives • Passive health checks • Define behavior if the upstream servers go down
  • 25. ©2022 F5 25 If is Evil Much Computationally Expensive! Very Segfaults 😱 If only works as intended if you use return or rewrite inside your if block
  • 26. ©2022 F5 26 • error_log off != turn off the error log • Directive inheritance is not additive • ip_hash does not work for addresses under the same CIDR block • proxy_buffering off might lead unexpected saturated connections • Beware of not properly securing your stat locations • It’s better to proxy_pass to upstream groups than directly to an upstream server • If. Is. Evil. Recap
  • 29. ©2022 F5 29 Further Resources • Performance-Tuning NGINX https://www.youtube.com/watch?v=YEdhuC2muOE • Best Practices for NGINX https://www.youtube.com/watch?v=pkHQCPXaimU • Avoiding the Top 10 NGINX Configuration Mistakes https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes • Tuning NGINX for Performance https://www.nginx.com/blog/tuning-nginx/