Optimizing Nginx configuration

Nginx can work with quite heavily loaded environments out of the box. However, by tweaking its parameters, performance can be significantly improved. This kind of optimization is usually called tuning.

How to configure

Typically, configuration file is named nginx.conf. It's located:

Debian
/etc/nginx/nginx.conf
Freebsd
/usr/local/etc/nginx/nginx.conf

Configuration file usually looks like this:

user www-data;
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    ...
}

Parameters

Connections

Maximum number of connections that Nginx can handle simultaneously can be determined by multiplying two parameters:

Total connections = worker_processes x worker_connections
Nginx worker_processes и worker_connections
worker_processes auto;

# Sets the number of worker processes. it's better to set this parameter to "auto".

worker_connections 1024;

# Sets the maximum number of connections for a single worker process. Best values are 1024...4096.

Use directive allows to set connection selection method. Certain methods should be used for different operating systems:

Linux
use epoll
Freebsd
use kqueue
Nginx epoll

By default, Nginx will try to select the most appropriate method automatically.

Request processing

multi_accept on;

# Allows receiving maximum possible number of connections

sendfile on;

# sendfile method is more effective than standard read+write

tcp_nodelay on;
tcp_nopush on;

# Enables sending headers and beginning of file in a single packet

File information

Nginx can cache some files that it servers (especially those generated by php/python/ruby/...). If such files are frequently accessed, caching can dramatically speed up everything.

open_file_cache max=200000 inactive=20s;

# Sets maximum number of files in cache

open_file_cache_valid 30s;

# Sets cache ttl

open_file_cache_min_uses 2;

# Enables caching data for files that have been accessed at least 2 times

open_file_cache_errors on;

# Enables caching data about missing files

Logging

The good idea is to disable access log to save some resources on disk operations. It's better to setup error log to log only critical issues as well.

access_log off;
error_log /var/log/nginx/error.log crit;

Gzip compression

Using compression is a must have as it significantly reduces traffic between server and client. You can check whether compression is enabled by using gzip checker.

gzip on;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

# Enables compression of all listed file types

Handling clients

Keepalive connections will reduce creation of repeated client-server connections.

keepalive_timeout 30;

# Enables 30-second timeout before closing keepalive connection

keepalive_requests 100;

# The maximum number of keepalive requests from a single client

Lots of problems can be caused by slow (laggy) clients. Slow request transfer and unexpected connection’s closing by the client can create large number of extra connections.

reset_timedout_connection on;

# If client stops responding, Nginx will reset the connection

client_body_timeout 10;

# Enables 10-second timeout waiting for request body from the client, after which the connection is reset

send_timeout 2;

# If client stops reading the response, Nginx will wait for 2 seconds and reset the connection

Sending large sized requests to the server (e.g. large files uploads) should be limited, if your website isn't designed for this.

 client_max_body_size  1m;

# In this case, the server will not accept requests larger than 1Mb

Reload Nginx after changing the settings:

nginx -s reload

Complete configuration

worker_processes  auto;
events {
    use epoll;
    worker_connections 1024;
    multi_accept on;
}
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log off;
    error_log /var/log/nginx/error.log crit;

    keepalive_timeout  30;
    keepalive_requests 100;

    client_max_body_size  1m;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 2;
    sendfile on;
    tcp_nopush on;

    gzip on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    open_file_cache max=200000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
}

The most important

Best effect for visitors happens when gzip compression is enabled. Everything else will reduce server load and will allow to handle more visitors simultaneously.

Подпишитесь на Хайлоад с помощью Google аккаунта
или закройте эту хрень