jupyterhub / jupyter-rsession-proxy

Jupyter extensions for running an RStudio rsession proxy
BSD 3-Clause "New" or "Revised" License
118 stars 87 forks source link

Not working behind a reverse proxy #121

Open victor-moreno opened 2 years ago

victor-moreno commented 2 years ago

Bug description

For some reason, with jupyterhub (v1.4.2 in docker), behind a reverse proxy, rstudio returns a malformed url, doubling the hostname separated by comma, like: "https://www.myhost.org%2C%20www.myhost.org/jupyter/user/victor/rstudio/auth-sign-in/?appUri=%2F" The %2C%20 is ", "

In case it helps someone with the same problem, I managed to get a working version with these changes to rewrite_auth. I never got redirected to /auth-sign-in, so the elif is there to keep current version, but is not needed in my setup.

def rewrite_auth(response, request):
    '''
       As of rstudio-server 1.4ish, it would send the client to /auth-sign-in
       rather than what the client sees as the full URL followed by
       /auth-sign-in. See rstudio/rstudio#8888. We rewrite the response by
       sending the client to the right place.
    '''
    for header, v in response.headers.get_all():
        if header == "Location":
            # Visit the correct page VM 211222
            if ", " in urlparse(v).netloc:
                u = urlparse(v)
                u = u._replace(scheme='https')
                u = u._replace(netloc=u.netloc.split(",")[0])
                response.headers[header] = urlunparse(u)
            elif v.startswith("/auth-sign-in"):
                # Visit the correct page
                u = urlparse(request.uri)
                response.headers[header] = urlunparse(u._replace(path=u.path+v))
welcome[bot] commented 2 years ago

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively. welcome You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! :wave:
Welcome to the Jupyter community! :tada:

juzdzema commented 2 years ago

We encountered a similar behaviour with rstudio returning a malformed url (in our case the url gets stripped). For example, if we expect the url https://www.myhost-proxied.comp-any.org/user/userID/auth-sign-in/?appUri=%2F we actually get https://www.myhost/user/userID/auth-sign-in/?appUri=%2F.

We are behind a reverse proxy as well and in an Openshift context. In PR #124 we propose a general solution that should fix all kinds of root url problems (including that of the OP).

guimou commented 2 years ago

@juzdzema You're the real MVP! Thank you so much. I had seen this issue before as I was struggling with the same problem since a few weeks (redirected to http://jupyterhub/user/userid/...). I worked on something else, got back to it today, debugging different things and coming closer to what you did, but then had a quick check in the issues. And BAM! The solution was there. Please accept this small token of gratitude 🍻

moschlar commented 1 year ago

In my case, it redirects to an URI that contains a (wrong) port specification:

https://jupyterhub....:80/user/.../rstudio/auth-sign-in?appUri=%2F

ryanlovett commented 1 year ago

@moschlar I had the same issue, with jupyter-rsession-proxy v2.1.0 and jupyter-server-proxy v3.2.2, but my nginx reverse proxy was not configured like the recommendation at https://jupyterhub.readthedocs.io/en/stable/reference/config-proxy.html. After adjusting it to match the recommendation, the port addition problem went away.

@victor-moreno Regarding the rewrite, I suspect the rewriting will no longer be necessary due to changes merged for jupyter-server-proxy v3.2.2. I believe it is the hop-by-hop PR, but I haven't tested this yet. Can you please test with the latest releases of jupyter-rsession-proxy and jupyter-server-proxy? If you're still seeing the issue, can you please share your reverse proxy configuration?

moschlar commented 1 year ago

@ryanlovett Hm, I just double-checked it, but it doesn't seem to make a difference... However, this is a z2jh instance which uses chp between my external reverse proxy and the hub/notebook itself... Maybe I need to dig there!

ryanlovett commented 1 year ago

@moschlar Thanks for checking. The latest does not work on mybinder.org either. I don't know whether mybinder uses the recommended proxy configurations or not (it probably does), but I think that jupyter-rsession-proxy has to work on it regardless.

mathematicalmichael commented 1 year ago

In my case, it redirects to an URI that contains a (wrong) port specification:

https://jupyterhub....:80/user/.../rstudio/auth-sign-in?appUri=%2F

I'm getting the same error with that environment.

jupyter-rsession-proxy==2.1.0
jupyter-server-proxy==3.2.2

I'm running a jupyterhub and using cloudflare tunnel as my reverse proxy so I don't exactly have the luxury of configuring nginx differently (per @ryanlovett ... not sure what the "adjustment" needed to be, nor whether I can make such a change).

ryanlovett commented 1 year ago

Okay, thanks @mathematicalmichael ! Are you able to apply https://github.com/jupyterhub/jupyter-rsession-proxy/pull/134 to your environment?

mathematicalmichael commented 1 year ago

edits to bottom of Dockerfile:

# RUN python3 -m pip install jupyter-rsession-proxy jupyter_server_proxy
RUN python3 -m pip install git+https://github.com/ryanlovett/jupyter-rsession-proxy@95759d85e95de6cb102702762d23f74ae2d46baa

fixed it!

thank you... had a bit of a time trying to figure out what the "solution" was amongst the discussions. The fast reply was much appreciated!!!

ryanlovett commented 1 year ago

@mathematicalmichael Okay, great. That fixes it on binder as well. I'll merge soon.

mathematicalmichael commented 1 year ago

:shipit: