Guide

How to run Squid as a Windows service on Windows Server

A proxy has to start with the server, come back after a failure and keep running with nobody signed in. On Windows that means a service. Squid was written for Unix, where it runs as a daemon, so on Windows Server it needs some help. This guide covers the ways to run it as a service, the settings that matter, and what goes wrong.

Why Squid needs a wrapper on Windows

A Windows service is a program that talks to the Service Control Manager: it reports when it has started, and it stops when asked. Squid's source has a Windows service mode (squid -i installs it), but only in builds compiled with --enable-win32-service. Most current Squid builds for Windows are made with Cygwin and leave it out; run squid -h and, if there is no -i in the list, your build is one of them.

Such a build runs as a service only under a wrapper: a small program that is the service, starts squid.exe as its child, and stops it when Windows asks.

Ways to run Squid as a service

  • Cygwin's cygrunsrv. If you built Squid in a full Cygwin installation, Cygwin's own service wrapper can run it. From a Cygwin shell run as administrator, with the paths adjusted to your build:
    cygrunsrv --install squid --disp "Squid proxy" \
      --path /usr/sbin/squid --args "-N -f /etc/squid/squid.conf"
    Then start it with net start squid. -N keeps Squid in the foreground: without it Squid starts a background process and the first one exits, and the wrapper takes that as the service stopping.
  • A general-purpose service wrapper. Any wrapper that can run a console program as a service works the same way: point it at squid.exe -N -f <squid.conf>, and have it stop Squid with squid.exe -k shutdown -f <squid.conf> rather than by ending the process, so Squid closes its cache cleanly.
  • A distribution with its own service. Squid Proxy Professional (this site) ships a service program for its Squid build, described below.

Before the first start, create the cache folders with squid.exe -z -f <squid.conf>. Squid does not create the cache_dir structure on its own, and a service that exits at once because of it shows nothing more than a stopped state.

Start-up, recovery and stopping

Start type. Set the service to Automatic, so the proxy starts with the server:

sc config squid start= auto

Recovery. Tell Windows to restart the service if it stops on its own. This restarts it 5 seconds after the first and second failure, and counts failures from zero again after a day:

sc failure squid reset= 86400 actions= restart/5000/restart/5000

Keep the number of restarts small. A configuration error or a port already in use fails again at once, and endless restarts only fill the event log.

Stopping. On a stop request Squid waits for open connections to finish, for up to shutdown_lifetime, which is 30 seconds by default. Windows expects a service to stop sooner than that, and long downloads keep connections open, so set a shorter time in squid.conf:

shutdown_lifetime 3 seconds

Service account and permissions

Wrappers run as Local System unless you choose another account. Whichever account runs Squid needs to read squid.conf and the program folder, and to write the cache folder, the log folder and the folder that holds Squid's pid file (pid_filename).

The logs record every address your users visit, and the configuration may hold passwords or internal names. Keep the data folders readable by administrators and the service account only, not by every local user.

For Windows domain sign-on (Kerberos or NTLM), the server must be a member of the domain. Run as Local System, Squid's sign-on helpers use the computer's own domain account. See Windows domain sign-on.

What goes wrong with a Squid service

  • It starts by hand but not as a service. The service runs with a different account, a different working folder and no mapped drives. Use full paths everywhere in squid.conf, and never a mapped drive letter.
  • Paths with spaces. Squid cuts a value at the first space, so a path under C:\Program Files breaks unless it is quoted and configuration_includes_quoted_values on comes first. The annotated squid.conf shows how.
  • No DNS. A Cygwin Squid reads its DNS servers from /etc/resolv.conf, which Windows does not have. Without it, or a dns_nameservers line in squid.conf, every request fails to resolve. A fixed dns_nameservers line also has to be changed by hand when your DNS servers change.
  • Unsigned DLLs. Smart App Control and application-control policies such as App Control for Business refuse unsigned DLLs, even in a signed program. A Squid service that ships unsigned Cygwin DLLs stops starting when such a policy turns on. The CodeIntegrity log in Event Viewer names the blocked file.
  • Logs that never rotate. Squid rotates its logs only when told to with squid -k rotate, and on Windows nothing tells it. Schedule it, for example with Task Scheduler, or access.log grows until the disk is full.
  • Stale shared memory. After a crash or a forced stop, a Cygwin Squid can leave shared-memory files behind that stop the next start. They live in the build's dev\shm folder and can be deleted while Squid is stopped.

How Squid Proxy Professional runs Squid as a service

Squid Proxy Professional includes its own Windows service for Squid, so none of the work above is left to you. The installer copies the files; the console's Setup Wizard writes and checks squid.conf, then installs the service and starts it.

Service nameSquidProxyProfessional (display name Squid Cache)
Start typeAutomatic, and Windows restarts it if it fails
ConfigurationC:\Program Files\Squid Proxy Professional\squid\etc\squid.conf
Cache, logs, backupsC:\ProgramData\SquidProxyPro, open to SYSTEM and Administrators only

The service takes care of the problems listed above:

  • Squid uses the server's Windows DNS servers and follows them when they change, for example after a VPN connects. There is no dns_nameservers line to keep up to date.
  • The logs rotate on the schedule set in the console: daily, weekly or by size. No scheduled task is needed.
  • If Squid stops unexpectedly after it has started serving, the service starts it again, and leftovers from a crash do not stop the next start.
  • A stop or restart takes a few seconds.
  • Every file the installer puts on the machine, the Cygwin DLLs included, is code-signed, so the service starts under Smart App Control and application-control policies.

Managing the service

From PowerShell:

Get-Service     SquidProxyProfessional
Restart-Service SquidProxyProfessional

The service writes to the Windows Application event log under the source Squid, and Event Viewer shows each entry's full text. The documentation covers service control and troubleshooting in more detail, and the install guide covers the steps before this one.

Try it

Squid as a Windows service, set up for you.

The 30-day evaluation is the full product, and starts on activation.