[squid-users] Howto make Squid config dependent on hostname?

Alex Rousskov rousskov at measurement-factory.com
Thu Sep 15 18:06:37 UTC 2022


On 9/15/22 10:29, Hildegard Meier wrote:

> we have two Squid servers (Linux hosts) and each shall have the very same config file /etc/squid/squid.conf
> which is versioned and deployed from a central deployment server.
> So each host shall have deployed the same files.
> 
> Each of the two shall have the other configured as sibling cache peer.
> 
> So node1 shall have
> cache_peer node2.examlpe.com sibling 3128 3130
> 
> and node2 shalle have
> cache_peer node1.examlpe.com sibling 3128 3130
> 
> I guess it is not so nice to have both configured with both lines together, no?
> 
> cache_peer node1.examlpe.com sibling 3128 3130
> cache_peer node2.examlpe.com sibling 3128 3130

 > I find that ugly.

I agree.

Squid does not support being its own peer (yet?): Upon startup, Squid 
validates connectivity to peers. If that validation happens before Squid 
starts listening for requests, the validation will (silently) fail. 
There are several race conditions here, but that failure is likely in a 
self-peering case.

In your particular use case, such self-peering also does not reflect 
reality. You do not want to lie to Squid about it being its own peer 
because Squid will try to use both configured peers, creating problems 
that you do not want to solve. It also duplicates peer configurations.


> Does squif offer a hostname conditional if clause ?

Not yet. Squid currently only supports three kinds of conditions: true, 
false, and integer equality comparison (as documented).

The only build-in macro that always yields an integer is the 
${process_number} macro.

Depending on your setup, you may also be able to use the ${service_name} 
macro if your service name is an integer, but that is kind of hacky, and 
I have not tested it.

For now, your best option is probably to post-process the versioned 
configuration to substitute your own custom macro(s). For example, your 
Squid startup (or deployment) stript can substitute a 
${mynamespace:PeerName} macro in squid.conf.template with either true or 
false, depending on the node, producing squid.conf that Squid can use:

cache_peer ${mynamespace:PeerName} sibling 3128 3130


If you need to hard-code peer names or substantially different 
configurations for individual cache peers, then you can do something 
like this in the template configuration file:

if ${mynamespace:AtNode1()}
     cache_peer node2.example.com sibling 3128 3130
endif

if ${mynamespace:AtNode2()}
     cache_peer node1.example.com sibling 3128 3130
endif


FWIW, quality PRs introducing the following related features should be 
welcomed IMO:

* a macro function evaluating a named environment variable
* a macro function evaluating a given shell command
* string comparison in squid.conf conditionals


HTH,

Alex.


> I think about something like this:
> 
> if $MY_HOSTNAME == 'node1' then
>      cache_peer node2.examlpe.com sibling 3128 3130
> fi
> 
> if $MY_HOSTNAME == 'node2' then
>      cache_peer node1.examlpe.com sibling 3128 3130
> fi


More information about the squid-users mailing list