Apache & Nginx. Linked by the same chain

How the Apache & Nginx Bundle is Implemented in Timeweb



For many companies, Nginx + Apache + PHP is a very typical and widespread bundle, and Timeweb is no exception here. However, figuring out exactly how it is implemented can be interesting and rewarding.



image



The use of such a bundle is, of course, dictated by the needs of our clients. Both Nginx and Apache play a special role, each solving a specific problem.



The main Apache settings are done in the configuration files of Apache itself, and the settings for client sites are done through the .htaccess file... .htaccess is a configuration file where the client can independently customize the rules and behavior of the web server. This setting will be specific to his site. For example, thanks to the Apache functionality, users can change the mode of operation within the same PHP version from mod_php to mod_cgi; you can customize redirects, SEO optimization, convenient URL, some limits for PHP.



Nginx is used as a proxy to redirect traffic to Apache and as a web server to serve static content. Also for Nginx, we have developed security modules that allow us to protect the data of our users, for example, to separate access rights.



Let's imagine that some user visits our client's site. First, the user goes to Nginx, which serves up static content. It happens instantly. Then, when it comes to loading PHP, Nginx redirects the request to Apache. And Apache, together with PHP, already generates dynamic content.



Features of the Apache & Nginx bundle in Timeweb



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



Shared



scheme This scheme is used for most users. It is distinguished by simplicity and resource intensity: the Shared scheme uses fewer resources, therefore its tariff is also cheaper. According to this scheme, one Nginx is running on the server, which allows serving all user requests, and several Apache instances.



The Shared scheme has been improving for a long time: gradually we fixed the shortcomings. Conveniently, it can be done without having to modify the source code.





Shared



schema Dedicated schema



Dedicated requires more resources, so its tariff is more expensive for customers. In the Dedicated scheme, a separate Apache is raised for each client. The resources here are reserved for the client, they are allocated exclusively. How it works: There are several PHP versions on the server. We support versions 5.3, 5.4, 5.6, 7.1, 7.2, 7.3, 7.4. So, for each PHP version, its own Apache is started.





Dedicated scheme



Safe zone. Configuring Zones in Nginx



Previously, we used many shared memory zones (zones) for Nginx - one server block per domain. This configuration requires a lot of resources, since a separate zone is created for each site. However, in the Nginx settings, most sites are of the same type, so they can be placed in one zone thanks to the use of map directives in the ngx_http_map_module , which allow you to set mappings. For example, we have a zone template to which we must supply variables: path to the site, PHP version, user. Thus, re-reading the Nginx configuration, that is, reload, has been speeded up.



This configuration greatly saved RAM resources and made Nginx work faster.



Reload will not work!



In the Shared scheme, we got rid of the need to reload (reload) Apache when changing the site settings. Previously, when one client wanted to add a domain or change the PHP version, a mandatory Apache reload was required, which led to delays in responses and negatively affected site performance.



We got rid of reloads by creating dynamic configurations. Thanks to mpm-itk (Apache module), each process runs from a separate user, which increases the level of security. This method allows you to transfer data about the user and his document_root from Nginx to Apache2. Thus, Apache does not contain site configurations, it receives them dynamically, and reloads are no longer required.





Shared schema configuration



What about Docker?



Many companies have moved to a container-based system. Timeweb is currently considering such a transition. Of course, there are pros and cons in every solution.



Along with the undeniable advantages, the container system provides the user with fewer resources. In Timeweb, thanks to the described hosting operation scheme, the user has no limitation in RAM. It receives more resources than the container. In addition, the user can have more Apache modules loaded.



Timeweb powers about 500,000 websites. We take great responsibility and do not make instant, unnecessary changes to a complex architecture. Apache & Nginx bundle is reliable and time-tested. We, in turn, strive to achieve maximum performance through unique configurations.



For high-quality and fast work of a large number of sites, you need to use the template and dynamic configuration of Apache and Nginx. It allows you to easily and quickly administer a large number of similar servers.



All Articles