From 5f8931c2317064c11732a16b2f347bfca6f16665 Mon Sep 17 00:00:00 2001 From: Michael Krickl Date: Tue, 2 Feb 2021 22:20:46 +0100 Subject: [PATCH] Improve Connection header for WebSocket reverse proxy (#217) * Improve Connection header for WebSocket reverse proxy reference: https://nginx.org/en/docs/http/websocket.html * Move map to http context --- src/nginxconfig/generators/conf/nginx.conf.js | 9 +++++++++ src/nginxconfig/generators/conf/proxy.conf.js | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/nginxconfig/generators/conf/nginx.conf.js b/src/nginxconfig/generators/conf/nginx.conf.js index b0b5c33..cd55566 100644 --- a/src/nginxconfig/generators/conf/nginx.conf.js +++ b/src/nginxconfig/generators/conf/nginx.conf.js @@ -190,6 +190,15 @@ export default (domains, global) => { } } + // Connection header for WebSocket reverse proxy + if (domains.some(d => d.reverseProxy.reverseProxy.computed)) { + config.http.push(['# Connection header for WebSocket reverse proxy', '']); + config.http.push(['map $http_upgrade $connection_upgrade', { + 'default': 'upgrade', + '""': 'close', + }]); + } + // Configs! config.http.push(['# Load configs', '']); config.http.push(['include', [ diff --git a/src/nginxconfig/generators/conf/proxy.conf.js b/src/nginxconfig/generators/conf/proxy.conf.js index 3f136f3..b89bf03 100644 --- a/src/nginxconfig/generators/conf/proxy.conf.js +++ b/src/nginxconfig/generators/conf/proxy.conf.js @@ -32,7 +32,7 @@ export default global => { config['# Proxy headers'] = ''; config['proxy_set_header Upgrade'] = '$http_upgrade'; - config['proxy_set_header Connection'] = '"upgrade"'; + config['proxy_set_header Connection'] = '$connection_upgrade'; config['proxy_set_header Host'] = '$host'; config['proxy_set_header X-Real-IP'] = '$remote_addr'; config['proxy_set_header X-Forwarded-For'] = '$proxy_add_x_forwarded_for';