jazzband / django-revproxy

Reverse Proxy view that supports all HTTP methods, Diazo transformations and Single Sign-On.
Mozilla Public License 2.0
300 stars 118 forks source link

How to set upstream to same server #124

Open JoePercipientAI opened 3 years ago

JoePercipientAI commented 3 years ago

I want to set up a reverse proxy that will redirect a Django app URL to another part of the same website that is outside of Django.

(for example):

www.mysite.com/MyDjangoApp/foo/ -> www.mysite.com/bar/

And of course, I need this to work for whatever domain/server the app is installed on.

The example in the docs only shows redirecting to a static alternate domain (upstream='http://example.com/'). What to I set "upstream" to in order to use whatever server the original request was made to, instead of always going to example.com?

otuoma commented 3 years ago

I have not tested this, but if you create a view that extends ProxyView like this, it should allow you to set the upstream URL to whatever is passed in the url GET parameter.

from revproxy.views import ProxyView
from django.http import HttpResponse

class MyProxyView(ProxyView):

    def get(self, request):
        self.upstream = request.GET.get('upstream') 
        return HttpResponse(revproxy.response)
JoePercipientAI commented 3 years ago

@otuoma Thanks. I have tried setting "upstream" from code, and it works if I set it to just a host, like http://www.mysite.com/, but if I add anything else at the end of the URL, like in my example www.mysite.com/bar/, then I get an exception:

AttributeError: 'MyProxyView' object has no attribute '_parsed_url'

For my use case, I definitely need to have the additional part to the path.

Is django-revproxy not going to work for me?