Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat(traffic): enable Traefik JSON access log when analytics on

Aditya Tripathie8e651bd7e86
diff --git a/bootstrap/helpers/proxy.php b/bootstrap/helpers/proxy.php
index 6997043937..ae6dbf2357 100644
--- a/bootstrap/helpers/proxy.php
+++ b/bootstrap/helpers/proxy.php
@@ -8,6 +8,24 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Yaml\Yaml;
+function traefikAccessLogCommands(bool $enabled): array
+{
+ if (! $enabled) {
+ return [];
+ }
+
+ return [
+ '--accesslog=true',
+ '--accesslog.filepath=/traefik/access.log',
+ '--accesslog.format=json',
+ '--accesslog.fields.headers.names.Cf-Connecting-Ip=keep',
+ '--accesslog.fields.headers.names.Cf-Ipcountry=keep',
+ '--accesslog.fields.headers.names.Cf-Cache-Status=keep',
+ '--accesslog.fields.headers.names.Cf-Verified-Bot=keep',
+ '--accesslog.fields.headers.names.Cf-Ray=keep',
+ ];
+}
+
/**
* Check if a network name is a Docker predefined system network.
* These networks cannot be created, modified, or managed by docker network commands.
@@ -330,6 +348,9 @@ function generateDefaultProxyConfiguration(Server $server, array $custom_command
} else {
$config['services']['traefik']['command'][] = '--api.insecure=false';
$config['services']['traefik']['volumes'][] = "{$proxy_path}:/traefik";
+ foreach (traefikAccessLogCommands($server->isTrafficAnalyticsEnabled()) as $cmd) {
+ $config['services']['traefik']['command'][] = $cmd;
+ }
}
if ($server->isSwarm()) {
data_forget($config, 'services.traefik.container_name');
diff --git a/tests/Feature/TrafficAnalytics/TraefikAccessLogTest.php b/tests/Feature/TrafficAnalytics/TraefikAccessLogTest.php
new file mode 100644
index 0000000000..1246c1af04
--- /dev/null
+++ b/tests/Feature/TrafficAnalytics/TraefikAccessLogTest.php
@@ -0,0 +1,17 @@
+<?php
+
+it('returns empty commands when traffic analytics is disabled', function () {
+ expect(traefikAccessLogCommands(false))->toBe([]);
+});
+
+it('returns JSON access log + Cloudflare header capture flags when enabled', function () {
+ $cmds = traefikAccessLogCommands(true);
+ expect($cmds)->toContain('--accesslog=true')
+ ->toContain('--accesslog.filepath=/traefik/access.log')
+ ->toContain('--accesslog.format=json')
+ ->toContain('--accesslog.fields.headers.names.Cf-Connecting-Ip=keep')
+ ->toContain('--accesslog.fields.headers.names.Cf-Ipcountry=keep')
+ ->toContain('--accesslog.fields.headers.names.Cf-Cache-Status=keep')
+ ->toContain('--accesslog.fields.headers.names.Cf-Verified-Bot=keep')
+ ->toContain('--accesslog.fields.headers.names.Cf-Ray=keep');
+});