Nginx is a widely used webserver, which ranks for better choice to serve static and dynamic web content faster. In present era, computing resources are shaped up in broader room. Optimization of service holds up as a priority to use these resources optimally. Nginx optimization is the one, which this article speaks about.

Generally in linux systems, nginx config files can be located in /etc/nginx among it nginx.conf is main config file. Tuning parameters found in this file to correct values thumbs up performance. Following points projects how to do it.

I. nginx.conf

1. “worker_processes“:

It can be considered as one worker process per processor core.

2. “worker_connections“:

Commonly, 1024 concurrent connections per server is enough. If these connections are not enough can raise it, by doubling it. But remember higher value may lockdown nginx with I/O operations like slow disks.

3. “sendfile“:

By keeping “sendfile on“, makes nginx to ignore content of files it is sending, thus freeing resources for the administration level (request confirm).

4. “keepalive_timeout

Set this values as low as (2 or 3 seconds). It makes connection killed if no requests are received during the time frame for established connection, therfore serving requests makes better.

5. “open_file_cache”

It helps nginx to keep open file descriptors (modification time, file and directory parameters) to serve fast. “open_file_cache max=10000 inactive=30s” Sets maximum of 10000 files and valid for 30 sec, can be tuned respective to available memory.

6. “open_file_cache_min_uses

It sets the value for minimum no.of access till file descriptor remains in the cache.

open_file_cache_min_uses 2

7. “client_max_body_size

It is which client maximum request body size. Tune it with proper value to avoid (413 Request entity to large error), here we keep it as 100MB. “client_max_body_size 100M

8. “client_body_buffer_size

It sets buffer size for reading client request body. Tune it w.r.t client_max_body_size. Here we kept it as 128K. (client_body_buffer_size 128k)

9. “server_names_hash_bucket_size

 If there is a situation where nginx need to parse large number of server names or if a long text of server names are defined, then server_names_hash_bucket_size directive need to be set. Generally it has default value of 32 and 64. “server_names_hash_bucket_size 64”

10. “gzip

Compressing amount of network data sent by nginx upon client requests, will not only reduce size of data but also transfer in minimal time.

i)gzip on 

To enable gzip compression.

ii)gzip_disable “msie6”

Disables gziping for headers with MSIE 6

iii)gzip_vary on;

It enables inserting the “Vary: Accept-Encoding” response header field

iv)gzip_comp_level 6

 It sets compression level 1 to 9, don’t’ keep it higher value since it is CPU bound.

v)gzip_buffers 16 8k

  Sets the no.of buffers and size of it, for compression. Analyze memory page of platform and set it.

vi)gzip_http_version 1.1

  Sets the minimum HTTP version of a request required for compression.

vii)gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript text/js

These are all MIME types which are subject to compression.

11. “access_log

Access logs are the one which puts on overhead of disk writes for each request. If they are  not needed one can turn off. “access_log off”

II. Virtual host file:

1. Cache Control for Static Files

Browser caching will save resources for repaired transfer of static files. 

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 30d;
        log_not_found off;    
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files $uri =404;
        } 

 

i)”expires”

Directive sets expiry headers for static files in browser cache.

ii)”log_not_found”

Disables logging of errors about not found files.

iii)”add_header”

Standard headers which can be assigned for expiry files. 

iv)”try_files”

Either serve the image or go to 404 error.

2. If nginx serves dynamic content processed by FastCGI Process Manager like PHP-FPM, then  following parameters also need to be tuned.

i)”fastcgi_buffering

Enables buffering of responses from fastcgi server. “fastcgi_buffering on

ii)”fastcgi_buffer_size

It sets buffer size for responses received from fastcgi server, platform dependent.

fastcgi_buffer_size 128k

iii)”fastcgi_buffers

It sets the number of buffers and size of it, used for reading a response from the FastCGI server, for a single connection.

 “fastcgi_buffers 4 256k

iv)”fastcgi_busy_buffers_size

It sets the busy buffer size, By default, size is limited by the size of two buffers set by the fastcgi_buffer_size.

fastcgi_busy_buffers_size 256k

3. To enable nginx to use FastCGI Process Manager like PHP-FPM and tune timeout parameters if php-fpm takes more time to serve.

 i)”fastcgi_connect_timeout

Defines a timeout for establishing a connection with a FastCGI server

 ii)”fastcgi_send_timeout

To avoid 502 bad gateway, set a timeout for transmitting a request to the FastCGI server.

iii)”fastcgi_read_timeout

To avoid 502 bad gateway, set a timeout for reading a response from the FastCGI server.

These config makes nginx to perform better, when compared to default settings.

Note: Optimizing FastCGI Process Manager like “PHP-FPM” is an another post.