NGINX is a popular open source web server that aims to be an alternative to Apache with better performance on handling large volumes of requests.
As with any technology learning how to configure it properly and troubleshoot errors can be challenging. If the configuration is incorrect, it can affect performance and ability of the web server to handle the load demand. That great looking web site can quickly go downhill if the server is slow and starts returning blank pages (500 errors).
A 500 error is http code meaning that something went wrong with the server. If the site always gives 500 that could be an issue on the website. If the site is working correct and it gives a 500 then it could be a time out issue caused by a lot of connections.
Here are the key technologies involved in this discussion:
- NGINX (Version 1.4.6 and later)
- Linux (Ubuntu 14.04 and later)
Here are two of the key configuration parameters to look at:
- worker_processes – Is a single thread process. This means that it will execute in a sequence way. We typically set this at 1 worker process per core CPU
- worker_connections – Is the maximum number of simultaneous connections that can be opened by a work process. We typically set this at a value of 1024 * the number worker_process. If we have 2 worker process and 1024 worker connections. We will be able to server 2048 clients
Nginx uses an asynchronous, event-driven approach where requests are handled in a single thread. And it was built for high-traffic websites.
Below are the steps that you need to review in order to setup and mitigate server errors:
- Find out how many CPUs the system has configured, you can do this with the following command under a terminal session:
$ grep processor /proc/cpuinfo | wc -l
That will give you the # of processors, in this example it is 2
- Edit Nginx Config – For Ubuntu is located in the following directory:/etc/nginx/nginx.conf
- Look for the worker_processes in nginx.conf and set it to match the # of CPUs – which is 2 in this example:
The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. You can start with setting it to match the CPU cores, but you might be able to increase it if there are more overall resources in the system to handle load, or you may need to decrease it if there are other bottlenecks that will limit the worker processes..
- Next look for worker_connections and set it to be the number of CPUs * 1024. In our example that is 2 * 1024 = 2048. Worker connections is the number of simultaneous connections that can be opened by a worker process.
You can tweak this as needed based on your results.
Useful Reference links:
We hope you found this post useful. If you have questions, please feel free to contact us