nginx 'invalid number of arguments in "map" directive'

Ryan Kennedy picture Ryan Kennedy · Oct 11, 2016 · Viewed 12.8k times · Source

I'm trying to reverse-proxy a websocket, which I've done with nginx before with no issue. Weirdly, I can't seem to re-create my prior success with something so simple. I've been over and over the config file but can't seem to find my error.

Here's my full default.conf:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
  listen 80;

  location /api/ {
    proxy_pass ${API_LOCATION};
  }

  location / {
    proxy_pass ${UI_LOCATION};
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  }
}

The error I'm getting:

2016/10/10 23:30:24 [emerg] 8#8: invalid number of arguments in "map" directive in /etc/nginx/conf.d/default.conf:1
nginx: [emerg] invalid number of arguments in "map" directive in /etc/nginx/conf.d/default.conf:1

And the exact Dockerfile that I'm using, in case you want to replicate my setup (save default.conf as conf.templates/default.conf relative to the Dockerfile:

FROM nginx
COPY conf /etc/nginx/conf.templates
CMD /bin/bash -c "envsubst < /etc/nginx/conf.templates/default.conf > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

Answer

Vittorio Palmisano picture Vittorio Palmisano · Nov 4, 2016

The envsubst command replaces all occurrences of $vars, including $http_upgrade and $connection_upgrade. You should provide a list of variables to be replaced, e.g.:

 envsubst '${API_LOCATION},${UI_LOCATION}' < /etc/nginx/conf.templates/default.conf

See also: Replacing only specific variables with envsubst

Moreover, into the Dockerfile configuration you should use the double $$ escape in order to disable the variable substitution:

FROM nginx
COPY conf /etc/nginx/conf.templates
CMD /bin/bash -c "envsubst '$${API_LOCATION},$${UI_LOCATION}' < /etc/nginx/conf.templates/default.conf > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"