Lifecycle of a Backend Request - HL Vanilla Community
<main> <article class="userContent"> <p>This document outlines the lifecycle of a backend a request on the Vanilla platform, from CDN to web server, to response.</p><h2 data-id="cdn-cloudflare">CDN / Cloudflare</h2><p>On Vanilla Cloud every requests request starts in Cloudflare. Here one of a few things may happen:</p><ul><li>Request may be blocked by rate-limiting rules.</li><li>A cached response may be found and returned immediately. <em>Notably Cloudflare's cache does not respect the </em><em><code class="code codeInline" spellcheck="false" tabindex="0">vary</code></em><em> </em>header, so the only differentiators for this cache are:<ul><li>Users with session cookies bypass this cache layer.</li><li>Different URLs have different caches.</li></ul></li><li>Cloudflare will pass the request along to Vanilla's load balancers.</li></ul><p>Cloudflare will also resolve SSL certificates for independent domains at this point.</p><p>At this point cloudflare will also append a <code class="code codeInline" spellcheck="false" tabindex="0">rayID</code> to the request which is used in logs to be able to trace a request/response through each layer.</p><h2 data-id="load-balancer-haproxy">Load Balancer / HAProxy</h2><p>Requests are then forwarded to HAProxy. Vanilla's HAProxy cluster will determine which site the request is for based on the host in the request. The correct cluster for the site will be determined and the request will be forwarded to an application server on that cluster.</p><h2 data-id="application-server-nginx">Application Server / Nginx</h2><p>At this layer one of 3 things may happen based on the path of the request.</p><ul><li>If the path is blocked for security reasons a 403 will be returned.</li><li>If the path points to a static asset in the <code class="code codeInline" spellcheck="false" tabindex="0">vanilla</code> application that static asset will be returned.</li><li>Otherwise the asset will be forwarded into a <code class="code codeInline" spellcheck="false" tabindex="0">php-fpm</code> process using FastCGI.</li></ul><h2 data-id="application-server-php-fpm">Application Server / PHP-FPM</h2><p>At this point Vanilla's PHP backend begins executing, starting from the <code class="code codeInline" spellcheck="false" tabindex="0">/index.php</code> file.</p><h3 data-id="entry-point-index.php">Entry point - <code class="code codeInline" spellcheck="false" tabindex="0">index.php</code></h3><p>Responsibilities:</p><ul><li>Configure error reporting.</li><li>Load environment (including composer autoloader).</li><li>Bootstrap the application.</li><li>Start the dispatcher and dispatch the current request.</li></ul><h3 data-id="bootstrapping-the-application">Bootstrapping the application</h3><p>Responsibilities:</p><ul><li>Create and apply core rules for the DIC (Dependency Injection Container).</li><li>Load the configuration.</li><li>If a site is not not configured redirect to the application setup.</li><li>Start addons.<ul><li>Make addon classes available in the autoloader.</li><li>Apply addon config defaults.</li><li>Apply addon DIC rules.</li><li>Bind addon event handlers.</li></ul></li><li>Start authenticators.<ul><li>This will construct a Session instance based on cookies for the current request.</li></ul></li><li>Apply shutdown functions.<ul><li>End of request logging.</li><li>Running any scheduled jobs. (after the response has been sent the client).</li></ul></li></ul><h3 data-id="running-the-dispatcher">Running the Dispatcher</h3><p>This is where the bulk of the actual application code runs.</p><p>Vanilla has 2 dispatchers. <code class="code codeInline" spellcheck="false" tabindex="0">Garden\Web\Dispatcher</code> (the new one) and <code class="code codeInline" spellcheck="false" tabindex="0">Gdn_Dispatcher</code> (the old one). The new one runs first and is responsible for:</p><ul><li>/api/v2 endpoints.</li><li>Newer react-based pages.</li></ul><p>If the new dispatcher doesn't match any route, then the request will be passed to the old dispatcher and it will attempt to route the request to a <code class="code codeInline" spellcheck="false" tabindex="0">Gdn_Controller</code>.</p><p>This can be some of the most dynamic control flow in Vanilla. After this figuring out where a request progress is just a matter of following method calls. To determine where a request is going, follow this documentation.</p><div class="js-embed embedResponsive" data-embedjson="{"body":"Overview Vanilla has 3 primary ways of dispatching URLs and they are tried in the following order. Routes registered with Garden\\Web\\Dispatcher. Pages using Gdn_Dispatcher. Redirects from Gdn_Router. Garden\\Web\\Dispatcher Routes There are primarily 2 types of routes dispatched from Garden\\Web\\Dispatcher.…","photoUrl":"https:\/\/us.v-cdn.net\/6030677\/uploads\/VWGAXAF5OFCO\/microsoftteams-image.png","url":"https:\/\/success.vanillaforums.com\/kb\/articles\/161-dispatching-urls","embedType":"link","name":"Dispatching & URLs - Vanilla Success"}"> <a href="https://success.vanillaforums.com/kb/articles/161-dispatching-urls" rel="nofollow noreferrer ugc"> https://success.vanillaforums.com/kb/articles/161-dispatching-urls </a> </div><p><br></p> </article> </main>