Basic authentication in nginx works by requiring clients to send a special header with username and password encoded. When a client requests a protected path, nginx looks for the Authorization header. If it is missing, nginx responds with 401 Unauthorized and a prompt to send credentials. When the client sends credentials, nginx decodes and checks them against a password file. If valid, nginx allows access with a 200 OK response. If invalid, nginx again responds with 401 Unauthorized. This process repeats until valid credentials are provided or the client stops trying.