CentOS - Nginx Configuration

Whether you have installed Nginx using the package manager or from source, you will need to look at the main configuration file and see what may need changing and optimizing.

Although I'll make some suggestions, the aim is not to change a great deal at this point. Rather, we will look at the main settings, see what they mean and what a change will actually do.


So why only a few changes to the default? Well, it's difficult to give a definitive configuration as there are so many variables to consider such as expected site traffic, Slice size, site type, etc.

During this article we'll discuss the main settings and you can make any decisions as to what you feel are best for your site. Any changes I do suggest are simply that: suggestions.

My advice is very simple: experiment. Find what works best on your setup.


Assuming you installed via the package manager, open up the main Nginx config file:

sudo nano /etc/nginx/nginx.conf

If you installed from source, the location may be different:

sudo nano /usr/local/nginx/conf/nginx.conf

The default file is very similar in both case (again, assuming you followed the articles shown above):

user              nginx;
worker_processes  1;

error_log         /var/log/nginx/error.log;

pid               /var/run/nginx.pid;

events {
    worker_connections  1024;

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

The main difference you will see if you installed from source is the path in the 'include' setting which would be something like:

include /usr/local/nginx/conf/conf.d/*.conf;

Beyond that, any changes are minor and can be adjusted as discussed below although I won't mention some of the more obvious settings such as logs and pid's. You will also notice that this file has more settings included if you installed Nginx using Yum.


Default using Yum:

user nginx;

Default when installing from source:

user nobody;

As you can imagine, this sets the user that the Nginx process runs as.

You can also add a group to this setting if you would like:

user nginx nginx;

It is not necessary to modify this setting for either installation as it will not affect the performance of Nginx.



worker_processes  1;

Nginx can have more than one worker process running at the same time.

To take advantage of SMP and to enable good efficiency I would recommend changing this to read:

worker_processes  4;

Although you can experiment with this number (and I encourage you to do so) setting it at more than 4 processes may actually cause Nginx to be less efficienct on your Slice.



events {
    worker_connections  1024;

Note the worker_connections setting is placed inside the 'events' module.

This sets the number of connections that each worker can handle. 1024 is a good default setting.

You can work out the maximum clients value by multiplying this and the worker_processes settings:

max_clients = worker_processes * worker_connections

HTTP Module

Next comes the http module which contains base settings for http access:

include       /etc/nginx/mime.types;
default_type  application/octet-stream;

Unless you have an overwhelming desire, I would leave these settings alone (again, for those who installed via source, adjust the paths to those of your install).

You can, of course, add more includes if you want to customize it but messing with mime-types usually ends up with broken web pages and download errors.

Mind you, it is good fun to play with!



sendfile        on;

Sendfile is used when the server (Nginx) can actually ignore the contents of the file it is sending. It uses the kernel sendfile support instead of using it's own resources on the request.

It is generally used for larger files, such as images, which do not need use of a multiple request/confirmation system to be served - thus freeing resources for items that do need that level of 'supervision' from Nginx.

Keep it an on unless you know why you need to turn it off.



#tcp_nopush     on;

tcpnopush: Sends the HTTP response headers in one packet. You can read more about tcpnopush here

I would change the default here and uncomment the setting as it is useful when combined with the sendfile option we set earlier.



#keepalive_timeout  0;
keepalive_timeout  65;

The default is very high and can easily be reduced to a few seconds (an initial setting of 2 or 3 is a good place to start and you will rarely need more than that). If no new requests are received during this time the connection is killed.

OK, but what does it mean? Well, once a connection has been established and the client has requested a file, this says "sit there and ignore everyone else until the time limit is reached or you get a new request from the client".

Why would you want a higher time? In cases where there will be a lot of interactivity on the site. However, in most cases, people will go to a page, read it for a while and then click for the next page. You don't want the connection sitting there doing nothing and ignoring other users.



#gzip  on;

Gzip allows for instant, real time compression. This setting is commented by default. We like compression so let's uncomment it.

However, I would add a few more settings as follows:

gzip_comp_level 2;
gzip_proxied any;
gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml 
application/xml+rss text/javascript;

I think those are self explanatory and simply add to the gzip setting. You can read more about the various gzip settings on this page



include /etc/nginx/conf.d/*.conf;

If you installed from source, we added this line:

include /usr/local/nginx/conf/conf.d/*.conf;

Either way, it defines what files to include that are located outside of the main nginx.conf.

In this case, it points to the conf.d directory so it will include any configuration files located within this directory so long as they end in .conf. Thus, enabling any available Vhosts.


There is a lot going on in this article, especially from such a small config file.

However, taking one setting at a time, we can see that each one is not only essential but pretty flexible.

The next article will take you through setting up virtual hosts.

Article Comments:

Nnyan commented Wed Jan 13 22:25:42 UTC 2010:

Thank you for the great article! One note, when I created the init.d script and use that to restart nginx I get the following error:

Starting nginx: [warn]: duplicate MIME type "text/html" in /usr/local/nginx/conf/nginx.conf:36

If I take out the "text/html" part of your changes it stops giving me that error.

spirit commented Thu Dec 16 14:58:58 UTC 2010:

You advice to use worker_processes 4; but I thought that this param must reflect the number of core of your CPU. How many core do 1 slice correspond to?

Want to comment?

(not made public)


(use plain text or Markdown syntax)