Skip to content

Instantly share code, notes, and snippets.

@cboettig
Last active December 31, 2023 15:23
Show Gist options
  • Save cboettig/8643341bd3c93b62b5c2 to your computer and use it in GitHub Desktop.
Save cboettig/8643341bd3c93b62b5c2 to your computer and use it in GitHub Desktop.
debugging NGINX configuration for Jupyter
jupyter:
image: jupyter/datascience-notebook
environment:
- PASSWORD=${PASSWORD}
nginx:
image: nginx
links:
- jupyter
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./letsencrypt.crt:/data/cert.crt
- /letsencrypt.key:/data/key.key
ports:
- 80:80
- 443:443
## Based on: https://github.com/calpolydatascience/jupyterhub-deploy-data301/blob/master/roles/nginx/templates/nginx.conf.j2
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream jupyter {
server jupyter:8888 fail_timeout=0;
}
server {
listen 80;
server_name xsede.carlboettiger.info;
rewrite ^ https://$host$request_uri? permanent;
}
server {
listen 443;
client_max_body_size 50M;
server_name xsede.carlboettiger.info;
ssl on;
ssl_certificate /data/cert.crt;
ssl_certificate_key /data/key.key;
ssl_ciphers "AES128+EECDH:AES128+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
add_header X-Content-Type-Options nosniff;
ssl_stapling on; # Requires nginx >= 1.3.7
ssl_stapling_verify on; # Requires nginx => 1.3.7
resolver_timeout 5s;
# Expose logs to "docker logs".
# See https://github.com/nginxinc/docker-nginx/blob/master/Dockerfile#L12-L14
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
proxy_pass http://jupyter;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* /(api/kernels/[^/]+/(channels|iopub|shell|stdin)|terminals/websocket)/? {
proxy_pass http://jupyter;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}
@xros
Copy link

xros commented Apr 4, 2016

Thanks this works!

@morphean
Copy link

what about just serving staring from / instead of ~?

where are the static files actually served from?

@dolvik
Copy link

dolvik commented Mar 12, 2017

Thanks. It helped

@javrasya
Copy link

Thank you, it works like a charm.

@vaayne
Copy link

vaayne commented Sep 4, 2017

Thanks, It works for me

@coder4web
Copy link

Thanks, works with nginx mainline and JH 0.8.1

@afragop72
Copy link

Thanks for this - that worked for me, as well.

@oharach1
Copy link

oharach1 commented Sep 25, 2020

Hi, does anyone has been trying transfer the NGINX user uid header to jupyternotebook session? I mean something like to identify the authenticated user on the notebook session.

Thanks,

@hannes
Copy link

hannes commented Feb 4, 2021

Dude you saved our ass here just now. Thanks :)

@aabdulsalams
Copy link

Thank you!. You've saved my time :)))))))))

@v01d-gh
Copy link

v01d-gh commented Oct 9, 2022

Thank you!

@Pk13055
Copy link

Pk13055 commented Mar 25, 2023

Still works as of 2023, however note that on Safari, the kernel will seem like it's restarting continuously, while still generating the right cell output(s). If Firefox or Chrome is installed, better use that.

@dmikushin
Copy link

I've got many errors of the form below with the container, as well as with the official container. I turn of SSL:

WebSocket connection to 'ws://localhost/api/events/subscribe?token=9fe2a91c775e50ddc0535cf0cd55c58b38919586caf08470' failed: 

Does anybody have the same problem, and perhaps a solution?

@alesito85
Copy link

@dmikushin have you found a solution?

@dmikushin
Copy link

dmikushin commented Aug 30, 2023

@alesito85 , kind of. First of all, not all websocket errors are an indication of issue. For example, if connection to the Jupyter website has been established, but then lost due to the internet or network interruption, then these errors are presented naturally, as for any socket connection that becomes broken. If Docker container has been restarted, this is also a legitimate cause of websocket errors. So these errors may be attributed to Nginx configuration only given stable networking conditions. Be sure to refresh the page first, to clear out the old errors.

Instead of carrying out an own Nginx configuration, please first take a look at the official one, as it might have changed in the newer versions of Jupyter.

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