Kokil Thapa - Professional Web Developer in Nepal
Freelancer Web Developer in Nepal with 15+ Years of Experience

Kokil Thapa is an experienced full-stack web developer focused on building fast, secure, and scalable web applications. He helps businesses and individuals create SEO-friendly, user-focused digital platforms designed for long-term growth.

Traefik as an API Gateway

By Kokil Thapa | Last reviewed: August 2026

Running multiple backend services behind a single entry point is standard practice, but configuring the routing layer often becomes a bottleneck. Using Traefik as an API Gateway solves this by automatically discovering containers and applying middleware without manual config reloads. If you are building Laravel APIs or microservices in Docker, Traefik eliminates the friction of maintaining static Nginx configs while providing native support for modern cloud-native patterns.

How does Traefik as an API Gateway differ from Nginx?

The fundamental difference lies in configuration philosophy. Nginx is a static reverse proxy that requires explicit configuration files and reload signals when upstreams change. Traefik is a dynamic edge router designed for ephemeral infrastructure. When I deploy Laravel applications using Deployer and GitLab CI on shared EC2 instances, the ability to spin up a new container and have it immediately routable—without touching a central config file—is a massive operational advantage.

Static Nginx ProxyManual Config EditReload / Restart SignalDowntime Risk During ReloadTraefik as an API GatewayContainer Starts with LabelsAuto-Discovery via Docker APIInstant Zero-Downtime Routing
Static Nginx requires manual intervention and reloads, while Traefik as an API Gateway dynamically routes traffic based on container metadata.

In practice, this means your routing logic lives next to your application code in docker-compose.yml rather than in a separate infrastructure repository. For agencies managing multiple client sites like legal-tech portals or eCommerce platforms on shared infrastructure, this reduces the cognitive load significantly. You don't need to remember which port maps to which domain; the label defines it.

FeatureNginx (Traditional)Traefik as an API Gateway
ConfigurationStatic files (nginx.conf)Dynamic labels + optional file providers
Service DiscoveryManual upstream definitionAutomatic via Docker/K8s/Consul API
SSL/TLS ManagementCertbot + cron + reloadNative Let's Encrypt with auto-renewal
Middleware ChainLua/OpenResty or external modulesBuilt-in plugins (Auth, RateLimit, Headers)
Reload BehaviorRequires nginx -s reloadHot-reload without dropping connections
ObservabilityAccess logs only (unless enhanced)Real-time dashboard + metrics export

How do you configure Traefik for Laravel Docker containers?

Setting up Traefik for a Laravel application running in PHP-FPM 8.4 requires exposing the correct internal port and defining host-based routing. On production systems I maintain, I typically run Traefik v3.x alongside the application stack. The key is ensuring the Docker provider is enabled and the network is correctly shared.

Base Docker Compose Configuration

This configuration assumes you have a traefik-public network created externally. This separation prevents backend services from being accidentally exposed to the public internet without passing through the gateway.

<!-- docker-compose.yml -->
services:
  traefik:
    image: traefik:v3.1
    command:
      - "--api.dashboard=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.le.acme.httpchallenge.entrypoint=web"
      - "--certificatesresolvers.le.acme.email=admin@example.com"
      - "--certificatesresolvers.le.acme.storage=/letsencrypt/acme.json"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./letsencrypt:/letsencrypt"
    networks:
      - traefik-public

  laravel-app:
    image: my-laravel-app:php8.4-fpm
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.laravel.rule=Host(`api.example.com`)"
      - "traefik.http.routers.laravel.entrypoints=websecure"
      - "traefik.http.routers.laravel.tls.certresolver=le"
      - "traefik.http.services.laravel.loadbalancer.server.port=8080"
      - "traefik.docker.network=traefik-public"
    networks:
      - traefik-public
      - default

networks:
  traefik-public:
    external: true

A common mistake here is forgetting traefik.docker.network. If your container is attached to multiple networks (which is typical for Laravel apps connecting to MySQL/Redis), Traefik might pick the wrong IP address, resulting in 502 Bad Gateway errors. Explicitly naming the network ensures traffic flows through the intended interface.

Handling Non-Standard Ports

Laravel Octane or FrankenPHP setups often listen on port 8000 or 8080 instead of the default 80. Always specify traefik.http.services.<name>.loadbalancer.server.port explicitly. Relying on auto-detection works for standard Nginx/Apache containers but frequently fails for custom PHP runtimes.

What middleware should every API gateway implement?

Routing is only half the job. A competent API gateway must enforce security and reliability policies at the edge. When architecting REST APIs in Laravel, I offload cross-cutting concerns to Traefik so the application code remains focused on business logic.

Client RequestRate Limit(Edge Layer)Auth / JWT(ForwardAuth)Security Headers(HSTS, CSP)Laravel App
Middleware chain in Traefik as an API Gateway processing requests before they reach the application backend.

Rate Limiting at the Edge

Protecting your Laravel API from abuse shouldn't wait until the request hits PHP. Define a global rate limit middleware to absorb spikes before they consume expensive application resources.

labels:
  - "traefik.http.middlewares.api-ratelimit.ratelimit.average=100"
  - "traefik.http.middlewares.api-ratelimit.ratelimit.burst=50"
  - "traefik.http.middlewares.api-ratelimit.ratelimit.period=1m"
  - "traefik.http.routers.laravel.middlewares=api-ratelimit@docker"

This configuration allows 100 requests per minute on average with bursts up to 50. For multi-tenant SaaS platforms where different clients have different quotas, consider using the rateLimit.sourceCriterion.ipStrategy or custom headers to differentiate users before they reach your application's throttle middleware.

Authentication Offloading

For internal dashboards or staging environments, Basic Auth at the gateway level prevents unauthorized access entirely. For production APIs requiring JWT validation, Traefik's ForwardAuth middleware delegates verification to a lightweight auth service, keeping your main Laravel app free from repetitive token parsing on health checks or public endpoints.

- "traefik.http.middlewares.internal-auth.basicauth.users=admin:$$apr1$$xyz..."
- "traefik.http.routers.dashboard.middlewares=internal-auth@docker"

Note the double dollar signs ($$) in Docker Compose files. This escapes the variable interpolation syntax. Missing this escaping is one of the most frequent issues I see when developers first adopt Traefik for securing admin panels.

How do you handle SSL and canonical redirects in production?

Every production API must enforce HTTPS and normalize URLs. Traefik handles this natively through entrypoint redirections and certificate resolvers, eliminating the need for separate Certbot containers or cron jobs.

Global HTTP to HTTPS Redirect

Configure this once at the entrypoint level rather than per-router. This ensures every service inherits secure-by-default behavior.

command:
  - "--entrypoints.web.http.redirections.entryPoint.to=websecure"
  - "--entrypoints.web.http.redirections.entryPoint.scheme=https"
  - "--entrypoints.web.http.redirections.entryPoint.permanent=true"

Canonical Domain Enforcement

SEO and cookie consistency require enforcing a single canonical domain. If your legal-tech portal serves both www.notarynepal.com and notarynepal.com, redirect one to the other at the gateway. This prevents duplicate content issues and ensures session cookies work reliably across all entry points.

labels:
  - "traefik.http.routers.www-redirect.rule=Host(`www.notarynepal.com`)"
  - "traefik.http.routers.www-redirect.middlewares=strip-www"
  - "traefik.http.middlewares.strip-www.redirectregex.regex=^https://www\\.(.+)"
  - "traefik.http.middlewares.strip-www.redirectregex.replacement=https://$1"
  - "traefik.http.middlewares.strip-www.redirectregex.permanent=true"

When working on technical SEO audits for Nepali businesses, I frequently find missing canonical redirects causing indexation bloat. Fixing this at the Traefik level is far more reliable than relying on framework-level redirects, which may not trigger if caching layers intercept the request first.

When should you choose Traefik over other gateway solutions?

Traefik isn't universally superior—it's specifically optimized for dynamic, containerized environments. Understanding its boundaries prevents architectural mismatches.

Infrastructure Type?Static VPS / Bare MetalDocker / SwarmKubernetes ClusterUse Nginx / CaddySimpler config, lower overheadUse TraefikAuto-discovery, zero reloadsTraefik OR Ingress-NginxBoth valid; Traefik = simpler CRDsKey Decision FactorIf services change weekly → Traefik winsIf infra is stable for months → Nginx suffices
Decision framework for choosing Traefik as an API Gateway based on deployment environment dynamism.

Choose Traefik when:

  • You run Docker Compose, Swarm, or Kubernetes with frequently changing services
  • You want automatic Let's Encrypt management without external tooling
  • Your team prefers declarative configuration co-located with application code
  • You need built-in observability dashboards without additional monitoring stacks

Stick with Nginx/Caddy when:

  • You manage static VPS deployments with infrequent changes
  • You require advanced caching or complex rewrite rules better served by mature modules
  • Your team has deep existing Nginx expertise and minimal containerization
  • Performance benchmarks show measurable differences at your specific scale (rare below 10k RPS)

For most Laravel shops and agencies I work with in Nepal and globally, the operational savings from auto-discovery outweigh the marginal performance benefits of static proxies. The time saved debugging routing issues during deployments pays for itself within weeks.

Practical Production Considerations for Traefik

Beyond basic setup, several nuances determine whether Traefik succeeds in production. These lessons come from maintaining live systems handling real transactions, not toy examples.

Persistent Storage for Certificates

Always mount /letsencrypt/acme.json to persistent storage. Losing this file means re-issuing certificates on every restart, which quickly triggers Let's Encrypt rate limits. On EC2 instances, use EBS volumes or bind mounts to named Docker volumes—never ephemeral container storage.

Dashboard Security

The Traefik dashboard exposes sensitive routing information. Never expose it publicly without authentication. Either restrict access by IP using middleware or place it behind a VPN. In my deployments, the dashboard is typically accessible only via SSH tunnel or internal network, never on a public subdomain.

Log Aggregation Integration

Traefik outputs structured JSON access logs when configured properly. Forward these to your logging stack (Loki, ELK, or CloudWatch) for centralized analysis. Correlating gateway logs with Laravel application logs is essential for debugging latency issues that span the entire request lifecycle.

command:
  - "--accesslog=true"
  - "--accesslog.format=json"
  - "--accesslog.fields.headers.names.X-Request-ID=keep"
  - "--log.level=WARN"

Health Checks for Resilience

Define health checks so Traefik stops routing to failing containers automatically. This is critical for zero-downtime deployments where old and new containers coexist briefly during transitions.

labels:
  - "traefik.http.services.laravel.loadbalancer.healthcheck.path=/up"
  - "traefik.http.services.laravel.loadbalancer.healthcheck.interval=10s"
  - "traefik.http.services.laravel.loadbalancer.healthcheck.timeout=3s"

Laravel's built-in /up endpoint (available since Laravel 11) works perfectly here. It returns 200 only when the application is fully booted and maintenance mode is disabled, preventing traffic from hitting partially initialized containers.

Moving Forward with Traefik as an API Gateway

Adopting Traefik as an API Gateway shifts routing complexity from static configuration files to dynamic, code-proximate declarations. For teams shipping Laravel APIs, microservices, or containerized eCommerce platforms, this alignment between application and infrastructure reduces deployment friction and operational overhead. Start with the base Docker Compose setup above, add middleware incrementally, and monitor the dashboard during initial rollout to verify routing behavior matches expectations.

If you're evaluating gateway options for a production system or need help migrating from static Nginx configs to a dynamic Traefik setup, reach out to discuss your architecture. I regularly help teams in Nepal and worldwide modernize their deployment pipelines while maintaining reliability and security standards.

Frequently Asked Questions

Traefik is a cloud-native reverse proxy that routes HTTP and TCP traffic to backend services using automatic service discovery. As an API gateway, it handles routing, load balancing, SSL termination, and middleware execution without manual configuration files for every route change.

Traefik auto-discovers Docker, Kubernetes, and Consul services natively, while Nginx requires manual config reloads or third-party tools like nginx-plus. In my experience deploying Laravel microservices on Ubuntu servers, Traefik reduces operational overhead significantly for containerized APIs compared to maintaining static Nginx upstream blocks.

Yes, Traefik Community Edition is open-source and free for production. The Enterprise edition costs approximately USD 500 per node annually (Rs 67,000) and adds features like distributed rate limiting and advanced observability. Most Nepal-based projects I have worked on run perfectly fine on the free community version.

Yes, via the ForwardAuth middleware or dedicated JWT plugins. Traefik delegates token validation to an external auth service or uses built-in plugin logic before routing requests to your Laravel or Symfony backend. This keeps API gateway configuration decoupled from business-specific authentication rules stored in your application database.

Yes, Traefik natively proxies WebSocket and Server-Sent Events by detecting the Upgrade header automatically. No special configuration is needed beyond standard entrypoint definitions. I have used this successfully for Livewire-powered booking systems where real-time status updates are critical alongside REST endpoints.

Use the RateLimit middleware defined in dynamic configuration or Docker labels. Set average, burst, and period values per route. For distributed setups across multiple Traefik instances, you need Redis-backed storage or the Enterprise edition. On single-server Laravel deployments, local memory rate limiting works reliably for moderate traffic volumes.

Yes, Traefik has built-in ACME support that obtains and renews certificates automatically. Configure the certificatesResolvers section in static config with your email and storage path. This eliminates certbot cron jobs entirely. I use this on every client project hosted on Ubuntu VPS to ensure HTTPS without manual intervention.

Yes, using Matchers like PathPrefix, Headers, or Query parameters in router rules. Define separate routers for v1 and v2 API versions pointing to different backend services. This enables clean API versioning without modifying application code. Middleware chains can then apply version-specific transformations or authentication requirements.

Configure healthCheck in the service definition with a path, interval, and timeout. Traefik stops routing traffic to failing instances automatically. For Laravel APIs, point this to a lightweight /health endpoint that verifies database connectivity. Unhealthy containers in Docker Swarm or Kubernetes are also removed from rotation automatically.

Traefik outputs structured access logs in JSON or CLF format and exposes Prometheus metrics at /metrics. Integrate with Grafana, Datadog, or Loki for dashboards. Enable debug logging temporarily for troubleshooting routing issues. In production, I always configure access log rotation and ship metrics to avoid disk exhaustion on smaller EC2 instances.

Yes, Traefik supports TCP and UDP routers alongside HTTP. gRPC works over HTTP/2 with proper TLS configuration. Raw TCP routing enables proxying database connections or SSH tunnels. However, for complex message queue protocols, a dedicated broker is usually better than forcing everything through an API gateway layer.

Run Traefik on a new port first, replicate existing VirtualHost rules as Traefik routers, and test thoroughly before switching DNS or ports. Keep Apache running as fallback during transition. Validate SSL certificates and header forwarding behavior match exactly. I follow this approach when modernizing legacy PHP infrastructure to avoid downtime.

Yes, but configure CORS middleware and cookie forwarding correctly in Traefik. Ensure SameSite and Secure attributes pass through to the browser. Sanctum relies on session cookies, so the gateway must preserve them without stripping headers. Test cross-origin requests explicitly after deployment to avoid subtle authentication failures in production SPAs.

Missing trailing slashes in PathPrefix matchers, incorrect entrypoint references, and forgotten middleware attachments cause most issues. Also verify that Docker provider watches the correct network if using container labels. Always check the Traefik dashboard at /dashboard to visualize active routers and services before assuming backend faults.

Choose Kong for enterprise plugin ecosystems and multi-database backends, or Envoy for extreme performance and xDS service mesh integration. Traefik wins for simplicity, native container integration, and low operational overhead. For most Nepal-based SMEs and legal-tech platforms I build, Traefik provides sufficient functionality without the complexity tax of heavier alternatives.

Share this article

Quick Contact Options
Choose how you want to connect me: