Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run IPython behind an Apache proxy

I would like to run an IPython notebook web server behind an Apache (reverse) proxy so that instead of the URL

https://my.server:XXXX

(where XXXX is some port number) I could use

https://my.server/py0

I am aware that IPython uses websockets and I suspect this is the part that is missing from my setup, but I simply could not find a suitably detailed description on how to configure this. Unfortunately the IPython webserver setup docs don't have much to say regarding proxies apart from this:

When behind a proxy, especially if your system or browser is set to autodetect the proxy, the notebook web application might fail to connect to the server’s websockets[...]

So I decided to try it on my own and put the following in /etc/apache2/sites-enabled/default-ssl.conf :

            SSLProxyEngine On
            SSLProxyVerify none
            SSLProxyCheckPeerCN off
            SSLProxyCheckPeerName off
            ProxyPass       /py0/ https://localhost:10000/
            ProxyPassReverse        /py0/ https://localhost:10000/

Accessing IPython "directly" over the URL https://my.server:10000 works perfectly as advertised.

The URL https://my.server/py0 (without a trailing slash) returns "404 Not found".

The same with a trailing slash https://my.server/py0/ does "work" in that it forwards to https://my.server/login?next=%2F, which is then "Not found" in its own right -- obviously because the /py0/ part got lost. Maybe I should tell IPython about it but how ??

Perhaps relevant version numbers: Ubuntu 14.04 LTS, Apache 2.4.7.

Perhaps relevant SO question: IPython behind nginx. However, since everything else in my setup is handled by Apache to my full satisfaction, I do not want to run Nginx in addition.

Is there any good soul out there who has successfully configured IPython notebook web servers behind Apache? If yes, then please step forward and share your knowledge :-) Many thanks!

like image 877
Laryx Decidua Avatar asked May 27 '14 13:05

Laryx Decidua


People also ask

How do I run IPython notebook locally?

Launch a Notebook To launch a Jupyter notebook, open your terminal and navigate to the directory where you would like to save your notebook. Then type the command jupyter notebook and the program will instantiate a local server at localhost:8888 (or another specified port).

Is IPython the same as Jupyter?

In 2015, the IPython developers made a major code reorganization of their ever-growing project. The Notebook is now called the Jupyter Notebook. This interface can be used not only with Python but with dozens of other languages such as R and Julia. IPython is now the name of the Python backend (aka kernel).

Does Jupyter require IPython?

Jupyter installation requires Python 3.3 or greater, or Python 2.7. IPython 1. x, which included the parts that later became Jupyter, was the last version to support Python 3.2 and 2.6.


1 Answers

WARNING: This is rather verbose, as I gather you have figured much of this, but for documentation purposes, I laid out enough detail here for someone else to follow.

I put this answer together after implementing this myself with the help from various links. The first from here Websocket origin check fails when used with Apache WS proxy #5525. I repeat much of it here with some changes. Other links are referenced below.

1. Set up iPython:

This is in the post, but rather than do it as the original post suggested, I just followed the general instructions for Running a notebook server. With this done you should be able to test the setup, which will require enabling the port you have this configured for. If this does not work, then any Apache set up will not work.

2. Configure Apache:

  1. Make sure you have the following mods available and enabled.

./configure --enable-proxy --enable-ssl --enable-deflate --enable-proxy-http --enable-proxy-wstunnel --enable-info --enable-rewrite --enable-headers

Added --enable-headers here as they were not installed on mine. Also I used the Apache2 a2enmod command. So sudo a2enmod headers, sudo a2enmod proxy, etc.

If you're running a version of Apache prior to 2.4, you do not have the proxy_wstunnel mod. You can either a patch your version or upgrade. To patch your version, you can follow these instructions. Be sure to copy over both mod_proxy.so and mod_proxy_wstunnel.so. To get the configure script, you need to run ./buildconfig, which has its own dependencies. This is noted in a comment therein.

  1. Within Apache, create a "sites-available/iPython.conf" file. Originally I said to either add to httpd.conf or ports.conf. Adding your own site file is much cleaner and will allow you to enable/disable the configuration when desired.

    Listen [ANY PORT HERE] # post has port 8999 here... ... <VirtualHost *:[ANY PORT HERE]> SSLProxyEngine On # post did not have this... ProxyPass / http://127.0.0.1:8888/ ProxyPassReverse / http://127.0.0.1:8888/ # spoof headers to make notepad accept the request as coming from the same origin Header set Origin "http://127.0.0.1:8888/" RequestHeader set Origin "http://127.0.0.1:8888/" LogLevel debug </VirtualHost>

    NOTE 1: The post uses port 8999, but it can be any port you want. You want port 80 here, but you do not need to specify it, so, modifying the above would yield:

    <VirtualHost *:80> ... # Everything is the same here... </VirtualHost>

    NOTE 2: Since you are using SSL, you need to add SSLProxyEngine On within the body of the VirtualHost definition. As noted above, the post did not have this specifically.

    NOTE 3: Port 8888 is whatever port ipython is running on. Change this based on your configuration.

    NOTE 4: If you want to host multiple applications, and this is one of them, rather than having / and :8888/, you will want /ipython and :8888/ipython or whatever you want this to be named. In order to support this, see Running with a different URL prefix.

  2. Enable the new configuration: sudo a2ensite iPython

If you need to disable: sudo a2dissite iPython

  1. Reload Apache: sudo service apache2 reload

My Environment:

Ubuntu 14.04.1 Apache 2.4.7 ipython 2.3.0

EDIT: Updated to reflect the final changes I made to get this working. I also changed the instruction order to what I think makes more sense.

like image 101
sfdurbano Avatar answered Sep 22 '22 08:09

sfdurbano