Tips / Nginx

Nginx.conf Configuration Example

Configuring Nginx is pretty easy, even if you are a newbie and have no previous experience. In this post, we will give you an example of a simple but effective Nginx.conf configuration. It includes all of the common options and directives that most modern websites need.

First, you have to locate your Nginx.conf file. It can be in different locations depending on the OS you are using or how you compiled Nginx. These are the most common ones:


Then, start editing:

nano -w /etc/nginx/nginx.conf
user  nginx;
worker_processes  4;
error_log  logs/error.log crit;

worker_rlimit_nofile  8192;

events {
worker_connections  1024; # you might need to increase this setting for busy servers
use epoll; #  Linux kernels 2.6.x change to epoll

http {
server_names_hash_max_size 2048;
server_names_hash_bucket_size 512;

# Hide server info
server_tokens off;

# Mimetype configurations
include    mime.types;
default_type  application/octet-stream;

# Performance
sendfile on;
tcp_nopush on;
tcp_nodelay on;

# Gzip configuration
gzip on;
gzip_min_length  1100;
gzip_buffers  4 32k;
gzip_types    text/plain application/x-javascript text/xml text/css;

# Other configurations
ignore_invalid_headers on;
client_max_body_size 10m;
keepalive_timeout  10;
client_header_timeout 60s;
client_body_timeout 60s;
send_timeout 60s;
client_header_buffer_size 4k;
large_client_header_buffers 4 64k;
output_buffers   4 32k;
postpone_output  1460;

# Cache most accessed static files
open_file_cache          max=10000 inactive=10m;
open_file_cache_valid    2m;
open_file_cache_min_uses 1;
open_file_cache_errors   on;

# Virtual host includes
include "/etc/nginx/conf.d/*.conf";

Explanation of Each Directive

user nginx: This is the user that Nginx will run as. The other options is nobody. This is usually used for Nginx reverse proxy with Apache

worker_processes 4: This is your system’s number of CPUs. If you have less CPUs, then tweak it to match the actual number of CPUs.

error_log logs/error.log crit: This is where you configure the error log location and set it to only log critical errors and to avoid warnings. If you want warnings enabled, change “crit” to “warn.” On large websites, you must watch it to keep this file from growing a lot.

worker_rlimit_nofile 8192: This sets a limit on the maximum number of open files for worker processes.

worker_connections 1024: This is the total number of simultaneous connections per worker. You might need to increase this setting for busy servers. It is important to remember that the actual number of simultaneous connections can’t exceed the current limit on the maximum number of open files (previously set by worker_rlimit_nofile directive).

use epoll: Nginx supports a lot of connection processing methods. The most used for modern kernels (2.6.x) is epoll to get the best performance while processing information.

server_names_hash_max_size 2048: This sets the maximum size of the server name’s hash tables.
server_names_hash_bucket_size 512: This is the bucket size for the server name’s hash tables.

server_tokens off: This allows you to hide your server information from the web headers.

include mime.types: This includes the most common mime types used on websites.
default_type application/octet-stream: This sets the default mime type.

sendfile on: This allows Nginx to transfer data from a file descriptor to another directly in kernel space. This is especially good for saving system resources and pushing more speed.

tcp_nopush on: This optimizes the amount of data sent. It must be enabled if sendfile is on.
tcp_nodelay on: This allows you to bypass system delays and send the data as soon as it is available.

gzip on: This enables Gzip compression
gzip_min_length 1100: This is the minimum length for compressing a file. If a file is less than 1100 bytes, it will not be compressed.
gzip_buffers 4 32k: This configures Gzip buffering.
gzip_types text/plain application/x-javascript text/xml text/css: This enables Gzip compression for the most popular mime types.

ignore_invalid_headers on: This directive determines whether or not header fields with invalid names (non English letters, digits, hyphens, and underscores) should be ignored.

client_max_body_size 10m: This is the maximum allowed size of the client request body, set to 10Mb on this example. When the limit is reached, Nginx will show a 413 (Request Entity Too Large) error.

keepalive_timeout 10: This sets the timeout in seconds for keepalive connections on the server side.

client_header_timeout 60s: This sets the timeout for reading client request headers. When the limit is reached, a 408 (Request Time-out) error will be expected.

client_body_timeout 60s: This is the timeout for reading client request body. Just like client_header_timeout, it will show a 408 error when the limit is reached.

send_timeout 60s: This is the timeout for transmitting a response to the client between two successive write operations. If the limit is reached and the response is not yet transmitted, the connection will be closed.

client_header_buffer_size 4k: This variable sets the buffer size for reading client request headers.

large_client_header_buffers 4 64k: configures maximum number (4) and size (64k) of buffers used for reading large client request headers

output_buffers 4 32k: This allows you to configure the number (1) and size (32k) of the buffers used for reading a response from a disk.

postpone_output 1460: The transmission of client data will be postponed until Nginx has at least the specified size (1460 on this example) of bytes of data to send.

open_file_cache max=10000 inactive=10m: This configures a cache for open file descriptors, their sizes and modification times, information on existence of directories and more. Max=10000 sets the max number of elements inside the cache. Inactive=10 is the configured time after an element is removed from the cache if it hasn’t been accessed during that time (10 minutes in this case).

open_file_cache_valid 2m: This is the time limit after which open_file_cache elements should be validated.
open_file_cache_min_uses 1: This is the minimum number of file accesses (during the period configured by the inactive parameter from open_file_cache variable). It is required for a file descriptor to remain open in the cache.
open_file_cache_errors on: This enables caching of file lookup errors.

include “/etc/nginx/conf.d/*.conf”: This includes your virtual hosts files inside /etc/nginx/conf.d directory. All files ending with the .conf file extension will be included.

Once you have your nginx.conf file configured, reload Nginx to apply the changes:

service nginx reload

Most of these directives are part of the Nginx ngx_http_core_module. If you need to dig into more options or examples, you can access the full documentation at any time.

Popular search terms:

  • nginx conf example
  • nginx conf examples
  • example nginx conf file
  • taiga trello importer Connection timed out) while reading response header from upstream

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking