Apache & Nginx. Linked in a chain

How the Apache & Nginx Combination Works in Timeweb

For many companies, Nginx + Apache + PHP is a very typical and widely used combination, and Timeweb is no exception. However, understanding exactly how it is implemented can be interesting and useful.

Apache & Nginx. Linked in a chain

Using such a combination is certainly dictated by the needs of our clients. Both Nginx and Apache play special roles, each solving specific tasks.

Main Settings Apache are performed in the configuration files of Apache itself, while settings for client sites occur through the .htaccess file..htaccess is a configuration file where the client can independently set rules and behaviors for the web server. Such settings will specifically relate to their site. For example, thanks to Apache's functionality, users can change the operating mode within one version of PHP from mod_php to mod_cgi; redirects, SEO optimization, user-friendly URLs, and certain PHP limits can be configured.

Nginx is used as a proxy server to redirect traffic to Apache and as a web server to serve static content. We have also developed security modules for Nginx that allow us to protect our users' data, for instance, by segmenting access rights.

Let's imagine that a user visits our client's website. First, the user hits Nginx, which serves static content. This happens instantly. Then, when it comes to loading PHP, Nginx redirects the request to Apache. Apache, in conjunction with PHP, then generates dynamic content.

Features of the Apache & Nginx Combination in Timeweb

Our virtual hosting implements 2 main operating schemes for Apache & Nginx: Shared and Dedicated..

Shared Scheme

This scheme is used for most users. It is characterized by simplicity and resource efficiency: the Shared scheme uses fewer resources, which makes its tariff cheaper. According to this scheme, one Nginx is running on the server, which can handle all user requests, along with several instances of Apache.

The Shared scheme has been improved over a long period: gradually, we fixed deficiencies. It is convenient that it can be done without the need to modify the source code.

Apache & Nginx. Linked in a chain
the Shared scheme.

Dedicated Scheme.

Dedicated requires more resources, which is why its pricing is higher for clients. In the Dedicated scheme, a separate Apache instance is launched for each client. Resources are reserved exclusively for the client. Here's how it works: the server hosts multiple PHP versions. We support versions 5.3, 5.4, 5.6, 7.1, 7.2, 7.3, 7.4. Thus, a separate Apache is started for each PHP version.

Apache & Nginx. Linked in a chain
Dedicated scheme

Safe zone. Configuring zones in Nginx

Previously, we used many shared memory zones (zone) for Nginx—one server block for each domain. This configuration requires significant resources, as a separate zone is created for each website. However, in Nginx settings, most websites are similar, allowing them to fit into a single zone by using the map directive in the module ngx_http_map_module, which allows for mappings to be defined. For example, we have a zone template where we must provide variables: the path to the site, the PHP version, and the user. This has sped up the reading of Nginx configuration, meaning a reload is faster.

This kind of configuration has significantly saved memory resources and improved Nginx performance.

Reload failed!

In the Shared scheme, we eliminated the need to reload Apache when making changes to site settings. Previously, when a client wanted to add a domain or change the PHP version, a mandatory Apache reload was required, leading to delays in responses and negatively impacting site performance.

We eliminated reloads by creating dynamic configurations. Thanks to mpm-itk (the Apache module), each process runs under a separate user, enhancing security. This method allows passing user and document_root data from Nginx to Apache2. Thus, Apache does not contain site configurations within itself; it receives them dynamically, and reloads are no longer necessary.

Apache & Nginx. Linked in a chain
Configuration of the Shared scheme

What about Docker?

Many companies have transitioned to container-based systems. Timeweb is currently considering such a transition. Certainly, every solution has its pros and cons.

Along with undeniable advantages, a container system provides the user with fewer resources. At Timeweb, thanks to the described hosting operation scheme, there is no limit on RAM for the user. They receive more resources than in a container. Furthermore, the user can have more Apache modules loaded.

Timeweb supports around 500,000 websites. We bear great responsibility and do not introduce immediate, unwarranted changes to the complex architecture. The combination of Apache & Nginx is reliable and time-tested. We, in turn, strive to achieve maximum performance through unique configurations.

To ensure the quality and speed of operation for a large number of websites, it is necessary to use a template and dynamic configuration of Apache and Nginx. This allows for simple and quick administration of a large number of similar servers.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster