Apache with nginx

You can improve the work of the web server which hosts customer websites by installing 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 maximum number of concurrent connections to a website increases.
  • The consumption of server CPU and memory resources decreases.
    The maximum effect will be achieved for websites with a large amount of static content (such as photo galleries, video streaming sites, and so on).
  • The efficiency of serving visitors who have a slow connection speed (GPRS, EDGE, 3G, and so on) improves.
    For example, a client with a 10 KB/s connection requests a PHP script, which generates a 100 KB response. If there is no nginx on the server, the response is delivered by Apache. During the 10 seconds required to deliver the response, Apache and PHP continue to consume full system resources for this open connection. If nginx is installed, Apache forwards the response to nginx (the nginx-to-Apache connection is very fast as both of them are located on the same server) and releases system resources. As nginx has a smaller memory footprint, the overall load on the system decreases. If you have a large number of such slow connections, use of nginx will significantly improve website performance.

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.

How Plesk with nginx Processes HTTP Requests

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

  • mod_aclr2.
    This module sets up a handler which runs after handlers of all other Apache modules (mod_rewrite, .htaccess related modules, mod_php, and so on). Therefore, if the request is for dynamic content, mod_aclr2 will never get it as the request will be served by upper-level handlers of certain Apache modules (mod_php, mod_perl, mod_cgi, and so on). The only exceptions are SSI requests: once they reach mod_aclr2, it redirects them to proper handlers. If the request is for a static file, mod_aclr2 searches for the exact file location on the file system and sends the location to nginx.
  • mod_rpaf.
    From the point of view of Apache, all of its clients have the same IP address - the address of the nginx server (see the diagram above). This causes problems for websites and web apps that use client IP addresses for authentication, statistic purposes, and so on. mod_rpaf solves the problem by replacing the IP address of the nginx server in all requests with client IP addresses. In more detail, the module uses the special X-Forwarded-For header in which nginx puts the IP address of a client.

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, you should run 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.

 

Leave your comments on this page

Leave your feedback or question on this documentation topic below. For technical assistance, contact your hosting service provider or submit a request to Plesk support. Suggest new features for Plesk here. Discuss general questions on the Plesk forum. All offtopic comments will be removed.