Squid Proxy Basic Auth Configuration
This quick and straightforward how-to is the one I would have loved to have found while I was setting up Squid proxy for a production application.
Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-requested web pages.
I wanted to restrict access to the proxy to only authenticated users. Here’s how I did it.
Create the htpasswd file
The first step is to create the htpasswd file.
|
|
This will prompt for a password.
Make sure Squid can read the htpasswd file
Without this, Squid will not be able to read the file and fail silently when trying to authenticate.
|
|
Verify the authentication
Run the following command to verify the authentication against squid’s basic_ncsa_auth tool:
|
|
Squid configuration
Add the following to your Squid configuration file:
|
|
Ensure that http_access allow authenticated_users
is the last allow rule in the file.
Verify the squid configuration is correct with:
|
|
And restart Squid:
|
|
Check if it actually works
From your local machine (meaning not the machine that is running the Squid proxy), try to access a website through the proxy with authentication:
|
|
Make sure you try to use the proxy without authentication as well, to make sure we get an access denied error.
|
|
Check the logs
Use the following command to check the logs:
|
|
You should see logs like this:
|
|
TCP_TUNNEL/200
indicates the requests are redirected correctly.
Troubleshooting
If you get TCP_DENIED/40*
in the logs, it means something is wrong with the authentication mechanism, for example:
|
|
In this case, the request was denied because the user was not authenticated to the proxy.