【问题标题】:nginx 'invalid number of arguments in "map" directive'nginx'“map”指令中的参数数量无效'
【发布时间】:2017-02-19 11:16:02
【问题描述】:

我正在尝试反向代理一个 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,以防您想复制我的设置(相对于 Dockerfile,将 default.conf 保存为 conf.templates/default.conf

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

【问题讨论】:

    标签: nginx configuration reverse-proxy


    【解决方案1】:

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

    【讨论】:

    • 你是救生员!!另外,这种情况常见吗?这就像我的搜索查询的第一个结果,我什至没有在查询中输入 docker
    猜你喜欢
    • 2019-01-06
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2016-01-05
    • 2013-06-25
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多