我正试图反向代理一个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
参见:用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;'"