Skip to content

Instantly share code, notes, and snippets.

@mxr576
Created October 18, 2022 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxr576/701156f9c3084aa9ca4f2327164a15b0 to your computer and use it in GitHub Desktop.
Save mxr576/701156f9c3084aa9ca4f2327164a15b0 to your computer and use it in GitHub Desktop.
How to setup basic auth in DrupalDev (Wodby Nginx)

Content of the .htpasswd file

user:$apr1$TCSj3uFG$x3fNG4H6gvuxvNu5UMo3i/

(username: "user", password: "user")

@see https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-nginx-on-ubuntu-22-04

Only relevant changes in the drupal9-custom.conf.tmpl file

 
 location / {
+    auth_basic "Restricted Content";
+    auth_basic_user_file /etc/nginx/.htpasswd;
+
     {{- if getenv "NGINX_DRUPAL_FILE_PROXY_URL" }}
     location ~* /sites/.+/files {
         try_files $uri @file_proxy;

(use the latest source but this version was used when the demo was made)

Only relevant changes in docker-compose.override.yml file

services:

  # @see https://github.com/wodby/nginx/tree/5.25.6#custom-preset
  webserver:
    volumes:
      - ./.htpasswd:/etc/nginx/.htpasswd
      - ./drupal9-custom.conf.tmpl:/etc/gotpl/presets/drupal9-custom.conf.tmpl
    environment:
      NGINX_VHOST_PRESET: drupal9-custom

Just the relevant parts...

version: "3.3"

services:

  webserver:
    labels:
      - "traefik.enable=true"
      # @see https://doc.traefik.io/traefik/middlewares/http/basicauth/
      # TL;DR echo $(htpasswd -nB user) | sed -e s/\\$/\\$\\$/g
      # username: "user" password: "cica"
      - "traefik.http.routers.webserver.middlewares=auth"
      - "traefik.http.middlewares.auth.basicauth.users=user:$$2y$$05$$V69oJF53KEWuf01.AQNBMOTjjYCwu43BkMqAb1jCZREZCvspL6c5q"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment