DEV Community

Łukasz Wolnik
Łukasz Wolnik

Posted on • Edited on

9 1

Basic HTTP authentication in Traefik 2

In order to secure your website with Basic HTTP authentication in Traefik do the following.

1. Download apache2-utils



sudo apt install apache2-utils


Enter fullscreen mode Exit fullscreen mode

2. Generate password and copy its output



htpasswd -nB adam


Enter fullscreen mode Exit fullscreen mode

The n option will display the hash in stdout and the B option will use more secure encryption.

devto password's hash is shown below:



adam:$2y$05$h9OxLeY20/5uiXjfPgdRxuFlrfqBf2QifYDgrwsR6rAEgX3/dpOGq


Enter fullscreen mode Exit fullscreen mode

3. Replace $ with $$



adam:$$2y$$05$$h9OxLeY20/5uiXjfPgdRxuFlrfqBf2QifYDgrwsR6rAEgX3/dpOGq


Enter fullscreen mode Exit fullscreen mode

4. Create a new middleware in Traefik for HTTP basic auth for your HTTPS entrypoint.



  labels:
    - "traefik.http.middlewares.yourservice-basicauth.basicauth.users=adam:$$2y$$05$$h9OxLeY20/5uiXjfPgdRxuFlrfqBf2QifYDgrwsR6rAEgX3/dpOGq"


Enter fullscreen mode Exit fullscreen mode

Remember to use the escaped double $.

5. Create a new chain for your HTTPS connection.

You may have existing rules for your HTTPS route, e.g. compressing, etc.

So create a new chain where you'll combine your existing middlewars and the newly created yoursite-basicauth.

In your docker-compose.yml file:



  labels:
    - "traefik.http.middlewares.yourservice-https-chain.chain.middlewares=yourservice-basicauth,yourservice-other-middleware-remove-or-replace-with-yours"
    - "traefik.http.routers.yourservice-https.middlewares=yourservice-https-chain"


Enter fullscreen mode Exit fullscreen mode

Basic HTTP auth in Traefik

Restart your service and your website will now prompt browsers for username and password.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
woto profile image
Ruslan Kornev

Thanks. Works like a charm.

Collapse
 
extsol profile image
Solutions External • Edited

you have saved my life, thx man

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay