This article explains how Varnish, Apache and NginX fit together and/or differ. Varnish is an excellent cache and speeds up web-sites significantly. Terminate the HTTP connection at Varnish on port 80 and point Varnish internally to an NginX server listening for HTTP on port 81 (they can’t both be on port 80). The NginX server terminates the HTTPS connection on port 443. With this configuration you can have high-speed and secure content from a single server combining the great features of both Varnish and NginX. Don’t think we’ve forgotten Apache. Apache and NginX essentially serve the same purpose. Read on to find out how this all works.
Varnish is a caching server that works with HTTP only. That means no HTTPS/SSL. If your site requires secure connections, you must use Apache or NginX (or some other web server that supports HTTPS) to terminate the HTTPS connections. Varnish is a cache only and does not serve any content from disks – only from up-stream web servers.
Apache does both HTTP and HTTPS connections. It’s easy to get going with for sites requiring PHP because it comes with PHP loaded in as a module. The down-side is that php runs better in large sites when run as a seperate process. Apache “can” use php-fpm though. Apache supports the use of “.htaccess” files which allow developers to make (some) server behavior changes without altering the web server’s configuration.
NginX also does both HTTP and HTTPS connections. It’s easy to get going with sites requiring static content but if you require PHP features, you’ll need to install and configure php-fpm and point NginX to it. This isn’t hard, it’s just another step and php-fpm is great for large sites. NginX doesn’t support “.htaccess” files so all server behavior changes are made by the system administrator via restricted configuration files.
PHP-FPM needs a little explanation. Web servers such as Apache and NginX don’t know about PHP. We have to configure the web server to use a php interpreter. We have two options: a) mod_php and php-fpm. Mod_php is the php module that comes with Apache. It’s easy to get going and requires almost no configuration. But it performs less well in high-demand situations. The second option is to use php-fpm which is server software that listens on a network port for connections from web servers. php-fpm can scale out easily to accommodate high demand. With php-fom, the web server needs to be told to use the network to get to the php interpreter.