Skip to content

Instantly share code, notes, and snippets.

@minrk
Last active February 4, 2021 23:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/c373f660b3fece67b5c950e29cf16830 to your computer and use it in GitHub Desktop.
Save minrk/c373f660b3fece67b5c950e29cf16830 to your computer and use it in GitHub Desktop.
notebook docker proxy
# apache httpd.conf
# equivalent to nginx.conf, but allows us to test with apache as well
# in case it behaves differently (hint: it does)
# docker run -d --name nbproxy-apache -p 8088:80 -v "$PWD"/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:2.4
Include conf/original/httpd.conf
ServerName 127.0.0.1
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule rewrite_module modules/mod_rewrite.so
Define notebook_ip 192.168.99.1
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /(.*) ws://${notebook_ip}:8888/$1 [P,L]
<Location "/">
ProxyPreserveHost on
ProxyPassReverse http://${notebook_ip}:8888/
ProxyPass http://${notebook_ip}:8888/
</Location>
# nginx proxy configuration for notebooks
# used for connectivity tests
# notebook server must be on an accessible ip from the docker container
# this can be tricky when docker is in a vm (e.g. macOS)
# but `ifconfig en0 | grep "inet " | cut -d " " -f2` will get you a probable public ip
# you can also use vmnet ips, etc. Anything that will resolve in docker.
# On Linux, you can use --net host and 127.0.0.1 for $notebook_ip
# start the notebook:
# jupyter notebook --ip=$docker_accessible_ip
# start the proxy:
# docker run -d --name nbproxy -p8080:8080 -v "$PWD/nginx.conf":/etc/nginx/conf.d/default.conf:ro nginx
# connect to server at http://127.0.0.1:8080
# to drop connections:
# docker stop nproxy
# to resume connections:
# docker start nproxy
# websockets
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# proxy
server {
set $notebook_ip 192.168.99.1;
listen 8080;
server_name 127.0.0.1;
location / {
proxy_pass http://$notebook_ip:8888;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# websocket headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
@minrk
Copy link
Author

minrk commented Oct 4, 2017

Some notes:

docker stop nbproxy-apache doesn't have the same behavior as killing an interactive apache proxy, so there's an additional test case of:

docker run --rm -it -p 8088:80 -v "$PWD"/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:2.4

and interrupting the process to sever the connection

The interactive case seems to trigger kernel_dead for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment