You are here

Apache or Nginx, which makes your web site faster?

Submitted by Peter on Sun, 2012-07-22 11:30

Nginx uses less resources than Apache, some of the time, and, for most people, is more work to set up because it is not the default in their server. Is Nginx worth the extra work?

Are they equivalent

Nginx is a simple Web server plus a proxy server. Apache is a Web server and is used with a separate proxy server. If comparisons of Nginx and Apache do not use a proxy server with Apache, Nginx will always win for some types of requests.

Apache has more configuration options than Nginx and will be the only choice for some requirements. The Nginx Web server configuration options are closer to Lighttpd, a Web server that was popular before Nginx.

Popularity

Apache is the world's most popular Web server from 1996 onwards. Nginx usage peaked a couple of years ago and current usage is consistent with people not switching to Nginx unless they have a specific problem Nginx may solve.

Performance tests

There are plenty of performance tests with fancy charts showing Nginx performing better than Apache. Most show Nginx performing better only under medium load for generic web pages, not highly specialised pages. What happens outside that range?

For very low loads, the main overheads are those related to starting processes, opening databases, and finding files. The overheads are about the same if Apache is configured to do only what Nginx does. The overheads for Apache will be higher if you install lots of software options not used by your Web page. Apache will have to load most of that extra software and pass your request through most of the extra software despite your Web page not needing the extra processing.

For very large loads, your requests will hit bottlenecks and roadblocks. On Windows you tend to get gradual slowdown as you run out of memory, a result best described as a bottleneck. On Unix and Linux you get results more like a car crash on a freeway. Linux and Unix will perform better at high loads then suddenly stop when they go into a paging meltdown. You get the same type of results with databases and other resources. Nginx should perform better because it should leave more memory free for the other resources but the fact is, many of the other resources will not use the extra memory unless configured to use the extra memory.

If you configure all the software on your Web server for Apache then switch to Nginx, you may end up with free memory unused. If you configure all the software on your Web server for Nginx then switch to Apache, you may end up with severe memory shortages that would not occur if you tuned the settings for Apache.

Medium loads show the differences between web servers. There is enough load to show a difference and not enough load to trigger a crash into something that may be unrelated to the Web server software. medium loads show Nginx performs better for some types of page requests and does nothing for other page requests because the speed of the other page requests is dependent on things outside the Web server.

The proxy server effect

When you request a Web page, the software attached to the Web server performs a lot of work reading configuration files, assembling data from databases, and all sorts of activity logging. The Web page might be exactly the same as it was on the previous request. Regenerating static content is a waste of resources. You can configure a Web page to tell the Web server, proxy servers, and your Web browser to save a copy of the page for later use. The same settings apply to all the images, CSS files, and Javascript files.

Proxy servers save copies of everything for later use. Your Internet service provider might place a proxy server in front of your Web site. ISPs often place big proxy servers between incoming traffic and the data they deliver back to Web browsers because that reuse of data reduces their data transmission costs.

If your Web pages change only once per day, you might set the cache timeout for an hour. The proxy server will supply the copied data for every page request during the hour then request a new copy. When you update a page tomorrow, your visitors will see the old page for up to an hour.

Nginx includes a proxy server function and can reduce overheads by delivering cached pages instead of fresh pages. If you compare Nginx, with the proxy server, to Apache without a proxy server, Nginx will be faster than Apache for delivery of those cached pages.

Not all pages can be cached. If you use Drupal, pages delivered to anonymous users are cached and pages delivered to logged in users are not cached. Nginx will not beat Apache for delivery of pages to users who are logged in. Nginx might perform slightly faster than Apache for logged in users if you have all the Apache options switched on but most of the workload will be in the database and other areas, making the Web server overhead small by comparison and the difference between the two Web servers almost invisible.

Features

Apache has more features. Many of them are irrelevant for common Web sites. Of the important features, some are missing from Nginx.

Apache Nginx
Flexible No
Processes No
Threads Threads
PHP module  No
Fast PHP Fast PHP
.htaccess No

Flexible

Apache runs Solr search and a dozen other things you might need for some sites. If you do not need them, you can use Nginx. If you need those features and your site will be spread over several physical servers, you can configure your Web site on one server using Nginx and place the other services on another server using Apache. For a mixed configuration across several servers, the speed for a specific page will be based on the total time required for all the services used on that page. Solr search can be really slow compared to a normal page. The same can happen with some of the other services you might install under Apache. Apache gives you the choice.

Threads or processes?

Threads are faster than processes. When a thread crashes, every other thread in the same process crashes. Therefore threads are fast but unreliable. Use threads when available and your code is fully debugged. Use processes for every other situation. Apache gives you the choice.

Nginx call their thread approach event driven. Some descriptions of Nginx processing suggest the internal processing might be a little different to what is commonly called threading, more like monolithic. Other descriptions suggest it is threaded with a severe limit on the number of threads.

PHP module or Fast PHP

The fastest way to run PHP in Apache is using the PHP module. The PHP module does not work in any other Web server. Apache gives you the choice.

Fast PHP and several other options let you run PHP separate from the Web server. There are lots of reasons for choosing each option when you cannot use PHP module. There are lots of configuration issues and problems with each option. Fast PHP is the option recommended for Nginx.

You will have to perform more work to get Fast PHP working, compared to the default PHP in the default Apache configuration. You might hit problems and revert back to PHP module under Apache. Expect to spend a day or two or more making Fast PHP work for your Web site and do not expect it to be significantly faster because the best parts of Fast PHP only work for some types of page requests.

.htaccess

The Apache .htaccess file is a configuration file you can place in any directory of your Web site to change the way Apache works for pages in that directory. you use the feature when you host several different web sites in the one Apache. You might also use it when you have several different applications in the one Web site.

The feature is entirely optional and is switched off by default. Many hosting companies switch the feature on to handle multiple web sites. When the feature is switched on, Apache uses extra resources to look for a .htaccess file in every directory in the path to a web page. user /a/b/c/d.html as an example page request. Apache has to look for .htaccess in /a/ then /a/b/ then /a/b/c/ before processing /a/b/c/d.html.

Nginx does not offer that feature and does not have that overhead. The overhead is optional in Apache. Most comparisons do not use .htaccess because the Nginx pages would not work of .htaccess was required. Some of the comparisons have Apache .htaccess processing switched on because that is the configuration copied from their standard web servers.

When should you look at Nginx?

If you have only one Web site, Nginx is a possibility.

If you have a dedicated experienced Web server administrator with several days spare and a spare server, Nginx is a possibility.

If your Web server is approaching full, Nginx might reduce the load. You can also test Nginx as a proxy server in front of your current Apache Web site.

If your Web site does not make heavy use of a database, tuning Apache, or replacing Apache with Nginx, will make a difference. For heavy database use, focus on the database first because that will deliver the biggest benefit. Drupal 7 is an example of an application where you put database tuning ahead of changing your Web server software.

Conclusion

Your web site might benefit from Nginx. I do not use Nginx because I have many sites with different configurations.