squid outgoing ip patch

Some time ago a client of mine was running squid on a system with multiple interfaces and ips. He wanted a setup where squid would use the same ip on which it received the connection to create the connection to the destination server for fetching the content.

Let's say we have 3 ips 1.1.1.1, 1.1.1.2 and squid listens on both interfaces. Now if a client would connect to the proxy server on ip 1.1.1.1 and request google.com, squid will use 1.1.1.1 to make the connection to google. If the client will connect to 1.1.1.2 squid will use 1.1.1.2 to connect to the destination server.


One way to do this would be using acls and tcp_outgoing_address

acl acl_ip1 src 1.1.1.1
acl acl_ip2 src 1.1.1.2
tcp_outgoing_address 1.1.1.1 acl_ip1
tcp_outgoing_address 1.1.1.2 acl_ip2

This is easy to do if you have just a few ips, but if you have a few hundred ips you get tired of writing those rules, and even if you generate them with a script I don't think squid would really like that many configuration options. It will probably take a lot of time to parse them and probably the performance will suffer on high traffic servers.So I came up with a patch to squid's source code that does this automatically without any need for setting up acls and tcp_outgoing_address. You are still able to use tcp_outgoing_address after applying this patch, because the patch is active only for those connections where the acl of tcp_outgoing_address does not match.

The patch was created for squid 2.5, but I think it can be adapted for 2.6

And here is the actual patch ( gziped ) : squid outgoing ip patch

download it, go to squid source folder and then:

  1.  

6 thoughts on “squid outgoing ip patch

  1. I ended up ghetto rigging an excel spreadsheet to shoot out the needed lines. Might be useful to mention that the acl and then the tco_outgoing don’t need to be immediately one after another.

  2. Thanks for your post. But it seems your solution doesn’t work on 2.7:

    parseConfigFile: squid.conf:1267 unrecognized: ‘acl_ip1’
    parseConfigFile: squid.conf:1268 unrecognized: ‘acl_ip2’
    ACL name ‘acl_ip1’ not defined!
    FATAL: Bungled squid.conf line 1269: tcp_outgoing_address 1.1.1.2 acl_ip1
    Squid Cache (Version 2.7.STABLE3): Terminated abnormally.
    failed!

    So I had to modified

    acl_ip1 src 1.1.1.2
    acl_ip2 src 1.1.1.3

    to

    acl acl_ip1 src 1.1.1.2
    acl acl_ip2 src 1.1.1.3

    But finaly, it still doesn’t work. Squid will use the default route (1.1.1.1) instead of one of the defined outgoing interfaces. Any ideas? Configuration looks like this:

    acl acl_ip1 src 1.1.1.2
    acl acl_ip2 src 1.1.1.3
    tcp_outgoing_address 1.1.1.2 acl_ip1
    tcp_outgoing_address 1.1.1.3 acl_ip2

  3. You’re right about the acl definition. It was an error in my post. Thanks for letting me know.

    As for the outgoing interface. Are you actually connecting to 1.1.1.2 or 1.1.1.3 and it still uses the main ip as outgoing ?

Leave a Reply to MihaiCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.