Skip to content
Mahesh K. edited this page Mar 2, 2025 · 1 revision

Traefik Plugin: Block User-Agent

A Traefik middleware plugin to block HTTP requests based on the User-Agent header, allowing only user-specified browser patterns.

Features

  • Blocks all User-Agents by default.
  • Allows user-defined browsers via:
    • Custom regex patterns (e.g., MyBrowser/12[0-1].*).
    • Version thresholds (e.g., >121 generates MyBrowser/121.*).
  • Optional OS type filtering with regex patterns.
  • No external APIs or caching; relies entirely on user configuration.
  • Browser names are dynamic and used as provided in regex generation.

Usage

  1. Add the plugin to your Traefik configuration.
  2. Configure the plugin with the desired browser patterns.
  3. Attach the plugin to your Traefik middleware.

Traefik Experimental Plugin Registry (traefik.yml)

experimental:
  plugins:
    blockUserAgents:
      moduleName: "github.com/mkopnsrc/traefik-plugin-block-useragents"
      version: "v1.0" # Optional

Traefik Local Plugin (traefik.yml)

Ensure Local Plugin directory is mounted in the Traefik container.

experimental:
  localPlugins:
    blockUserAgents:
      moduleName: "github.com/mkopnsrc/traefik-plugin-block-useragents"

Usage Configuration

Browsers Only

http:
  middlewares:
    block-ua:
      plugin:
        blockUserAgents:
          allowedBrowsers:
            - name: "Chrome"
              regex: "Chrome/12[0-1].*"
            - name: "Firefox"
              version: ">122"

Browsers with OS Types Filtering

http:
  middlewares:
    block-ua:
      plugin:
        blockUserAgents:
          allowedBrowsers:
            - name: "Chrome"
              version: ">130"
            - name: "Firefox"
              regex: "Firefox/13[1-5].*"
            - name: "Safari"
              version: ">533"
            - name: "Edg" # Microsoft Edge
              version: ">100"
            - name: "Brave" 
              version: ">1.75"
            - name: "CriOS" # Chrome for iOS
              regex: "CriOS/13[0-9]"
          allowedOSTypes:
            - "Windows NT 10\.0" # Windows 10
            - "Mac OS X 10\.[0-9]+" # macOS 10.x
            - "Linux" # Linux
            - "Android" # Android
            - "iOS" # iOS