barbieswimcrew
barbieswimcrew

Reputation: 448

How to configure nginx basic_auth in DDEV

Trying to get basic_auth work for nginx in DDEV.

I added a .ddev/nginx_full/auth.conf with the following content

server {
    listen 80;
    listen 443;
    server_name dev.example.com;

    location ^~ / {
        root /var/www/html/public;
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
}

and the basic auth prompt appears but after typing in the credentials I get a 404... I have no clue. What am I doing wrong here?

Upvotes: 0

Views: 40

Answers (1)

barbieswimcrew
barbieswimcrew

Reputation: 448

To get basic_auth work properly there are just a few things to do:

1. Create a .ddev/nginx_full/auth.conf file containing the following:

auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

This needs to restrict all requests and leads to a basic auth prompt as expected.

2. Create a .ddev/nginx_full/.htpasswd file containing your username:password credentials

Create the file by using the htpasswd CLI tool or use an online generator tool. It doesn't matter, just make sure the file's content is valid.

3. Create a .ddev/docker-compose.mounts.yaml file containing the following:

services:
  web:
    volumes:
      - ./nginx_full/.htpasswd:/etc/nginx/.htpasswd

DDEV automatically detects that file and will mount the .htpasswd into the web container on restart.

4. Restart DDEV

Don't forget executing ddev restart ;-)

Upvotes: 1

Related Questions