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

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

我正在尝试反向代理一个 websocket,我之前用 nginx 做过,没有问题。奇怪的是,我似乎无法用如此简单的东西重现我之前的成功。我一遍又一遍地检查配置文件,但似乎找不到我的错误。

这是我的完整 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;
  }
}

我遇到的错误:

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

以及我正在使用的确切 Dockerfile,以防您想复制我的设置(将 default.conf 保存为 conf.templates/default.conf 相对于 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;'"

envsubst 命令替换所有出现的 $vars,包括 $http_upgrade$connection_upgrade。 您应该提供要替换的变量列表,例如:

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

另请参阅:Replacing only specific variables with envsubst

此外,在 Dockerfile 配置中,您应该使用双 $$ 转义以禁用变量替换:

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;'"