CONTAO_ENV variable vs config file for enforcing production mode
0 reputation · 20 Dec 2021, 20:17 UTC
0 reputation · 20 Dec 2021, 20:17 UTC
A Contao installation needs consistent mode and path resolution between local development and production. The documented approaches for environment detection are CONTAO_ENV set in the server environment and values persisted in system/config/localconfig.php. Path handling also varies between absolute system/path definitions and relative resolution of var/cache against the document root.
With a subfolder install behind a reverse proxy, baseUrl in routing configuration must align with the proxy prefix, while cache and upload paths are resolved relative to the document root. The interaction of these settings is unclear when the same codebase is deployed across environments with different filesystem layouts and proxy configurations.
Does Contao prioritize CONTAO_ENV over a value stored in system/config/localconfig.php when both are present? When installed in a subfolder, is var/cache resolved relative to the document root or the Contao root, and which documented setting controls that resolution? If baseUrl is defined in routing configuration, does it override automatic base path detection behind a trusted reverse proxy?
35275 reputation · 21 Dec 2021, 05:09 UTC
system/config/localconfig.php is loaded later for Contao‑specific settings and does not influence the kernel environment.var/cache resolved for a sub‑folder install? It is resolved relative to the Contao project root (kernel.project_dir), i.e., the directory that contains vendor/ and var/. It is not relative to the web document root (public/). No documented setting makes it document‑root relative; the path is project‑relative by design.baseUrl in routing override automatic detection behind a trusted reverse proxy? Yes. Defining router.request_context.base_url (or contao.routing.base_url) overrides both Symfony’s automatic base‑path detection and any X-Forwarded-* headers, even when trusted proxies are configured.localconfig.php.var/cache uses kernel.project_dir as its base; the Contao core bundles (ContaoCoreBundle) configure paths via contao.paths in config/packages/contao.yaml.baseUrl in routing configuration overrides automatic detection and forwarded‑header detection.Because the kernel environment is decided before Contao‑specific configuration is loaded, relying on localconfig.php for mode enforcement is unreliable. The cache and upload directories are intentionally tied to the project root to keep them outside the web‑accessible public/ folder, which works the same whether the installation is in the document root or a subfolder. When a reverse proxy adds a prefix, setting baseUrl guarantees that generated URLs match the proxy’s public path, independent of forwarded‑header handling.
CONTAO_ENV=prod in the server environment (e.g., via .env, web server config, or container). Do not rely on localconfig.php for mode.config/packages/framework.yaml if the reverse proxy should forward X-Forwarded-* headers./contao), set router.request_context.base_url to that prefix in config/packages/routing.yaml (or via contao.routing.base_url).baseUrl: bin/console cache:clear --no-warmup followed by bin/console cache:warmup.To confirm whether the explicit baseUrl is actually needed, verify if trusted proxies are already configured and whether the X-Forwarded-Host or X-Forwarded-Prefix headers are being passed correctly. If they are, the baseUrl override may be redundant; if not, it is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.