|
1 | | -.. _arbitrary-ports: |
| 1 | +(arbitrary-ports)= |
2 | 2 |
|
3 | | -================================== |
4 | | -Accessing Arbitrary Ports or Hosts |
5 | | -================================== |
| 3 | +# Accessing Arbitrary Ports or Hosts |
6 | 4 |
|
7 | 5 | If you already have a server running on localhost listening on |
8 | 6 | a port, you can access it through the notebook at |
9 | | -``<notebook-base>/proxy/<port>``. |
| 7 | +`<notebook-base>/proxy/<port>`. |
10 | 8 | The URL will be rewritten to remove the above prefix. |
11 | 9 |
|
12 | 10 | You can disable URL rewriting by using |
13 | | -``<notebook-base>/proxy/absolute/<port>`` so your server will receive the full |
| 11 | +`<notebook-base>/proxy/absolute/<port>` so your server will receive the full |
14 | 12 | URL in the request. |
15 | 13 |
|
16 | 14 | This works for all ports listening on the local machine. |
17 | 15 |
|
18 | 16 | You can also specify arbitrary hosts in order to proxy traffic from |
19 | | -another machine on the network ``<notebook-base>/proxy/<host>:<port>``. |
| 17 | +another machine on the network `<notebook-base>/proxy/<host>:<port>`. |
20 | 18 |
|
21 | | -For security reasons the host must match an entry in the ``host_allowlist`` in your configuration. |
| 19 | +For security reasons the host must match an entry in the `host_allowlist` in your configuration. |
22 | 20 |
|
23 | | -With JupyterHub |
24 | | -=============== |
| 21 | +## With JupyterHub |
25 | 22 |
|
26 | 23 | Let's say you are using a JupyterHub set up on a remote machine, |
27 | 24 | and you have a process running on that machine listening on port |
28 | | -8080. If your hub URL is ``myhub.org``, each user can |
| 25 | +8080\. If your hub URL is `myhub.org`, each user can |
29 | 26 | access the service running on port 8080 with the URL |
30 | | -``myhub.org/hub/user-redirect/proxy/8080``. The ``user-redirect`` |
| 27 | +`myhub.org/hub/user-redirect/proxy/8080`. The `user-redirect` |
31 | 28 | will make sure that: |
32 | 29 |
|
33 | | -#. It provides a redirect to the correct URL for the particular |
| 30 | +1. It provides a redirect to the correct URL for the particular |
34 | 31 | user who is logged in |
35 | | -#. If a user is not logged in, it'll present them with a login |
| 32 | +2. If a user is not logged in, it'll present them with a login |
36 | 33 | screen. They'll be redirected there after completing authentication. |
37 | 34 |
|
38 | | -You can also set ``c.Spawner.default_url`` to ``/proxy/8080`` to have |
| 35 | +You can also set `c.Spawner.default_url` to `/proxy/8080` to have |
39 | 36 | users be shown to your application directly after logging in - |
40 | 37 | without ever seeing the notebook interface. |
41 | 38 |
|
42 | | -Without JupyterHub |
43 | | -================== |
| 39 | +## Without JupyterHub |
44 | 40 |
|
45 | 41 | A very similar set up works when you don't use JupyterHub. You |
46 | | -can construct the URL with ``<notebook-url>/proxy/<port>``. |
| 42 | +can construct the URL with `<notebook-url>/proxy/<port>`. |
47 | 43 |
|
48 | | -If your notebook url is ``http://localhost:8888`` and you have |
| 44 | +If your notebook url is `http://localhost:8888` and you have |
49 | 45 | a process running listening on port 8080, you can access it with |
50 | | -the URL ``http://localhost:8888/proxy/8080``. |
| 46 | +the URL `http://localhost:8888/proxy/8080`. |
51 | 47 |
|
52 | 48 | This is mostly useful for testing, since you can normally just |
53 | 49 | access services on your local machine directly. |
54 | 50 |
|
55 | | -From Notebook Extension |
56 | | -======================= |
| 51 | +## From Notebook Extension |
57 | 52 |
|
58 | 53 | If you have a client side extension for the classic Jupyter Notebook |
59 | 54 | interface (nbextension), you can construct the URL for accessing |
60 | 55 | your service in this way: |
61 | 56 |
|
62 | | -.. code:: js |
| 57 | +```js |
| 58 | +define(['base/js/utils'], function(utils) { |
| 59 | + // Get base URL of current notebook server |
| 60 | + var base_url = utils.get_body_data('baseUrl'); |
63 | 61 |
|
64 | | - define(['base/js/utils'], function(utils) { |
65 | | - // Get base URL of current notebook server |
66 | | - var base_url = utils.get_body_data('baseUrl'); |
| 62 | + // Construct URL of our proxied service |
| 63 | + var service_url = base_url + 'proxy/' + port; |
67 | 64 |
|
68 | | - // Construct URL of our proxied service |
69 | | - var service_url = base_url + 'proxy/' + port; |
70 | | - |
71 | | - // Do stuff with your service_url |
72 | | - }); |
| 65 | + // Do stuff with your service_url |
| 66 | +}); |
| 67 | +``` |
73 | 68 |
|
74 | 69 | You can then make HTTP / Websocket requests as you wish from your |
75 | 70 | code. |
76 | 71 |
|
77 | | -From JupyterLab Extension |
78 | | -========================= |
| 72 | +## From JupyterLab Extension |
79 | 73 |
|
80 | 74 | Accessing your service from a JupyterLab extension is similar to |
81 | 75 | accessing it from a classic notebook extension. |
82 | 76 |
|
83 | | -.. code:: typescript |
84 | | - |
85 | | - import { PageConfig } from '@jupyterlab/coreutils'; |
| 77 | +```typescript |
| 78 | +import { PageConfig } from '@jupyterlab/coreutils'; |
86 | 79 |
|
87 | | - // Get base URL of current notebook server |
88 | | - let base_url = PageConfig.getBaseUrl() |
| 80 | +// Get base URL of current notebook server |
| 81 | +let base_url = PageConfig.getBaseUrl() |
89 | 82 |
|
90 | | - // Construct URL of our proxied service |
91 | | - let service_url = base_url + 'proxy/' + port; |
| 83 | +// Construct URL of our proxied service |
| 84 | +let service_url = base_url + 'proxy/' + port; |
92 | 85 |
|
93 | | - // Do stuff with your service_url |
| 86 | +// Do stuff with your service_url |
| 87 | +``` |
0 commit comments