From c4c51106fa58a01c485eb3b1f585a6818296e971 Mon Sep 17 00:00:00 2001 From: George Hunt Date: Sat, 17 Apr 2021 00:05:18 +0100 Subject: [PATCH] get nginx to proxy websockets correctly --- .../templates/jupyterhub-nginx.conf | 42 +++++++++++-------- roles/nginx/templates/nginx.conf.j2 | 9 ++++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/roles/jupyterhub/templates/jupyterhub-nginx.conf b/roles/jupyterhub/templates/jupyterhub-nginx.conf index 301ab2a41..6758458e9 100644 --- a/roles/jupyterhub/templates/jupyterhub-nginx.conf +++ b/roles/jupyterhub/templates/jupyterhub-nginx.conf @@ -1,20 +1,26 @@ -location /jupyterhub { - proxy_pass http://127.0.0.1:8000; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-NginX-Proxy true; +location /jupyterhub { + proxy_pass http://127.0.0.1:8000; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-NginX-Proxy true; + } - # websocket headers - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - #proxy_set_header Connection $connection_upgrade; - proxy_set_header X-Scheme $scheme; +# Managing WebHook/Socket requests between hub user servers and external proxy + location ~* /(api/kernels/[^/]+/(channels|iopub|shell|stdin)|terminals/websocket)/? { + proxy_pass http://127.0.0.1:8000; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_buffering off; -} - -# Managing requests to verify letsencrypt host -location ~ /.well-known { - allow all; -} + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + #proxy_set_header Connection $connection_upgrade; + proxy_set_header X-Scheme $scheme; + proxy_buffering off; + } + # Managing requests to verify letsencrypt host + location ~ /.well-known { + allow all; + } diff --git a/roles/nginx/templates/nginx.conf.j2 b/roles/nginx/templates/nginx.conf.j2 index 10f33f72f..10fc4cfda 100644 --- a/roles/nginx/templates/nginx.conf.j2 +++ b/roles/nginx/templates/nginx.conf.j2 @@ -67,6 +67,15 @@ http { # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; + + # top-level http config for websocket headers + # If Upgrade is defined, Connection = upgrade + # If Upgrade is empty, Connection = close + map $http_upgrade $connection_upgrade { + default upgrade; + '' close; + } + ## # Virtual Host Configs ##