Run Jupyter Notebook behind a nginx reverse proxy subpath
Run Jupyter Notebook behind a nginx reverse proxy subpath

The server I want to run Jupyter on has some other apps running on an nginx server, so I'll configure Jupyter to at the path /groot

First, generate the Jupyter config with:

jupyter notebook --generate-config

Generate a sha1 sum of the password you want to use, in an ipython shell:

from notebook.auth import passwd; passwd()

Update the config generated in the --generate-config command

c.NotebookApp.allow_origin = '*'
c.NotebookApp.base_url = '/groot'
c.NotebookApp.password = 'sha1:[put generated password here...]'
c.NotebookApp.port = 8888

In the nginx http block, create an upstream:

upstream upstream_groot {
  server localhost:8888;
  keepalive 32;
}

In your nginx server block, set:

    location = /groot {
      rewrite ^/(.*)$ $1/ permanent;
    }
    location /groot {
      error_page 403 = @proxy_groot;
      deny 127.0.0.1;
      allow all;
      # set a webroot, if there is one
      root /some-webroot;
      try_files $uri @proxy_groot;
    }
    location @proxy_groot {
      #rewrite /groot(.*) $1  break;
      proxy_read_timeout 300s;
      proxy_pass http://upstream_groot;
      # pass some extra stuff to the backend
      proxy_set_header Host $host;
      proxy_set_header X-Real-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location ~ /groot/api/kernels/ {
            proxy_pass            http://upstream_groot;
            proxy_set_header      Host $host;
            # websocket support
            proxy_http_version    1.1;
            proxy_set_header      Upgrade "websocket";
            proxy_set_header      Connection "Upgrade";
            proxy_read_timeout    86400;
        }
    location ~ /groot/terminals/ {
            proxy_pass            http://upstream_groot;
            proxy_set_header      Host $host;
            # websocket support
            proxy_http_version    1.1;
            proxy_set_header      Upgrade "websocket";
            proxy_set_header      Connection "Upgrade";
            proxy_read_timeout    86400;
    }
Hope this was helpful. If so, feel free to checkout my YouTube channel, get updates when a new article is posted by following on Feedly and read the the other guides at nathan.vertile.com/blog
comments powered by Disqus