Site icon rtCamp

Optimizing Nginx Configuration

Nginx needs least tweaking as compared to PHP & MySQL.

Still to get the most out of Nginx, you can tweak few directives in /etc/nginx/nginx.conf

worker_processes

This is very important. It controls number of worker processes Nginx is running.

Set worker_processes = number of processors in your system.

To find out how many processors you have on your server, run the following command:

grep processor /proc/cpuinfo | wc -l

It will display a number only:

32

Set that number as value forworker_processes directive.

worker_connections

If you are running very high traffic sites, its better to increase value of worker_connections. Default is 768.

Theoretically, nginx can handle max clients = worker_processes * worker_connections

We use worker_connections = 10240

worker_rlimit_nofile

Increase number of files opened byworker_process.

This directive is not present by default. You can add it in /etc/nginx/nginx.conf in main section (belowworker_processes)

We use worker_rlimit_nofile 100000;

keepalive_timeout

I have come across many definitions for this. Apart from traffic, response time of FASTCGI backed application is a factor to be considered while tweaking this directive. Its default is 65 or 75 seconds.

We use keepalive_timeout = 30s

Rarely Used

Following two directives need tweaking in rarely. When the time is right, Nginx, itself, will tell you to make a change! 😉

Nginx will throw a friendly error like below:

Reloading nginx configuration: nginx: [emerg] could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64

server_names_hash_bucket_size

When you have a domain name longer than 64-chars, you will need it.

By default, its value is 64. If you have a domain 80-chars wide, do not set it to 80. Instead use next value of multiple of 2.  That means 128, 256 and so on.

server_names_hash_max_size

Its default value is 512. If you are hosting hundreds of sites on your server.

Nginx suggests, you can change eitherserver_names_hash_max_size orserver_names_hash_bucket_size to accomodate large number of sites, but I prefer keepingserver_names_hash_bucket_size as it is and makingserver_names_hash_max_size big in multiple of 2’s till error disappears.

On a server, where we host 300+ sites, we needed to change it to 8192!

I used a trick to find out correct size by using following command:

ls /etc/nginx/sites-available/ | wc -c

Above command list of enabled sites’ name which I pass towc command to get number of total characters allserver_name are using together. In my case, above command returned 7414, for which next 2’s multiple value is 8192.

In each site config, I was using only 1server_name without any wildcard or regex. I guess wildcard & regex will also affect value of server_names_hash_max_size .

WordPress Multi-site Subdomain/Domain-Mapping & server_name hashing

As you know, you can host millions of subdomains/domain-mapping using a WordPress multisite setup. But as far as my experience goes, it doesn’t put any load on Nginx’s server_name hashing.

More…

Exit mobile version