2016-10-10 74 views
0

我試圖反向代理一個websocket,我之前用nginx做了一個沒有問題的代理。奇怪的是,我似乎無法用這麼簡單的事情重新創造我的成功。我一直在配置文件,但似乎無法找到我的錯誤。nginx'map'指令中參數的無效數

這裏是我的全部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 as 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;'" 

回答

5

envsubst命令替換所有發生的$vars,包括$http_upgrade$connection_upgrade。 你應該提供的變量列表被替換,如:

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

參見:Replacing only specific variables with envsubst

此外,進入docker-compose配置應以禁用變量替換使用雙$$逃生:

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