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

Hildegard Meier daku8938 at gmx.de
Fri Sep 16 14:22:40 UTC 2022


I found about

man squid

-f file     Use the given config-file instead of /etc/squid/squid.conf .  If the file name starts with a !  or | then it is assumed  to  be
                   an  external  command  or  command  line.   Can for example be used to pre-process the configuration before it is being read by
                   Squid.  To facilitate this Squid also understands the common #line notion to indicate the real source file.

While I dnot know what "common #line notion" should be (searching on internet matches only the squid man page),

I found the following solution for Ubuntu 18.04 server (Squid 3.5.27):

Create a script
--------------------
/usr/local/script/squid/pre_process_squid_config_file.sh

#!/bin/bash

readonly SQUID_CONFIG_TEMPLATE_FILE='/etc/squid/squid.conf.TEMPLATE'

case "${HOSTNAME}" in
    'node1')  readonly HOSTNAME_PEER='node2' ;;
    'node2')  readonly HOSTNAME_PEER='node1' ;;
    *)
        echo "invalid hostname in script ${0}. Exit"
        exit 1
        ;;
esac

sed \
-e "s@{HOSTNAME}@${HOSTNAME}@g" \
-e "s@{HOSTNAME_PEER}@${HOSTNAME_PEER}@g" \
"${SQUID_CONFIG_TEMPLATE_FILE}"
--------------------

Create file /etc/default/squid with content
CONFIG='|/usr/local/script/squid/pre_process_squid_config_file.sh'
SQUID_ARGS="-YC -f $CONFIG"

Now you can use the Macros {HOSTNAME} (which will give the actual hosts name) and {HOSTNAME_PEER} (name of the other host)

I have only one problem with it, is that a bug?

/usr/sbin/squid -k parse -f '|/usr/local/script/squid/pre_process_squid_config_file.sh'

works, but

/usr/sbin/squid -k reconfigure -f '|/usr/local/script/squid/pre_process_squid_config_file.sh'

gives the following line in /var/log/squid/cache.log

2022/09/16 16:20:55 kid1| storeDirWriteCleanLogs: Starting...
2022/09/16 16:20:55 kid1|   Finished.  Wrote 0 entries.
2022/09/16 16:20:55 kid1|   Took 0.00 seconds (  0.00 entries/sec).
FATAL: parseConfigFile: '|/usr/local/script/squid/pre_process_squid_config_file.sh' failed with exit code -1

Squid Cache (Version 3.5.27): Terminated abnormally.
CPU Usage: 1.267 seconds = 0.760 user + 0.507 sys
Maximum Resident Size: 107296 KB
Page faults with physical i/o: 7
2022/09/16 16:20:58 kid1| Current Directory is /



More information about the squid-users mailing list