mljar / mercury

Convert Jupyter Notebooks to Web Apps
https://RunMercury.com
GNU Affero General Public License v3.0
4.01k stars 255 forks source link

Can't host mercury apps on paths other than root URL #356

Closed lukastk closed 1 year ago

lukastk commented 1 year ago

I'm using NGINX to serve my Mercury app using a reverse proxy. I'm finding that my app only works properly if I forward it from the root URL. That is, it only works if I host it at www.website.com, but not if I host it at www.website.com/path. I notice that it tries to access assets at the root, such as www.website.com/static etc, so I presume this has something to do with how the root path is set in the Django settings.

pplonski commented 1 year ago

Hi @lukastk,

Thank you for creating the issue. I think that we need to add some parameter to control deployments in subdirectories.

Would it be possible for you to deploy at subdomain instead? For example www.path.website.com.

May I ask what is your use case? Did you manage to create web app with Mercury?

lukastk commented 1 year ago

Hi @pplonski, thanks for the prompt reply!

I think that we need to add some parameter to control deployments in subdirectories.

That would be great.

May I ask what is your use case?

I'd like to set up a way for my team to quickly turn their notebooks into apps, where each app would be served from its own path www.website.com/app123. I realise that a single Mercury app can host several notebooks, so an alternative would be to have a single Mercury server on a subdomain apps.website.com, where each individual notebook would then be at apps.website.com/apps/app123 etc. Our issue with doing this however is that each of our projects have different dependencies, and sometimes different python versions, so it would be useful to be able to run separate instances of mercury for each project.

Did you manage to create web app with Mercury?

Yes, other than the aformentioned issue, I have been able to deploy it successfully, If I deploy it from the root URL. That is, if the NGINX site is configured as

location / {
    proxy_pass http://127.0.0.1:8000/;
    ...
}

rather than

location /path/ {
    proxy_pass http://127.0.0.1:8000/;
    ...
}
slvstr1 commented 1 year ago

I have a similar problem. I run Mercury at port 8002 with: mercury run 8002

I get:

     _ __ ___   ___ _ __ ___ _   _ _ __ _   _ 
    | '_ ` _ \ / _ \ '__/ __| | | | '__| | | |
    | | | | | |  __/ | | (__| |_| | |  | |_| |
    |_| |_| |_|\___|_|  \___|\__,_|_|   \__, |
                                         __/ |
                                        |___/ 

Version: 2.3.4
The notebook Untitled1.ipynb will be updated
Successfully updated a notebook (id:1)
The notebook tryout.ipynb will be updated
Successfully updated a notebook (id:2)
The notebook tryoutb.ipynb will be updated
Successfully updated a notebook (id:3)
The notebook Untitled.ipynb will be updated
Successfully updated a notebook (id:4)
The notebook tryout listthings.ipynb will be updated
Successfully updated a notebook (id:5)
The notebook article.ipynb will be updated
Successfully updated a notebook (id:6)
Performing system checks...

System check identified no issues (0 silenced).
September 07, 2023 - 08:27:06
Django version 4.2, using settings 'server.settings'
Starting ASGI/Daphne version 4.0.0 development server at http://127.0.0.1:8002/
Quit the server with CONTROL-C.

I now configured nginx to redirect straight, without path, to 8002:

location / {
    proxy_pass http://127.0.0.1:8002/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

Connecting to http://78.128.250.126/ seems to work, in the sense that the tab title becomes "Mercury, turn Jupyter...". I also can log into the admin part: image

However the page is empty: image

BTW: on my local machine it works fine.

pplonski commented 1 year ago

working on this ...

pplonski commented 1 year ago

I was able to setup Mercury on subpath, but need to fix routes (other links are not working). I will let you know when it will be ready.

image

pplonski commented 1 year ago

Good news! I manage to run mercury on subpath :)

image

Here is summary of what I did. I deployed Mercury on subdomain analytics.

1. Update nginx config

Comment out code in `docker/nginx/default.conf:

    # location / {
    #     root   /usr/share/nginx/html;
    #     index  index.html index.htm;
    #     try_files $uri $uri/ /index.html;
    # }

Add code:

    location = /analytics {

        root /usr/share/nginx/html;
        try_files /index.html =404;
    }

    location ~ ^/analytics(.*) {

        root /usr/share/nginx/html;
        try_files $1 $1/ /index.html =404;
    }

2. Update frontend/package.json

We need to add homepage in package.json:

{
  "name": "frontend",
  "homepage":"/analytics",
  "version": "2.3.4",
   # ... rest of json ...

3. Update frontend/src/Routes.tsx

Update Router with basename:

   // rest of code ...
    <Router basename="/analytics">
   // rest of code ...

One more thing, there are some places in the code that are using <a> links instead <Link> from react-router-dom - they will not work with basename. I will check how to fix them and let you know.

pplonski commented 1 year ago

@lukastk @slvstr1 @gjkalkman @eriknl1982 I've fixed the issue with links, code is in the main branch. Please pull the latest changes. I've added in the documentation steps on how to deploy Mercury on subpath https://runmercury.com/docs/docker-compose/#deploy-on-subpath

@aplonska and I will be super happy to see what have you created with Mercury. You can share it here https://github.com/mljar/mercury/issues/366 :)

Good luck! :rocket: