Squid Chinese authoritative guide (9)

Source: Internet
Author: User
Continued in Squid (9)-Linux Enterprise Application-Linux server application information. The following is a detailed description. > It is important to understand how squid searches for ACL elements for dematching. When an ACL element has multiple values, any single value can cause a match. In other words, squid uses the OR logic when checking the ACL element value. When squid finds the first value match, it stops searching. This means placing the most likely matching value at the beginning of the list can reduce latency.

Let's take a special example and consider the following ACL definition:
Acl Simpsons ident Maggie Lisa Bart Marge Homer

When squid encounters the Simpsons ACL in the access list, it executes the ident query. Let's take a look at what happens when the user's ident service returns Marge? The squid ACL code compares this value with Maggie, Lisa, and Bart before successfully matching Marge. When the search is complete, we think the Simpsons ACL matches the request.

In fact, this is a bit of deception. The ident ACL value is not stored in the unordered list. They are stored in the splay tree. This means that in non-match events, squid does not search all names. To search for N entries in a splay tree, you must record N comparisons. Many other ACL types also use the splay tree. However, regular expression-based types are not used.

Since regular expressions cannot be stored like this, they are stored as linked lists. This makes them very inefficient in large chain tables, especially requests that do not match any regular expressions in the linked list. To improve this form, squid moves the regular expression to the top of the list when a match occurs. In fact, because the ACL matches the Code's natural characteristics, squid moves the matched entries to the second position in the list. In this way, the normal matching value is naturally moved to the top of the ACL list, which reduces the number of comparisons.

Let's take a look at another simple example:
Acl Schmever port 80-90 101 103 107 1 2 3 9999

This ACL matches requests from the original server's port 80-90 and other independent ports. For port 80 requests, squid matches the ACL by checking the first value. For port 9999, each other value is checked first. For a port not in the list, squid checks all values to declare that it does not match. As I have already said, placing the most common values first can optimize ACL matching.


6.2 Access Control Rules

As mentioned above, the ACL element is the first step to establish access control. The second step is access control rules used to allow or deny certain actions. In earlier examples, you have seen http_access rules. Squid has a large number of other access control lists:

Http_access
This is the most important access control list. It determines which clients are allowed HTTP requests and which are rejected. If the http_access configuration is incorrect, squid cache is vulnerable to attacks or improper use.

Http_reply_access
Http_reply_access is similar to http_access. The difference is that the former will be detected only when squid receives response from the original server or superior proxy. Most access control methods are based on customer requests, and http_access is enough for these methods. However, some people prefer to allow or reject requests based on the response content type. For more information, see Chapter 6.3.9.

Icp_access
If your squid is configured to serve the ICP response (see Chapter 10.6), use the icp_access list. In most cases, you should allow only the CDN requests from the neighbor cache.

No_cache
You can use the no_cache access list to indicate the squid, which does not have to store certain responses (in disk or memory ). This list is typically used in combination with dst, dstdomain, and url_regex ACL.
If you use the "no" condition for no_cache, such a double denial will cause some confusion. Requests rejected by the no_cache list are not cached. In other words, no_cache deny... is to prevent the target from being cached. See the example in Chapter 6.3.10.

Miss_access
The miss_access list is mainly used for the neighbor cache of squid. It determines how squid processes cache loss requests. This function is required if squid uses the cluster technology. See the example in 6.3.7.

Redirector_access
The access list determines which request is sent to the redirection process (see chapter 11 ). By default, if you use a reconfigurator, all requests are redirected through the reconfigurator. You can use the redirector_access list to prevent certain requests from being overwritten. This is especially useful because such an access list requires fewer request information than the access control system.

Ident_lookup_access
The ident_lookup_access list is similar to redirector_access. It allows you to perform lazy ident queries on some requests. By default, squid does not publish ident queries. If the request is allowed by ident_lookup_access rules (or ident ACL), squid queries ident.

Always_direct
This access list affects how squid is processed and how the neighbor cache forwarding cache is lost. Squid usually tries to forward the cache
It is lost to the parent cache, and/or squid uses the ICP to find the response to the neighboring cache. However, when the request matches the always_direct rule, squid directly forwards the request to the original server.
With this rule, matching the "allow" rule causes squid to forward requests directly. For more details and examples, see Chapter 10.4.4.

Never_direct
Never_direct is opposite to always_direct. The cache loss request matching the list must be sent to the neighbor cache. This is especially useful for proxies after the firewall.
Using this list, matching the "allow" rule causes squid to forward requests to the neighbor cache. For more details and examples, see Chapter 10.4.3.

Snmp_access
The access list is applied to the query of the SNMP port sent to squid. The ACL you can use with this list is snmp_community and src. If you really want to use it, you can also use srcdomain, srcdom_regex, and src_as. See the example in Chapter 14.3.

Broken_posts
This access list affects how squid processes some POST requests. Some old user proxies send a special carriage return line break at the end of the Request body. That is to say, the message body is 2 bytes longer than the length indicated by the content-length header. What's worse, some old HTTP servers actually rely on this incorrect behavior. When the request matches the access list, squid simulates the client and sends a special line break.
Squid has a large number of other configuration commands that use ACL elements. Some of them used to be global configurations and were modified to use the ACL to provide more flexible control.

Cache_peer_access
This access list controls the HTTP requests sent to the neighbor cache and the query by ICP/HTCP. For more information and examples, see Chapter 10.4.1.

Reply_body_max_size
This access list limits the maximum acceptable size of the HTTP response body. For more information, see Appendix.

Delay_access
The access rule list controls whether the latency pool is applied to the cache loss response of a request. See appendix C.

Tcp_outgoing_address
This access list is bound to the server's TCP connection to the specified local IP address. See Appendix.

Tcp_outgoing_tos
The access list can be set to different TOS/Diffserv values for TCP connections between the original server and the neighboring cache. See Appendix.

Header_access
With this command, you can configure squid to delete some HTTP headers from the requests it forwards. For example, you may want Squid to filter out the Cookie headers in requests sent to some original servers. See Appendix.

Header_replace
This command allows you to replace, rather than delete, the content in the HTTP header. For example, you can set the user-agent header as a false value to meet the requirements of some original servers, but still protect your privacy. See Appendix.


6.2.1 access rule syntax

The access control rule syntax is as follows:
Access_list allow | deny [!] ACLname...

For example:
Http_access allow MyClients
Http_access deny! Safe_Ports
Http_access allow GameSites AfterHours

When reading the configuration file, squid only scans the access control line once. In this way, you must define the ACL elements in the acl row before referencing the ACL elements in the access list. Even the order of access list rules is very important. In what order do you write access lists, squid will check them in what order. Placing the most common ACL at the beginning of the list can reduce the CPU load of squid.

For most access lists, deny and allow have obvious significance. However, some of them are not so clear. Write always_direct, never_direct, and no_cache rules with caution. In always_direct, The allow rule means that matched requests are directly forwarded to the original server. The always_direct deny rule means that the matched requests are not forcibly sent to the original server. However, if the neighbor cache is not accessible, this may happen. No_cache rules are also troublesome. Here, you must use deny for requests that do not need to be cached.

6.2.2 how to match an access rule with Squid

Recall the "or" logic used by squid to search for ACL elements. Any single value in the acl can cause a match.

However, access rules are the opposite. For http_access and other rule settings, squid uses the "and" logic. Consider the following example:
Access_list allow ACL1 ACL2 ACL3

For this matching rule, the request must match any of ACL1, ACL2, and ACL3. If any of these ACLs does not match a request, squid stops searching for the rule and continues to process the next one. For a rule, placing the least-matched ACL in the first place can optimize the efficiency. Consider the following example:
Acl A method http
Acl B port 8080
Http_access deny A B

The http_access rule is A little inefficient because a acl seems easier to match than B ACL. The reverse order should be better so that squid only checks one ACL, instead of two:
Http_access deny B

A typical mistake that is easy to make is to write rules that are never correct. For example:
Acl A src 1.2.3.4
Acl B src 5.6.7.8
Http_acc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.