Apache with nginx

You can improve the work of the web server which hosts customer websites (Apache) by using nginx, a supplementary high-performance web server which is typically used as a reverse proxy server. This web server was specifically designed for delivering large amounts of static content (such as images, video, css, xml, and so on). As opposed to Apache, nginx is much more efficient when it comes to handling a large number of concurrent connections. Another advantage of this web server compared with Apache is that nginx has a significantly smaller memory footprint per client connection.

To leverage all the benefits of nginx, Plesk configures it as a reverse proxy server that stands between the Internet and Apache (see the diagram below). This means that nginx becomes a frontend web server that processes all incoming requests from site visitors. The requests are sent to Apache which, in turn, distinguishes requests for static and dynamic content. If a request is for a static file (such as jpg, css, html, and so on), Apache passes the request through all registered handlers (applies .htaccess directory-level configuration, rewrites a URL, and so on) and returns to nginx a response which contains only the location of the requested file on the file system. nginx locates the file and sends it to the client. If the request is for a dynamic file (such as a PHP script), Apache executes the file and sends the response to nginx, which delivers it to the client.

Such a combination of nginx and Apache gives the following advantages:

The technical details on how Plesk processes HTTP requests with the help of nginx are provided next in this section. For information on how to turn on the support for nginx in Plesk, refer to the section Installing nginx. If you do not want to use nginx, make Apache your frontend web server following the instructions in the section Turning off nginx. If you want nginx to process all HTTP requests for web content, see Adjusting Apache Web Server Settings.

How Plesk with nginx Processes HTTP Requests

To seamlessly integrate nginx with Apache, Plesk uses two additional Apache modules:

Let us take a closer look at how Plesk processes requests for static and dynamic content with the help of these modules.

The sequence of processing an HTTP request for a static file is as follows (see the diagram):

  1. A client sends a request to a web server.
  2. nginx adds the X-Accel-Internal (used by mod_aclr2) and X-Forwarded-For (which contains the IP address of the client) headers to the request and sends the request to Apache.
  3. Apache receives the request and starts to process it by registered handlers (applies .htaccess configuration, rewrites URL, and so on). In this step, mod_rpaf replaces the IP address of the nginx server in the REMOTE_ADDR Apache variable with the client's address from the X-Forwarded-For header.
  4. After the request is processed by all registered handlers, it reaches mod_aclr2. The handler checks for the X-Accel-Internal header presence. If the header is present, the module sends to nginx a response with zero content length and the X-Accel-Redirect header. This header contains the exact location of the file as determined by mod_aclr2.
  5. Once nginx receives the response, it locates the file and delivers it to the client.

The diagram below is an example of how Plesk handles a request for a 2 KB GIF file.

static_seq

In the case of processing requests for dynamic content, the steps from 1 to 3 are the same. Then the request passes to the handler of the appropriate Apache module (mod_php, mod_perl, mod_cgi, and so on). The request never reaches mod_aclr2 (except for SSI requests). The handler generates a response and sends it to nginx, which, in turn, delivers the response to the client. The diagram below illustrates how Plesk processes a request for a PHP file.


Next in this section:

Installing nginx

Turning off nginx

 

Installing nginx

If you perform a clean installation of Plesk, nginx will be turned on by default. If you upgrade from earlier versions, you can add the nginx component at any time after the upgrade in Tools & Settings > Updates & Upgrades > Add Components. Once the component is added, start the Reverse Proxy Server (nginx) service in Tools & Settings > Services Management.

You can view the version of the installed nginx server in Tools & Settings > Server Components.

nginx version

  

 

Turning off nginx

To return to the configuration with a single Apache web server, stop the Reverse Proxy Server (nginx) service in Tools & Settings > Services Management.

nginx_stop

To make nginx the frontend web server again, start the Reverse Proxy Server (nginx) service.

Note: The start and stop operations for the Reverse Proxy Server (nginx) service do not only start and stop nginx, they actually switch the web server configuration (nginx and Apache combination or just Apache as a frontend web server). The restart operation works in the same way as for all other services: the nginx service is restarted.