Search code examples
djangohttp-proxy

django-http-proxy prepending slash


I have this in my urls.py

from httpproxy.views import HttpProxy

urlpatterns += patterns('', 
    url(r'^proxy/(?P<url>.*)$', HttpProxy.as_view(base_url=settings.PROXY_URL))
)

And my settings.py

...

PROXY_URL = 'http://external.com'

...

My problem is when accessing the URL http://localhost:8000/proxy/, I can see from the log of http://external.com it is returning 404 because the url has an extra slash prepended so for example:

http://localhost:8000/proxy/test/ will log "GET //test/ HTTP/1.1" 404 15447

I have been digging but couldn't find the bone! If all the masters would be kind enough to lend a bone for this hunger?

Cheers!


Solution

  • Since no one answered (I even got a badge because no one answered, how cool is that?), I will post my solution, which was solved 2 days after the question was asked.

    1 - Because of this issue pointed out by a friend, I have steered away from using django-http-proxy.

    2 - So I resorted to a better library which proxies all HTTP Methods, unlike django-http-proxy that can only proxy GET. Meet django-revproxy.

    3 - Which introduces another problem — The Cookie Conflict. This happens because I have two django instances. The solution is to explicitly declare the cookie path in one of your django app so it won't be using the same path. Just add in settings.py these two lines:

    SESSION_COOKIE_NAME = "yourApp_session_id"
    CSRF_COOKIE_NAME =  "yourApp_csrftoken"
    

    4 - That's it. I hope this solution will help those on the lookout.