This is a guest post written Austin W. Gunter, the Brand Ambassador of WP Engine’s Managed WordPress Hosting Platform.
WordPress’s development has been intertwined with Apache since the beginning. WordPress was built to be served on Apache. Recently Nginx has risen to prominence to serve traffic from 11.4% of all known traffic.
Automattic moved WordPress.com over to Nginx, and managed WordPress hosts like WP Engine, and ZippyKid have adopted the technology as well. WordPress.com has publicly stated they have thousands of servers serving hundreds of thousands of requests per second. I spend most of my days talking about complicated server technology, but I hadn’t found a good resource to actually explain, in conversational terms, why Nginx is cool, why people are using it, and what the tradeoffs are between it and Apache. This post is intended to explain what Nginx is at a high level, not provide a “how to” for folks who may want to install it themselves.
First off, Nginx and Apache are not equal, but what makes them different? What makes Nginx suited to handle high performance web hosting tasks – such as serving an onslaught of concurrent requests when a post makes the front page of HackerNews or Reddit? What do you still need Apache for?
Nginx was developed, in part, to solve the C10k problem, or optimizing web server software to handle a large number of clients at the same time. C10k stands for concurrent ten thousand connections. When a blog post hits the front page of HackerNews or Reddit, thousands of visitors can arrive at your site at once. Nginx is event-based, and is fantastic at responding to concurrent requests.
Nginx is designed to handle thousands of concurrent connections with low CPU usage. It is a non-blocking event-driven web server, using a single process (or a small number of processes) to handle connections using evented I/O. Apache has several Multi-Processing Modules (“MPM”), but for most Linux systems, Apache’s default is the “prefork” MPM. Apache recommends using the “worker” MPM unless you’re using PHP – since PHP isn’t thread safe. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time. Nginx’s event-based” design allows it to serve data quickly, and with many concurrent connections, but we sacrifice Apache’s ability to process PHP
Additionally, Apache is limited by how many threads it has CPU to run, and Apache processes are costly. At 50-300MB per process, after the of concurrent processes, default is 256, Apache’s performance will decrease. Nginx, on the other hand, can handle 10,000+ concurrent connections using about 2MB of memory, never spawning new threads or processes. That’s absurdly scalable.
So how do we translate WordPress PHP with Nginx? Apache often bundles PHP with its processes, but to process PHP, Nginx needs to be paired with an external process like FastCGI, PHP-FPM, or HTTP. This can be done fairly easily. What’s more, by separating processes we can actually see more speed gains with Nginx. It will forward requests to process PHP externally, and send the response back to the browser.
Additionally, these can be configured to process different requests on various servers. This is a big deal for web host or sysadmin, because it means they can specify which requests go to which server, reducing request backlog. It also means that server and application processes can be restarted separately.
WordPress.com makes heavy use of Nginx’s ability to proxy_cache. You can program Nginx to send requests to the backend, but on the way back out have it hold the request on disk for “X seconds” so that the next requests can serve the page without having to go back to Apache for each new user.
3rd Party Modules
With Nginx gaining prominence, we’re seeing developers start to create cool modules we can compile into Nginx that add functionality we might need. As with any open-source project, these modules will likely be a key source of innovation on the web server.
Those are some huge advantages. But it’s important to note that Nginx isn’t an appropriate solution for shared hosting solutions, and it’s certainly not necessary for everyday WordPress blogs that don’t see high levels of traffic. Under minimal traffic, visitors may not notice any speed difference between Apache and Nginx. You want to serve WordPress off of Nginx if you expect to someday experience traffic spikes, and want to keep your site up and loading pages at sub 1.5seconds. Under load Nginx’s event-based processing, ability to serve statics quickly, and proxy_cache requests give it the ability to serve tens of thousands of connections on 2MB of CPU. That same traffic would cause Apache to commit harakiri.
Because Nginx is so scalable, Automattic, WordPress.com, and Managed WordPress hosts like WP Engine or ZippyKid have adopted the technology, usually along with Apache to serve PHP, and are using it to provide massively scalable hosting for tens of thousands of domains. Apache is still the place to process complex requests, but it cannot keep up with the scalability of Nginx.
What is your site running on? Have you considered installing Nginx to serve WordPress? Let me know in the comments what you’re using.