access log
The access log feature of Classic Load Balancer (CLB) is designed for Layer 7 load balancing. It integrates with Simple Log Service (SLS) to improve the efficiency of log analysis and troubleshooting for business development, testing, and user behavior analysis.
Overview
CLB access logs
The access log feature for Classic Load Balancer (CLB) captures detailed information about all requests sent to a CLB instance, including the request time, client IP address, latency, request path, and server response. As a public-facing entry point, CLB handles a high volume of requests. You can use access logs to analyze user behavior, understand traffic distribution, and troubleshoot issues.
After you enable the access log feature, you can store, collect, and analyze access logs in a Logstore of Simple Log Service (SLS). You can disable the access log configuration at any time.
The CLB access log feature is free, but Simple Log Service incurs charges. For more information, see Billing overview of Simple Log Service.
Limitations
-
The access log feature is available only for Layer 7 load balancing (HTTP and HTTPS listeners) of CLB.
-
Layer 4 listeners (TCP/UDP) do not support the access log feature. Please configure logging directly on your backend servers, for example using Nginx access logs or an application-level logging framework.
-
Ensure that HTTP header values do not contain
||. Otherwise, log fields may be misaligned during parsing.
Prerequisites
-
You have created a CLB instance.
-
You have created a vServer group, added backend servers to the vServer group, and deployed applications on the backend servers.
-
You have configured an HTTP listener or an HTTPS listener for the CLB instance.
-
You have activated Simple Log Service.
Configure access logs
Log on to the CLB console.
-
In the left-side navigation pane, choose .
-
In the top navigation bar, select the region where the CLB instance is deployed.
-
If this is your first time using the feature, click Authorize. On the Authorize Access to Cloud Resources page, click Confirm.
NoteYou only need to perform this authorization once.
If you are a RAM user, the primary Alibaba Cloud account must first grant you the required permissions. For more information, see Authorize a RAM user to use the access log feature of CLB.
-
On the Access Log (Layer 7) page, find the target CLB instance, and click Configure in the Actions column.
-
In the Log Settings panel, set the Project and Logstore, and then click OK.
-
Project: A Project is a resource management unit in SLS used for resource isolation and control. We recommend using different Projects for different applications.
-
Logstore: A Logstore is the unit for collecting, storing, and querying log data in SLS. We recommend creating separate Logstores for different types of logs within the same application.
-
Select Logstore: This enables a preset analysis dashboard for the selected Logstore by default. If an index is already configured for the Logstore, the existing index configuration is overwritten.
-
Note-
Ensure the Project is in the same region as the CLB instance and its name is globally unique.
-
After configuration, the SLS Project and Logstore may take a few minutes to appear in the SLS console. Log data is not lost during this period. If they are not yet visible, refresh the page later.
After you configure access logging for the load balancer, you can query and retrieve log information by using the following fields in SLS.
Parameter
Description
body_bytes_sent
The size of the HTTP body sent to the client, in bytes.
client_ip
The client IP address.
client_port
The port of the client that sent the request.
host
The host is retrieved from the first available source in the following order: request parameters, the Host header, and the IP address of the backend server that processes the request.
http_host
The content of the Host header in the request.
http_referer
The Referer header of the HTTP request.
http_user_agent
The User-Agent header of the HTTP request.
http_x_forwarded_for
The X-Forwarded-For header of the HTTP request.
http_x_real_ip
The X-Real-IP header of the HTTP request.
read_request_time
The time that the load balancer takes to read the request. Unit: milliseconds.
request_length
The length of the request, including the start line, HTTP header, and HTTP body.
request_method
The request method.
request_time
The time from when the load balancer receives the first byte of a request to when it sends the last byte of the response. Unit: seconds.
request_uri
The URI of the request that is received by the load balancer.
scheme
The request scheme. Valid values: http and https.
server_protocol
The HTTP version of the request that is received by the load balancer, such as HTTP/1.0 or HTTP/1.1.
slb_vport
The listener port of the load balancer.
slbid
The ID of the load balancer instance.
ssl_cipher
The cipher suite used to establish the SSL connection, such as ECDHE-RSA-AES128-GCM-SHA256.
ssl_protocol
The protocol used to establish the SSL connection, such as TLSv1.2.
status
The status code in the response from the load balancer.
tcpinfo_rtt
The round-trip time (RTT) of the client's TCP connection. Unit: microseconds.
time
The time when the log entry was recorded.
upstream_addr
The IP address and port of the backend server.
upstream_response_time
The time from when a connection to a backend server is established to when the connection is closed after a response is received. Unit: seconds.
upstream_status
The response status code that the load balancer receives from the backend server.
vip_addr
The virtual IP address (VIP).
write_response_time
The time that the load balancer takes to write a response. Unit: milliseconds.
-
Usage notes
client_ip and http_x_forwarded_for
When you analyze request sources in SLS access logs, these two fields have different meanings:
-
client_ip: The source IP address that established the connection to CLB, as observed by CLB at the network layer. If there is no proxy in front of the client, this is the true client IP address. If the client is behind a proxy, this is the egress IP address of the nearest proxy. -
http_x_forwarded_for: The original value of theX-Forwarded-Forheader in the inbound request received by CLB. It is written by the client or an upstream proxy, can be spoofed, and is for reference only.
To analyze request sources in access logs, use client_ip.
If you need to obtain the true client IP address on a backend server (rather than analyze it in access logs), note that CLB Layer-7 listeners pass through the true client IP address in the X-Forwarded-For header by default. For more information, see Retrieve client IP addresses with a CLB Layer 7 listener.
Identifying which forwarding rule handled a request
Access logs do not record server group IDs or the names of traffic forwarding rules. To determine which forwarding rule handled a specific request, correlate the request_uri and http_host values in the log with the forwarding rules configured in the CLB console. Based on this, you can further verify that a forwarding rule is working as expected:
-
Identify inactive forwarding rules: Query the number of times a forwarding rule was matched within a specific time range by combining
slbid,request_uri, and a time range. If the result is0, the rule did not match any request during that period. Evaluate whether to clean it up based on your business needs.slbid: <instance-ID> and request_uri: <forwarding-rule-path> | select count(*) as hit_count -
Verify that forwarding matches expectations: Compare the
upstream_addrvalue (the backend server IP address and port to which the request was actually forwarded) in the log with the server group members of the corresponding listener in the CLB console, to confirm that requests are forwarded to the intended backend servers.
TLS policy interception
Connections that are blocked by a TLS security policy do not complete the TLS handshake and therefore do not generate access log entries. Access logs record only requests that successfully establish a connection with CLB.
Query access logs
After you configure the access log feature, you can query logs in the SLS console.
Log on to the CLB console.
-
In the left-side navigation pane, choose .
-
In the top navigation bar, select the region where the CLB instance is deployed.
-
On the Access Log (Layer 7) page, find the target instance, and click View Logs in the Actions column. You are redirected to the SLS console.
-
After SLS is configured, you can view log entries for any client that has accessed the CLB instance.
-
Enter an SQL statement to query specific access logs.
For example, run the following SQL statement to find the top 20 clients. The results can help you analyze traffic sources and inform business decisions.
* | select http_user_agent, count(*) as pv group by http_user_agent order by pv desc limit 20For example, run the following SQL statement to query for access requests from a specific IP address range by filtering logs by source IP address and response status code.
* | select * where client_ip like '172.17.%' and status=200Filtering logs when multiple CLB instances share a Logstore
If multiple CLB instances share the same SLS Logstore, queries that filter only by domain or port may return incomplete or misleading results. To narrow down logs to a specific CLB instance and listener, combine the
slbid(instance ID) andslb_vport(listener port) fields in your query.For example, to query 5xx errors for a specific instance and listener port, run the following SQL statement:
slbid: <instance-ID> and slb_vport: <port> and status: 5*If your query returns no results, verify the following:
-
The
slbidvalue must exactly match the instance ID shown in the CLB console (not the instance name). -
The
slb_vportvalue must match the listener port number (for example,80or443). -
Widen the query time range, because log entries may be written with a short delay.
NoteNote the following about log refresh behavior and same-timestamp entries:
-
Log refresh behavior: Field index statistics in SLS are updated in real time as log entries are written. There is no fixed refresh interval. If you switch the query time range in the console, click the query button to reload the results.
-
Multiple entries with the same timestamp: If you see multiple log entries that share the same
timevalue, this is expected behavior and indicates that the client sent multiple requests within the same second. This is a normal pattern for high-frequency clients and does not indicate a system error. If you determine that the repeated requests are abnormal, configure an access control policy to restrict the source IP address.
-
Query logs programmatically by using the SLS SDK or API
CLB access log data is stored in a Logstore in Simple Log Service (SLS). In addition to the console, you can query the log data programmatically by using the SLS SDK or API, without logging on to the console. For detailed steps and code examples, see Quickstart: Upload and analyze logs with the SLS SDK.
CLB-side APIs such as DescribeAccessLogsDownloadAttribute are used only for managing log download configurations and cannot be used to query log data.
Analyze access logs
You can use dashboards in SLS to analyze access logs. Dashboards provide rich data visualizations and insights.
-
On the SLS page, click the
icon in the left-side navigation pane, and then click Dashboard. -
Click the name of the dashboard that corresponds to the access logs of your CLB instance, such as slb_layer7_access_center_cn, to view the analysis report.
Business traffic analysis
In addition to dashboards, you can write SQL statements for the following common business scenarios.
Count requests by listener port
To count the total number of requests handled by a specific CLB instance and listener port:
slbid: <instance-ID> and slb_vport: <port> | select count(*) as total_requests
View domain traffic share
To calculate the traffic share of each domain based on the http_host and request_length fields:
* | select http_host, sum(request_length) as total_bytes, round(sum(request_length) * 100.0 / sum(sum(request_length)) over(), 2) as traffic_pct group by http_host order by total_bytes desc
View backend server traffic distribution
To identify which backend servers receive the most requests and estimate their traffic distribution using upstream_addr and request_length:
* | select upstream_addr, count(*) as request_count, sum(request_length) as total_bytes group by upstream_addr order by request_count desc limit 20
Identify abnormal requests
CLB access logs record all external requests that pass through the load balancer instance. If you find requests from unknown sources in the logs, you can use the following key fields to determine whether they are abnormal external requests and take appropriate protective measures.
Key fields
Combine the following log fields to determine if a request is from an abnormal client:
-
client_ip: The IP address of the client that sent the request. If the IP address does not belong to your business systems or the Alibaba Cloud internal IP address range, the request originates from an external client. You can use an IP lookup tool to further confirm the source. -
request_uri: The URI path of the request. If paths unrelated to your business appear (such as probe or scan paths, or API paths of other cloud services), the requests are typically abnormal external requests. -
request_method: The HTTP method of the request. If your business uses only GET requests but the logs show a large number of PUT or DELETE requests, check for abnormal behavior. -
http_user_agent: The User-Agent identifier of the request. Automated tools or crawlers often use specific User-Agent headers, such ascurlorpython-requests, which differ from those of normal browser requests.
For example, you can use the following SQL statement in SLS to query abnormal requests from unexpected IP addresses:
* | select client_ip, request_method, request_uri, count(*) as cnt group by client_ip, request_method, request_uri order by cnt desc limit 50
Restrict abnormal source IPs
After you confirm the source IP addresses of abnormal requests, you can use the access control feature of CLB to configure a blacklist and block subsequent requests from these IP addresses. Perform the following steps:
-
In the left-side navigation pane of the CLB console, choose Access Control.
-
Create an access control policy group and add the identified abnormal source IP addresses to it.
-
Bind the policy group to your listener as a blacklist. CLB will then reject all requests from the blacklisted IP addresses instead of forwarding them to backend servers.
Disable access logs
You can disable the access log feature to stop collecting CLB access logs.
Disabling access logging for a CLB instance does not delete the associated Project or Logstore. The instance's historical logs are not immediately deleted, and you can still manage them in SLS.
Log on to the CLB console.
-
In the left-side navigation pane, choose .
-
In the top navigation bar, select the region where the CLB instance is deployed.
-
On the Access Log (Layer 7) page, find the target instance, and then click Disable Logging in the Actions column.
-
In the dialog box that appears, click OK to disable access logging for the instance.
Related documents
-
To learn more about Simple Log Service, see What is Simple Log Service?.
-
If you suspect a backend server is unhealthy when using Layer 7 load balancing, see Use CLB access logs to quickly locate an unhealthy backend server.
FAQ
How do I use access logs to troubleshoot request latency, timeouts, or connection issues?
Identify the source of latency
Compare the request_time and upstream_response_time fields:
-
request_time: The total elapsed time from when CLB receives the first byte of a request to when it sends the last byte of the response. Unit: seconds. -
upstream_response_time: The time from when CLB establishes a connection to a backend server to when it receives the complete response and closes the connection. Unit: seconds.
The difference between the two (request_time minus upstream_response_time) includes CLB processing, the network round trip between the client and CLB, and the time the client takes to read the response. Compare their proportions to locate the bottleneck:
-
upstream_response_timeaccounts for a large proportion ofrequest_time: The latency is primarily caused by a slow backend server response. Check the performance of your backend applications, databases, and so on. -
upstream_response_timeaccounts for a small proportion (that is, the difference is large): The backend responds quickly, so the bottleneck is more likely the network between the client and CLB, or the client slowly reading a large response body. Use fields such asclient_ipandbody_bytes_sentto investigate further, rather than attributing the latency to CLB processing.
Troubleshoot timeouts
A request that times out fails during the TCP handshake or protocol establishment phase and does not generate a corresponding access log entry. As a result, you cannot find these requests directly in the access logs. To investigate indirectly, query the access logs for the relevant time range in the SLS console and check for a sharp drop in request volume or a gap in the logs to estimate when the timeouts occurred. For further diagnosis, perform end-to-end troubleshooting using upstream and downstream logs, such as WAF logs upstream of CLB and the application and firewall logs on your backend ECS instances.