Web server Nginx vs Apache – Which one should you choose?
Nginx or Apache – Which web server should I choose?
This is a common dilemma faced by server owners, especially when they start their business or when their existing web server creates any issues.
In our role as Technical Support Services for web hosting companies, installing, configuring and managing web servers is a major task we perform for our customers.
Among web servers, Apache and Nginx are the top players, when it comes to Linux platform. But choosing a web server is not something that has to be done blindly.
Web server Nginx vs Apache – The decision-making aspects!
The choice of web server to use depends on the type of websites running and the server specifications. In the servers we manage, we decide the web server based on 7 major aspects.
Is your server crashing often due to low memory? You cannot keep on adding more and more memory to your server, as it is not economical or a permanent fix.
A main culprit that uses up memory is the web server. When it comes to memory usage, Apache is the villain compared to Nginx web server.
Apache is configured to handle connections by creating a process for each connection. Each of these single-thread process allocates a certain amount of memory.
As a result, when traffic spikes, too many web server requests come in. This leads to spinning up of more processes, which in turn hogs up the memory and cause server crash.
By switching the MPM module of Apache from pre-fork to worker, we are able to reduce the memory overhead. But if memory is still an issue, we consider switching to Nginx web server.
Nginx uses a single-thread to handle the web server connections. Even when connections increase, new threads are not spawned. This helps to save memory and avoid a crash.
So, for web hosting providers who run websites in memory constrained server instances such as VPS, we configure Nginx as the web server to avoid a crash.
Apache web server is based on processes. But Nginx is based on events. So, when the number of web server requests increases, Apache has to spin up more processes.
This increase in process count can overload the CPU. The web server performance gets affected. And websites start to slow down with traffic, and they take ages to load.
Nginx, on the other hand, is designed to deliver stable performance even in case of spiking traffic. It just needs to run one process per CPU, than one process per connection.
Due to its event-based design structure, Nginx can handle four times more requests concurrently than what Apache can handle. This helps to avoid a load spike in the server.
So, for servers that have to handle increasing web traffic, we configure Nginx as web server. For servers with stable traffic and CPU usage, Apache is sufficient to handle the requests.
Apache uses one process to handle one web server connection. So the number of connections possible simultaneously to an Apache web server is limited.
But in Nginx, each worker process can handle thousands of web server connections at the same time. Within the hardware limitations, Nginx can scale up easily to accommodate increasing traffic, which Apache can’t do.
Both Apache and Nginx have a secure code base, but both of them get struck by security vulnerabilities intermittently. With its centralized single configuration management, Nginx is slightly more secure than Apache.
In the web servers we manage, we configure additional security modules and hardening tools in both Apache and Nginx, to secure them further to avoid an exploit.
With the help of our 24/7 security team, Bobcares engineers detect and patch vulnerabilities and exploits as soon as a security advisory is published.
In Apache, there is a main configuration file called httpd.conf, which is used for global server configuration. In addition, users have the option to manage their own configuration parameters.
This is done with the help of ‘.htaccess’ files in directories. Users can set redirect rules, password protection, directory listing, restrictions, etc. using this file.
But in Nginx, there is only central configuration possible using nginx.conf file. All changes are to be made by the server administrator and individual users cannot override these settings.
So, in shared web hosting where it is difficult to tweak each user setting in the main configuration file each time, Apache is preferred due to its ‘.htaccess’ customization feature.
But parsing through the directory level configuration files can be an overhead in Apache web server, slowing it down further. In such cases, we limit this feature based on user requirements.
6. Website type
Website files are of two types – static and dynamic. Static files include CSS, JS, images, etc., which do not require any real-time processing.
Dynamic pages are ones that can be produced only after processing them. These include files like PHP, Python or other applications such as Nodejs.
Apache web server is skilled to parse and execute various scripting languages, which enables it to process dynamic pages very fast.
Nginx does not have this ability to process dynamic pages efficiently. But it can serve static website content real fast, with its static content cache.
In servers with only Nginx web server, we configure PHP-FPM application to process dynamic contents like PHP. To further speed up the websites, we configure caching tools like Varnish or Memcached.
7. Load Balancing
Load Balancer setup is preferred in many scenarios such as running multiple application instances or distributed web server requirements, where high web traffic volume is involved.
Nginx can be easily configured as a Load balancer, to collect web traffic and distribute it to the appropriate web server and parse the output obtained.
Static pages are processed by the Nginx itself and dynamic page requests is transferred to the appropriate web server. This load balancing helps to improve website speed without causing resource overhead.
By configuring caching in Nginx, we further improvise the Load balancer setup to reduce website latency and to improve the server performance.
Nginx with Apache – The team effort!
We have seen the difference between Nginx and Apache with regard to the major web server criteria. But in real life scenarios, these parameters may not be so crystal clear to differentiate.
Most often, websites can be a combination of both static and dynamic pages, and not just one type. In such cases, we use a combination of both Nginx and Apache.
We configure Nginx as a reverse proxy. Nginx will act as the front end to collect web server requests. Static pages are handled by Nginx itself.
Dynamic pages such as PHP, Python, etc. are handed over to the Apache web server, which is in the back end and listening to another port. The results from Apache are then handed over to client via Nginx.
By using this reverse proxy setup with Nginx and Apache, we are able to offload web traffic between both servers and get the best out of both these servers.
We perform further tweaks in the web servers to deliver the optimal performance. To ensure that the web servers work fine even in increased load, we do periodic stress test and ongoing updates.
Today we saw how our Dedicated Support Engineers configure the web server in our customers’ servers after assessing the server instance type and the business purpose. This helps us to configure high performing web servers that are fast and stable.