1

I have this unit file for running a service under systemd.

[Unit]
Description=Varnish HTTP accelerator log daemon
After=varnish.service

[Service]
User=root
Environment="LOG_FORMAT='\"%{X-Forwarded-For}i\" %u %t \"%r\" %s %b \"%{Referer}i\ %{User-agent}i\"'"
ExecStart=/usr/bin/varnishncsa -a -w /var/log/www/www.my_website_varnish.log -q "ReqHeader ~ '^Host: www.my_website.fr'" -F $LOG_FORMAT
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

As you can see, I'm defining a log format in a variable and passing that on the command line to the program. This isn't working.

When I start the service, its actual command line that it was invoked with looks like this:

   CGroup: /system.slice/varnishncsa.service
           └─60950 /usr/bin/varnishncsa -a -w /var/log/www/www.my_website_varnish.log -q ReqHeader ~ '^Host: www.my_website' -F "%{X-Forwarded-For}i" "root" /run "" /bin/sh ffd205c41b1e4c1088dac...

As you can see, systemd has expanded %u, %t, %r and others itself, before passing them on the command line to my program. This is wrong, and not what is desired.

For what it's worth, I'm following the instructions at https://wiki.deimos.fr/Nginx_%2B_Varnish_:_Cache_even_in_HTTPS_by_offloading_SSL for setting up Varnish. But this problem is one of getting the daemon to be run with the right command line arguments in the first place, and not one in my Varnish configuration.

I'm trying to change the log format with the -F, by the way, because by default I'm getting 127.0.0.1 in my logs as the visitor IP address, which is the IP address of nginix. But again that is incidental. I might want to use percent signs and variables at other places in my command line.

How can I specify something on the program command line in ExecStart and prevent systemd from doing its own parameter expansion on anything with percent signs in it?

JdeBP
  • 66,967
  • 12
  • 159
  • 343
Youlou
  • 71
  • 6

1 Answers1

5

The answer turns out to be quite simple. I just "escape" the parameterization by doubling up the percent signs:

Environment="LOG_FORMAT='\"%{X-Forwarded-For}i\" %%u %%t \"%%r\" %%s %%b \"%{Referer}i\ %{User-agent}i\"'"

systemd's parameter expansion turns %% into %, which means that my program sees %%u as %u and things work as I want.

This is in the Specifiers section of the systemd.unit manual page.

JdeBP
  • 66,967
  • 12
  • 159
  • 343
Youlou
  • 71
  • 6