Authentication
Basic authentication
When your traffic is HTTP, you can use basic authentication to display a login prompt to users. Configuring it is easy, but it does have one drawback: credentials are transmitted in the clear over HTTP. You can mitigate this exposure by enabling TLS to encrypt the traffic. In our examples, we will do just that.
Enable basic authentication Jump to heading
Follow these steps to set up basic authentication:
-
Usernames and their associated passwords are stored in the load balancer’s running memory.
To define them, create a
userlistsection. Each entry in this section has auserargument to indicate the username and aninsecure-passwordargument to indicate the password.haproxyuserlist mycredentialsuser joe insecure-password joespassworduser alice insecure-password alicespassworduser mark insecure-password markspasswordhaproxyuserlist mycredentialsuser joe insecure-password joespassworduser alice insecure-password alicespassworduser mark insecure-password markspassword -
In your
frontendsection, enable TLS on yourbindline so that credentials will be encrypted when transmitted between the client and load balancer.In this example, we also redirect HTTP requests to HTTPS. We use the
http-request authline to display the basic authentication login prompt to users. If a user has already logged in, then they won’t see the prompt again.haproxyfrontend wwwbind :80bind :443 ssl crt /site.pemhttp-request redirect scheme https unless { ssl_fc }http-request auth unless { http_auth(mycredentials) }default_backend webservershaproxyfrontend wwwbind :80bind :443 ssl crt /site.pemhttp-request redirect scheme https unless { ssl_fc }http-request auth unless { http_auth(mycredentials) }default_backend webservers
Hash passwords in the userlist Jump to heading
You can store a hashed value for a password in the userlist section instead of storing it as cleartext.
-
Install the
mkpasswdtool:nix# mkpasswd is included in the whois packagesudo apt install whoisnix# mkpasswd is included in the whois packagesudo apt install whoisnixsudo yum install mkpasswdnixsudo yum install mkpasswd -
Call
mkpasswdwith the SHA-256 algorithm to hash your password:nixmkpasswd -m sha-256 mypassword123nixmkpasswd -m sha-256 mypassword123outputtext$5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/outputtext$5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/ -
Store the hashed password by using the
passwordargument:haproxyuserlist mycredentialsuser joe password $5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/haproxyuserlist mycredentialsuser joe password $5$s6Subz0X7FSX2zON$r94OtF6gOfWlGmySwvn3pDFIAHbIpe6mWneueqtBOm/
See also Jump to heading
For complete information on these directives related to authentication, see the HAProxy Configuration Manual:
- Specify the port for incoming traffic: bind
- Prompt the user to authenticate: http-request auth
- Specify accepted usernames and passwords: userlists
Do you have any suggestions on how we can improve the content of this page?