Applying Anubis Selectively with Middleware Manager
This guide details how to transition from a global, catch-all Anubis implementation to a more flexible setup where Anubis is applied as a forwardAuth middleware. This allows you to use Middleware Manager to protect specific routers (resources) on a case-by-case basis, giving you granular control over your application’s security.
The Concept: forwardAuth Middleware
Instead of using a low-priority router to catch all traffic, we will define Anubis as a middleware. This middleware can then be attached to any router you choose within the Middleware Manager UI.
Advantages of this approach:
-
Granular Control: Protect specific routes (e.g.,
/dashboard) while leaving others (e.g.,/api/public) unprotected. -
Dynamic Configuration: Easily add or remove Anubis protection from any resource through the Middleware Manager UI without editing YAML files or restarting containers.
-
Cleanliness: Keeps your routing configuration clean and focused on directing traffic, while security logic is handled by middleware.
Step 1: Revert Dynamic Routing Configuration
First, we must remove the “catch-all” logic from your dynamic configuration file. This step is crucial as it returns your routers to their original state, listening directly on the public entrypoint.
Edit your dynamic routing file (e.g., ./config/traefik/rules/your-file.yml or /root/config/traefik/dynamic_config.yml) and restore it to its original configuration before we added the Anubis routers and services.
# ./config/traefik/rules/your-file.yml
# ./root/config/traefik/dynamic_config.yml
http:
middlewares:
redirect-to-https:
redirectScheme:
scheme: https
routers:
# HTTP to HTTPS redirect router
main-app-router-redirect:
rule: "Host(`pangolin.development.hhf.technology`)"
service: next-service
entryPoints:
- web
middlewares:
- redirect-to-https
# Next.js router (handles everything except API and WebSocket paths)
next-router:
rule: "Host(`pangolin.development.hhf.technology`) && !PathPrefix(`/api/v1`)"
service: next-service
entryPoints:
- websecure # <-- Back to websecure
tls:
certResolver: letsencrypt
# API router (handles /api/v1 paths)
api-router:
rule: "Host(`pangolin.development.hhf.technology`) && PathPrefix(`/api/v1`)"
service: api-service
entryPoints:
- websecure # <-- Back to websecure
tls:
certResolver: letsencrypt
services:
# ORIGINAL SERVICES
next-service:
loadBalancer:
servers:
- url: "http://pangolin:3002" # Next.js server
api-service:
loadBalancer:
servers:
- url: "http://pangolin:3000" # API/WebSocket server
Key Change: All routers that should be publicly accessible are now back on the websecure entrypoint. The anubis entrypoint is no longer referenced here, and the anubis-catchall-router and anubis-service have been removed from this file.
Step 2: Ensure Anubis Service is Running
Your docker-compose.yml file should still contain the anubis service definition from the previous guide. It doesn’t need any changes. It simply needs to be running so that Traefik can forward authentication requests to it.
# docker-compose.yml (ensure this service exists)
services:
# ... your other services
anubis:
image: ghcr.io/techarohq/anubis:main
container_name: anubis
restart: unless-stopped
networks:
- pangolin
environment:
- BIND=:8923
- TARGET=http://traefik:3923 # This is now ignored but harmless
- COOKIE_DOMAIN=hhf.technology
- COOKIE_DYNAMIC_DOMAIN=false
- DIFFICULTY=4
After modifying your rules, restart the stack to apply the changes:
docker compose up -d --force-recreate
At this point, your site should function as it did before, with no Anubis protection active.
Step 3: Create the Anubis forwardAuth Middleware
Now, let’s create the reusable Anubis middleware in your Middleware Manager UI.
-
Navigate to your Middleware Manager dashboard.
-
Click on “Middlewares” in the sidebar.
-
Click the “Add Middleware” button.
-
Fill out the form:
-
Name:
anubis-auth(or another descriptive name). -
Type: Select
ForwardAuthfrom the dropdown menu. -
Address: Enter the address of the Anubis service:
http://anubis:8923. -
Trust Forward Header: Leave this checked (it’s the default and generally recommended).
-
-
Click “Save”.
You have now created a middleware named anubis-auth that, when applied to a router, will send all incoming requests for that router to the Anubis service for verification.
Step 4: Apply Anubis Protection to a Resource
Let’s apply your new middleware to protect the main front-end router but leave the API router unprotected.
-
In Middleware Manager, click on “Resources” in the sidebar.
-
You will see a list of your resources (
next-router,api-router, etc.). -
Find the eg:-
next-routerresource and click the “Manage” button. -
In the resource management view, click on the “Middlewares” tab.
-
You will see a dropdown list of available middlewares. Select
anubis-auth@filefrom the list. -
Click the “Assign” button.
The anubis-auth middleware is now active on your next-router.
Step 5: Deploy and Verify
-
Deploy Changes: In Middleware Manager, a “Deploy Changes” button should appear at the top. Click it to write the new middleware configuration to your Traefik rules file.
-
Verify:
-
Open a new private browsing window and navigate to
https://pangolin.development.hhf.technology. Because this matches thenext-router, you should be greeted by the Anubis challenge page. -
After the challenge, you should be redirected to your Pangolin UI.
-
Now, try to access an API endpoint directly, for example, by using
curlor visitinghttps://pangolin.development.hhf.technology/api/v1/some-endpoint. This request should go through without an Anubis challenge, because we did not apply theanubis-authmiddleware to theapi-router.
-
You have successfully configured granular, on-demand bot protection using Anubis and Middleware Manager. You can now repeat Step 4 for any other resource you wish to protect.