thomasw / djproxy

djproxy is a class-based generic view reverse HTTP proxy for Django.
MIT License
42 stars 15 forks source link

Question. Reverse Proxy. #42

Closed mchingotto closed 2 years ago

mchingotto commented 2 years ago

Hello Team,

Thank you for your efforts with this library. I am confused about something. When configuring the middleware to ProxyPassReverse in the view, should I expect all links in the response content to the user (for example images inside the Html page) are going to be changed to the hostname the one is doing the proxy? I could not make this work. Am I doing something wrong?

Thank you, I appreciate it. M

thomasw commented 2 years ago

No, you shouldn't expect any content rewiring. Please take a look at https://github.com/thomasw/djproxy/issues/18 - djproxy provides mechanisms for you to manage this complexity yourself.

mchingotto commented 2 years ago

Thank you Thomas for your reply. One more last question. Is any way to define the IP address of the base_URL plus the base_URL? I mean, something like this:

base_URL = 'www.remote.site' base_URL_IPaddress = 'x.x.x.x'

In order to avoid the DNS resolution or the /etc/hosts file.

Thank you once again. I appreciate it. M

thomasw commented 2 years ago

Not exactly, or at least not as you've described. If I'm understanding your question correctly, I think you can achieve what you're looking to do by setting a custom Host header on the upstream request and bypass DNS resolution entirely.

  1. Use the IP address in your base URL.
  2. Add a middleware that explicitly sets the host header to the desired domain. See https://github.com/thomasw/djproxy/blob/e7f0c6ef536c01fa8eab193d613ba71406a11cde/djproxy/proxy_middleware.py#L80 for an example middleware that modifies headers. See https://github.com/thomasw/djproxy/blob/e7f0c6ef536c01fa8eab193d613ba71406a11cde/djproxy/views.py#L73-L79 to understand how middleware are used to build kwargs to ultimately pass on to the python requests library. General related discussion for python requests (https://stackoverflow.com/a/29995264).
mchingotto commented 2 years ago

Thank you Thomas!!! I will look into the option (2). Having that, maybe It could point to the backend server to the same domain you are receiving the request (of course different IPaddress, the public one you receive the request and the private to the backend); so there is no need to translate every http/https link sent to the response. Now, I need to differentiate between http and https inside the class_view or in the URL pattern in order to forward correctly both traffic types.

I really appreciate your time with me, It is important to me. Best regards, M