Hi there!
I use traefic for local development and use redirect middleware. Also I have apps on two ports, for mobile and desktop versions. I want to detect device on traefik level and use one or another service. Middleware for user-agent (traefikuseragent) works fine for code below, but it set header on the front side and i want to add header with device type on backend side (docker) and on the frontend side (traefic dynamic) route user on right service.
I tried to use traefikuseragent on docker-side, but when i set it to nginx-web-secured-home
route - it just ignored. Middleware works, i see it on monitor, but header didn't add.
Sorry for my language, but i hope you could help me )
Thanks!
docker-compose.slim.yml
services:
traefik:
image: traefik:v2.6
build:
context: ./traefik
dockerfile: Dockerfile
restart: unless-stopped
container_name: traefik
ports:
- "80:80"
- "443:443"
expose:
- ${X_DEBUG_REMOTE_PORT:-9001}
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik/dynamic.yaml:/dynamic.yaml:ro
- ./traefik/traefik.yaml:/etc/traefik/traefik.yaml:ro
- ./traefik/certs:/etc/certs:ro
networks:
- vi-docker-network
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`monitor.vi.local`)"
- "traefik.http.routers.api.priority=1000"
- "traefik.http.routers.api.service=api@internal"
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
env_file:
- .env
ports:
- "8081:8081"
depends_on:
- php
expose:
- ${X_DEBUG_REMOTE_PORT:-9001}
networks:
- vi-docker-network
volumes:
- ./../:/app:delegated
labels:
- "traefik.enable=true"
- "traefik.http.middlewares.redirects.redirectScheme.scheme=https"
- "traefik.http.routers.nginx-web.rule=HostRegexp(`{subdomain:[a-z]+}.vi.local`)"
- "traefik.http.routers.nginx-web.entrypoints=web"
- "traefik.http.routers.nginx-web.middlewares=redirects"
- "traefik.http.routers.nginx-web-secured-home.rule=HostRegexp(`{subdomain:[a-z]+}.vi.local`)"
- "traefik.http.routers.nginx-web-secured-home.entrypoints=web-secured"
- "traefik.http.routers.nginx-web-secured-home.tls=true"
- "traefik.http.services.backend.loadbalancer.server.port=8081"
traefik.yaml
global:
sendAnonymousUsage: false
api:
dashboard: true
insecure: false
debug: true
entryPoints:
web:
address: ":80"
web-secured:
address: ":443"
providers:
docker:
exposedByDefault: false
network: "vi-docker-network"
file:
filename: "/dynamic.yaml"
watch: true
serversTransport:
insecureSkipVerify: true
experimental:
localPlugins:
traefikuseragent:
moduleName: traefikuseragent
dynamic.yaml
http:
middlewares:
my-traefikuseragent:
plugin:
traefikuseragent:
Foo: 3
routers:
local-home:
rule: HostRegexp(`{subdomain:[a-z]+}.vi.local`) && Path(`/`)
middlewares:
- my-traefikuseragent
entryPoints:
- "web"
- "web-secured"
service: frontend
priority: 800
tls:
domains:
- main: "vi.local"
sans:
- "*.vi.local"
local-vue:
rule: HostRegexp(`{subdomain:[a-z]+}.vi.local`) && PathPrefix(`/cart`, `/our-actions`, `/checkout`, `/_nuxt`, `/_loading`, `/__webpack_hmr`, `/__webpack`, `/services`, `/compare`, `/user`, `/lk-api`, `/review-multiform`, `/recommended`)
entryPoints:
- "web"
- "web-secured"
service: frontend
priority: 800
tls:
domains:
- main: "vi.local"
sans:
- "*.vi.local"
services:
frontend:
loadBalancer:
servers:
- url: "http://host.docker.internal:3000"
frontend-mobile:
loadBalancer:
servers:
- url: "http://host.docker.internal:3030"
tls:
certificates:
- certFile: "/etc/certs/localhost.crt"
keyFile: "/etc/certs/localhost.key"
stores:
- default
stores:
default:
defaultCertificate:
certFile: "/etc/certs/localhost.crt"
keyFile: "/etc/certs/localhost.key"
traefikuseragent plugin
// Package traefikuseragent a demo plugin.
package traefikuseragent
import (
"context"
"net/http"
"strconv"
"github.com/mssola/user_agent"
)
// Config the plugin configuration.
type Config struct {
Foo int
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// TraefikUserAgent a TraefikUserAgent plugin.
type TraefikUserAgent struct {
next http.Handler
name string
}
// New created a new Demo plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &TraefikUserAgent{
next: next,
name: name,
}, nil
}
func (mw *TraefikUserAgent) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ua := user_agent.New(req.Header.Get(UserAgentHeader))
rw.Header().Add("X-Device-Mobile", strconv.FormatBool(ua.Mobile()))
mw.next.ServeHTTP(rw, req)
}