EC2 上的 Jupyter:SSL 错误

Jupyter on EC2: SSL Error

我正在尝试从 EC2 提供 Jupyter notebook,但出现 SSL 错误。 我正在使用这个 AMI:TensorFlow GPU - @nottombrown (ami-8ed4d0e4)。我使用了自签名证书,这可能是问题的一部分。

我看过其他几个有类似错误的 SO 帖子,例如 this, this and this,但他们在完全不同的上下文中遇到错误,我不知道如何获取适合我的解决方案(例如设置 verify=False)。

[E 15:52:44.954 NotebookApp] Exception in callback (<socket._socketobject object at 0x7f5f993dad00>, <function null_wrapper at 0x7f5f99319758>)
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 883, in start
        handler_func(fd_obj, events)
      File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
        return fn(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/tornado/netutil.py", line 274, in accept_handler
        callback(connection, address)
      File "/usr/local/lib/python2.7/dist-packages/tornado/tcpserver.py", line 239, in _handle_connection
        do_handshake_on_connect=False)
      File "/usr/local/lib/python2.7/dist-packages/tornado/netutil.py", line 521, in ssl_wrap_socket
        return ssl.wrap_socket(socket, **dict(context, **kwargs))
      File "/usr/lib/python2.7/ssl.py", line 487, in wrap_socket
        ciphers=ciphers)
      File "/usr/lib/python2.7/ssl.py", line 241, in __init__
        ciphers)
    SSLError: [Errno 336265225] _ssl.c:355: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib

这个错误表示找不到SSL证书。

如果您还没有创建 SSL 证书:

通过在命令行中输入以下内容来创建一个(从您的主目录开始):

$ mkdir certs
$ cd certs
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

(来自 these instructions

现在您已拥有 SSL 证书:

您需要告诉 Jupyter 在哪里可以找到它。

看看 jupyter_notebook_config.py 是否有(或使用 jupyter notebook --generate-config 生成一个)。

确保您有以下行:

c.NotebookApp.certfile = u'/home/ubuntu/certs/mycert.pem' #location of your certificate file

(参见 the Jupyter Notebook docs

并确保您的自签名 SSL 证书确实位于该位置。例如,一个常见的错误是将 certs.certs 混淆。像这样的错误将导致 Jupyter 无法找到您的 SSL 证书,这将导致您收到错误。

我在 运行 jupyter 笔记本作为 public 服务器时遇到了同样的错误,在 Unbuntu 14.4 下具有自签名证书。在我的例子中,问题是由于 jupyter_notebook_config.py 文件中的错误造成的。我错误地设置了 c.NotebookApp.client_ca = u'/../mycert.pem'。注释掉这一行解决了问题。

尝试添加 https:// 以强制 Web 浏览器使用 HTTPS 连接。

我得到了:

[W 08:25:56.148 NotebookApp] SSL Error on 9 ('<jupyter-server-ip>', 62862): [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:590)

因为 Chrome 尝试使用 HTTP 连接。使用

https://<jupyter-server-ip> 

终于成功了:)

出现这个错误有两个原因:

  1. 证书问题
$ mkdir certs
$ cd certs
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.pem -out mycert.pem
  1. 错误的 http(s) 调用。

    • https://ec2-ip:8888 |使用 https

    • 获取ec2-ip curl http://checkip.amazonaws.com

我发现出现权限问题是因为 mycert.pem 文件只有 root 权限

drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 20 12:22 .
drwxr-xr-x 9 ubuntu ubuntu 4096 Mar 20 12:09 ..
-rw------- 1 root   root   2949 Mar 20 12:22 mycert.pem

我不能运行 jupyter notebook root 所以我chowned权限

sudo chown ubuntu:ubuntu mycert.pem

这解决了问题