How To Optimize Nginx Configuration With Example

About Nginx

Nginx is a free, open source web server. It’s fast and lightweight when compared to the Apache web server. It provides accurate, predictable performance under very high loads.

Working Configuration Files

Configuration files for nginx are located in /etc/nginx/ directory.
It’s always a good idea to copy any working configuration file before you begin making changes to it.So if anything goes wrong you can restore it from backup file. You can do it by using the following copy command:

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup

Core directives

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 768;
}

Where,
user nginx: Linux system user which will own and run the nginx server.
worker_process: This directive defines the number of worker processes usually by default it is 1.
error_log: Defines core directive used by ningx to control system logging.
pid: This defines where nginx will write its PID.

Worker Processes

It’s common practice to run 1 worker process per core. To find out what number you will need to set worker_processes to, Use the following command to find the number of cores you have on your server:

[[email protected] ~]# grep processor /proc/cpuinfo | wc -l

If this value returns as 1. Then it means the amount of cores on our system is 1.

Worker Connections

It is the maximum number of connections that every individual worker process can handle simultaneously. It’s default value is 768. To set this value we need to check our core limitations by using the ulimit command:

[[email protected] ~]# ulimit -n
1024

The output we get from this command in this case is: 1024

Now we need to update our configuration file:

vim /etc/nginx/nginx.conf
events {
worker_connections 1024;
}

Buffers

If the buffer sizes are set too low in the configuration file, then engine x will write to a temporary file causing for excessive disk I/O.

client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;

Where,
client_body_buffer_size: Most of the client buffers come from a POST method in form submissions.
client_header_buffer_size: It handles the size of client header.
client_max_body_size: The error 413 (Request Entity Too Large) is returned to the client if the size request exceeds the configured value.
large_client_header_buffers: It is the maximum number and size of buffers which can be set for large client headers.

After optimization process changes that you have made. Make sure that you haven’t made any typos or added any invalid entries. for this use following command:

[[email protected] ~]#nginx -t

Timeout

Timeout are important because they can drastically improve our web server.
client_body_timeout and client_header_timeout sends directives for the time till server has to wait for a header and body  to be sent after the request. If neither a header or body is sent, then the server will issue a Request time out or error 408.

send_timeout shuts down the connection if the client does not read any data for this amount of time.
keepalive_timeout – This directive is used to set how long to keep the idle keepalive connection open.

Configuration file location: /etc/nginx/nginx.conf

http {
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
}

After changes use the following command

[[email protected] ~] nginx -t

If everything ok then restart the service otherwise check your configuration file for errors.

Cache Static Files

We can set the Expires HTTP header with the help of the expires directive, which we can place in inside of http {}, server {}, location {} blocks.

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

In this example, all .jpg, .jpeg, .gif, .png, .ico, .js and  .css files get an Expires header with a date 360 days in the future from the browser current access time.

Reload service after changes

I will a post separate post on Caching in detail later.

Compression

The gzip directive can be used to enable the compression module available to limit the amount of bandwidth used and speed up network transfers.

gzip on;
gzip_vary on;
gzip_comp_level 2;
gzip_min_length 1024;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";

This setting enables compression for files that are over 1k.
gzip_comp_level value can be set between 1 and 9, where 1 requires the least amount of CPU cycles and 9 requires the greatest.

Logging

We can set the access_log directive in the http block in the configuration file or in the server block for any specific virtual domain.

access_log /var/log/nginx/access.log main;

If you don’t require this information then it’s preferable to disable it. You can disable the access log by editing nginx configuration file.

access_log off;

That’s all for now from me. If you have any questions? Leave a comment below.

Add Comment