Skip to content

Instantly share code, notes, and snippets.

@redmoses
Last active September 1, 2021 07:30
Show Gist options
  • Save redmoses/10002515 to your computer and use it in GitHub Desktop.
Save redmoses/10002515 to your computer and use it in GitHub Desktop.
Nginx Tuning
Requirement: Serve around 200 requests/second with with a system of 4 CPU core and 8 Gb of memory
Given Configuration
===============================================================
user www-data;
worker_processes 8;
pid /var/run/nginx.pid;
events {
worker_connections 4096;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
types_hash_max_size 2048;
….
….
}
===============================================================
----------------------------------------------
## The problem with the above configuration ##
----------------------------------------------
We would have to serve an application that is mostly static. This means that we
would not have to perform much processor intensive operation. So rather than using
2 worker processes per core we should be using 1 work process per core.
worker_processes 8 -> 4
------------------
## Optimization ##
------------------
To further optimize the performance of the Nginx server we can make the following configuration changes
~ Increase limit of open files (so that Nginx can create greater number of connections).
Add the below line after “worker_processes”
worker_rlimit_nofile 65000;
~ Use Epoll so that Nginx server prioritizes the active connections.
Add the following line inside the “events” section.
use epoll;
~ Modify GZip configurations so that network performance can be improved
gzip on;
gzip_static on;
gzip_proxied any;
gzip_min_length 256;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
~ Modify caching mechanism to decrease Disk I/O
open_file_cache max=65000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
------------------------------
## External System Settings ##
------------------------------
~ Change the limit number of open file for Nginx system user, so that the webserver doesn’t get
blocked while opening connections.
~ Configure limits.conf
- sudo nano /etc/security/limits.conf # open the file
- Add the following lines at the end of the file
www-data soft nofile 200000
www-data hard nofile 200000
~ Configure the common-session file
- sudo nano /etc/pam.d/common-session
- Add the following line at the end of the file
session required pam_limits.so
~ Reboot the system
- sudo reboot
~ Check the changed value
- ulimit -n
~ Change the following kernel parameters to increase the number of ephemeral ports,
reduce TIME_WAIT and increase the allowed listen backlog:
~ Use the following commands to make the changes
- echo "2048 64512" > /proc/sys/net/ipv4/ip_local_port_range
- echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle
- echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse
- echo "10" > /proc/sys/net/ipv4/tcp_fin_timeout
- echo "65536" > /proc/sys/net/ipv4/tcp_max_syn_backlog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment