Jupyter notebook keeps reconnecting to kernel

2020-04-07 05:45发布

问题:

I get to open the Jupyter console without any problems, but when I create a new notebook it keeps connecting to and disconnecting from the kernel (the messages "Connecting to Kernel" / "Connected" keep showing in the upper right corner). This is what Chrome's console spits out (it's the same in Firefox):

Untitled3.ipynb?kernel_name=python3:121 loaded custom.js
default.js:48Default extension for cell metadata editing loaded.
rawcell.js:82Raw Cell Format toolbar preset loaded.
slideshow.js:43Slideshow extension for metadata editing loaded.
menubar.js:240actions jupyter-notebook:find-and-replace does not exist, still binding it in case it will be defined later...
MenuBar.bind_events @ menubar.js:240
extension.js Failed to load resource: the server responded with a status of 404 (Not Found)
main.js:184Widgets are not available.  Please install widgetsnbextension or ipywidgets 4.0
(anonymous) @ main.js:184
session.js:54Session: kernel_created (1b236a4b-902d-4b33-9118-63013be4f270)
kernel.js:456Starting WebSockets: ws://[myipaddress]:[myport]/api/kernels/682dc980-d7c6-41e0-b984-14ceb7f8e50c
kernel.js:101Kernel: kernel_connected (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:101Kernel: kernel_disconnected (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:559Connection lost, reconnecting in 1 seconds.
kernel.js:101Kernel: kernel_reconnecting (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:456Starting WebSockets: ws://[myipaddress]:[myport]/api/kernels/682dc980-d7c6-41e0-b984-14ceb7f8e50c
kernel.js:101Kernel: kernel_connected (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:101Kernel: kernel_disconnected (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:559Connection lost, reconnecting in 1 seconds.
kernel.js:101Kernel: kernel_reconnecting (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:456Starting WebSockets: ws://[myipaddress]:[myport]/api/kernels/682dc980-d7c6-41e0-b984-14ceb7f8e50c
kernel.js:101Kernel: kernel_connected (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:101Kernel: kernel_disconnected (682dc980-d7c6-41e0-b984-14ceb7f8e50c)
kernel.js:559Connection lost, reconnecting in 1 seconds.
# ... more of the same, over and over ... #

Thing is, everything works fine when I create a notebook on the same machine that runs the Jupyter server (a MacBook I keep at home). The problem happens when I create a notebook from a different machine (a PC running Windows that I use at my company). What could be going on?

回答1:

I'm using jupyter behind a nginx proxy. I met the same problem as you are. After drill down, I find the problem is existed in my nginx conf.

After adding the following line to my nginx conf, it works!

proxy_http_version 1.1;

Here's the complete nginx conf:

upstream my-notebook-workhorse {
  server 127.0.0.1:8888 fail_timeout=0;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

# let my-notebook deal with the redirection
server {
  listen                    80;
  server_name               my-notebook.wh;
  server_tokens             off;
  root                      /dev/null;

  # Increase this if you want to upload larger attachments
  client_max_body_size      20m;

  # individual nginx logs for this vhost
  access_log                /var/log/nginx/my-notebook_access.log;
  error_log                 /var/log/nginx/my-notebook_error.log;

  location / {
    proxy_pass http://my-notebook-workhorse;
    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;
    auth_basic "Restricted Content";

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Origin "";
    proxy_read_timeout 86400;
  }
}

I don't known why it happens since the old version without proxy_http_version 1.1; worked well in last few months before I met the issue.



回答2:

I've just changed the port from 8888 to 9999, and the problem is gone.

use the command

jupyter notebook --generate-config

(it says where the generated config file is)

to generate a config file, then find the line

c.NotebookApp.port

and change the port.