"SQUID: How to set up separate logging of successful, failed and unknown requests"

Hello,
First of all, I want to say that www/squid works great on FreeBSD.
In this short tutorial, we will look at how to set up separate logging in the Squid proxy server to conveniently track successful, failed, and unspecified requests.
This configuration allows system administrators to easily monitor traffic, analyze the causes of failures, and quickly identify problems with access to resources.
We will configure Squid so that it saves successful and failed connections in separate log files.


1.
In this section, we create ACLs (Access Control Lists) for filtering requests and responses in Squid.
These ACLs help determine whether an HTTP connection contains request or response data.
Let's look at each element:
acl hasRequest has request
This line creates an ACL called hasRequest, which is triggered on any HTTP request that contains data.
This will allow Squid to determine that the request was received and processed.

acl hasResponse has response
This line creates an ACL called hasResponse, which is triggered on any response from the server that contains data.
This allows Squid to distinguish between responses that contain a payload, which is useful for controlling and filtering based on the type of response.
These ACLs allow us to set up more complex conditions for subsequent filtering and logging, helping to control access based on the content of requests and responses.

Let's add these ACLs to the configuration file squid.conf

acl hasRequest has request
acl hasResponse has response


2.
In this section, we configure Squid to log requests separately based on their success or failure by using ACLs to define different types of HTTP responses and connection methods.
This setup allows us to direct requests to different log files for easier analysis. Let’s go through each part:

Defining HTTP status codes for success and failure:


acl success_codes http_status 100-199 # informational
acl success_codes http_status 200-299 # successful transactions
acl success_codes http_status 300-399 # redirection
acl failure_codes http_status 400-499 # client error
acl failure_codes http_status 500-599 # server error


The success_codes ACL is set up for HTTP status ranges considered successful: informational responses (100-199), successful transactions (200-299), and redirections (300-399).
The failure_codes ACL applies to ranges indicating errors: client errors (400-499) and server errors (500-599).

Setting up hierarchy codes for successful and failed connections (hier_code):


acl success_hier hier_code HIER_DIRECT
acl failure_hier hier_code HIER_NONE


success_hier defines successful connections that are made directly (using HIER_DIRECT).
failure_hier captures failed connections that did not reach the server (using HIER_NONE).

Creating combined ACLs to filter successful and failed requests:


acl failure all-of hasRequest CONNECT failure_hier
acl failure all-of hasRequest !CONNECT hasResponse failure_codes

acl success all-of hasRequest CONNECT success_hier
acl success all-of hasRequest !CONNECT hasResponse success_codes


failure: Defines requests as failed if:
The method used is CONNECT, but the connection could not be established (using failure_hier).
Or, the request is not CONNECT, and the response contains error codes (failure_codes).

success: Defines requests as successful if:
The method is CONNECT, and the connection is successfully established directly (success_hier).
Or, the request is not CONNECT, and the response is successful or redirecting (success_codes).

3.
Setting up separate log files:


access_log stdio:/var/log/squid/success.log logformat=squid success
access_log stdio:/var/log/squid/failure.log logformat=squid failure
access_log stdio:/var/log/squid/unknown.log logformat=squid !success !failure


success.log: Records all successful requests as defined by the success ACL.
failure.log: Records all failed requests that match the failure ACL.
unknown.log: Logs requests that do not meet either the success or failure criteria.

Let's add these ACLs to the configuration file, and how does it all look together in squid.conf


acl success_codes http_status 100-199 # informational
acl success_codes http_status 200-299 # successful transactions
acl success_codes http_status 300-399 # redirection
acl failure_codes http_status 400-499 # client error
acl failure_codes http_status 500-599 # server error

acl success_hier hier_code HIER_DIRECT
acl failure_hier hier_code HIER_NONE

acl failure all-of hasRequest CONNECT failure_hier
acl failure all-of hasRequest !CONNECT hasResponse failure_codes

acl success all-of hasRequest CONNECT success_hier
acl success all-of hasRequest !CONNECT hasResponse success_codes

access_log stdio:/var/log/squid/success.log logformat=squid success
access_log stdio:/var/log/squid/failure.log logformat=squid failure
access_log stdio:/var/log/squid/unknown.log logformat=squid !success !failure


4.
By configuring Squid to log requests separately based on success and failure, we create a powerful tool for monitoring and analyzing network traffic.

P.S.
I hope this guide has helped you set up effective logging in Squid.
How do you analyze your Squid logs?
Do you use any additional tools to gain insights or streamline your process?

Best regards,
 
Back
Top