Five ways to optimize your Nginx server

Nginx is an open-source HTTP(web) server and reverse proxy, and an IMAP/POP3 proxy server (email server). It was developed to tackle the C10K problem that web servers in use at the time, which was largely Apache, were facing. C10K is simply a numeronym that stands for concurrently handling 10000 connections. Nginx was designed with the drawbacks of Apache in mind.

Five ways to optimize your Nginx server

Although Nginx is by default, fast and nimble, it can be fine-tuned to perform at its peak, by modifying the following parameters:

1. Worker Processes

Unlike Apache, Nginx does NOT create a new process for every connection that hits it. It instead has a predefined (set by an admin) number of worker processes set for the main Nginx process. Each worker process has the capability to serve thousands of concurrent web requests. Processes and threads are what utilizes processor memory and since, in the case of Nginx, there are very few processes and threads irrespective of the number of connection, the memory utilized is low and the performance stays high. The two main parameters are:

  • worker_processes: This parameter defines the number of Nginx worker processes (default is 1). The optimum number of worker processes you should set is one per processor available on the server, but can be increased if load incurred increases.
  • worker_connections: This parameter defines the maximum connections one process can handle concurrently. The default is set to 512, but considering the configuration of most servers today this can be increased. This value entirely depends on the resources available. Experiment to see what works best.

2. Buffer Size

With buffering enabled, user connects to Nginx which proxies the request to a backend server, then takes the backend server’s response and waits to see if the user needs this information. If the user is slow and doesn’t need the rendered info right away, it caches this data and terminates the backend connection. This allows Nginx to avoid unnecessary load and keeps it nimble.

Buffer sizes can be adjusted by modifying the following handles:

  • client_body_buffer_size sets max buffer size for reading client request body. 128K is the suggested value.
  • client_max_body_size sets the maximum client request body size.
  • client_header_buffer_size adjusts the client header size. 1K is a safe value.
  • large_client_header_buffers adjusts the max number and also size of buffers, for large client headers. 4 and 4K is the advised value.

3. Compression

Compressing data greatly improves data transfer rate and aids in improved, seamless user experience. It however uses additional worker process overhead which in turn means more memory utilization, but Nginx is designed to smartly use memory so compression comes at an affordable price.

  • Nginx can be configured to compress data by setting the gzip parameter to ON.
  • By default, proxied data is not compressed but can be made to do so by modifying the gzip_proxied parameter.
  • The gzip_min_length can be used tell Nginx the minimum size a file should be for it to be compressed. Setting this to 5000 would mean files larger than 5k will be compressed.

4. Caching

Nginx can also be set up to utilize a proxy cache. It can cache regularly accessed backend data to completely avoid making a backend connection, and provide cached data when required. Expire headers can be set for files that don’t change, for example images, by adding the following code:

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;

5. Timeouts

Timeouts can be set to tweak and improve server performance. Timeouts tell the server how long an idle connection can be kept active before terminating.

  • client_body_timeout: Time Nginx server waits for a client body to be sent after a request is made. Default is 60s
  • client_header_timeout: Time Nginx server waits for a client header to be sent after a request is made. Default is 60s

Note: when both these timeouts are reached, the server issues a request timeout error or a 408 error

  • keepalive_timeout: Nginx closes keep-alive client connections after this period. Default is 75s
  • send_timeout: Time Nginx will allow to pass between two successive write operations, before shutting the connection. Default is 60s.

With just these 5 basic modes, your Nginx server should instantly begin to show signs of improved performance. Happy surfing, and you’re welcome.