When you think of reverse proxies, you say nginx. nginx is the ideal reverse proxy tool.

But now the conditions are tough. The server doesn't have nginx and doesn't have root privileges, which means you can't compile and install nginx, and only one port, 80, is open for access. How do you get a request on port 80 to be forwarded to an http service on another port?

In other words, without nginx, how do you get a request from http://localhost:80/new_req to be forwarded to the http://localhost:9999 service, just as if you were accessing http://localhost:9999 directly?

Well, that requires implementing our own reverse proxy functionality.

This article shares how to implement a reverse proxy using only Django.

1.Installation

pip install django-revproxy

This wheel has a corresponding whl file, which is very easy to install successfully, and does not depend on other wheels. If it is an intranet environment, download the whl file from pypi.org, copy it into pip and install it.

2.Configuration

In Django's configuration file settings.py, in INSTALLED_APPS, add 'revproxy'.

#Add 'revproxy' to INSTALLED_APPS.
INSTALLED_APPS = (
    # ...
    'django.contrib.auth',
    'revproxy',
    # ...
)

Then write a view class, such as myproxy/views.py:

from revproxy.views import ProxyView

class TestProxyView(ProxyView):
    upstream = "http://localhost:9999" ##http://example.com

Then modify Django's urls.py file to add a route like this

from myproxy.views import TestProxyView
from django.conf.urls import url

urls = [
   # ...
   url(r'^new_req/(?P<path>.*)$', TestProxyView.as_view()),
   # ...
]

Or, instead of creating a new views.py file, you can just write this in url.py

from django.urls import re_path

from revproxy.views import ProxyView

urlpatterns = [
    re_path(r'(?P<path>.*)', ProxyView.as_view(upstream='http://example.com/')),
]

Finally, deploy the Django service on port 80, and then visit

http://localhost/new_req

is equivalent to accessing

http://localhost:9999

3.How it works

This diagram is what django-revproxy does:

1.Django receives requests from clients and processes them using revproxy.proxy.

2.Revproxy will clone client requests.

3.If the user is authenticated in Django and the add_remote_user property is set to True, the HTTP header REMOTE_USER will be set to request.user.username.

class CustomProxyView(ProxyView):
    upstream = 'http://www.example.com'
    add_remote_user = True
    

4.If the add_x_forwarded attribute is set to True, the HTTP headers X-Forwarded-For and X-Forwarded-Proto will be set to the IP address and protocol (http or https) of the requester, respectively.

5.The cloned request is sent to the upstream server, which is upstream.

6.After receiving a response from upstream, the view will process it to ensure that all headers are set correctly. some headers, like Location, are considered special cases.

7.The response received from the upstream server is converted to django.http.HttpResponse. for binary files, use StreamingHttpResponse to reduce memory usage.

8.If the user sets a set of diazo rules and a theme template, the diazo/XSLT transformation is applied to the response body.

views.py :

from django.urls import re_path

from revproxy.views import DiazoProxyView

proxy_view = DiazoProxyView.as_view(
    upstream='http://example.com/',
    html5=True,
    diazo_theme_template='base.html',
)

urlpatterns = [
    re_path(r'(?P<path>.*)', proxy_view),
]
    

base.html :

<html>
    <head>...</head>
    <body>
        ...
        <div id="content"></div>
        ...Fix all links in the docs (and README file etc) from old to new repo

9.Finally, the response will be returned to the user.

4.django-revproxy

django-revproxy doc

To share a file, I use a Python command in a directory: python -m http.server, which allows files or folders in that directory to be shared via http for others to download and use, and django-revproxy, which allows this functionality to be integrated into one of Django's routes in The developer server deployment is really easy for everyone to use.

Related articles

What does join mean in python?

Python has two functions .join() and os.path.join(), which do the following: . join(): Concatenate string arrays. Concatenates the elements of a string, a tuple, and a list with the specified characters (separator) to generate a new string os.path.join()

Add new content to the python dict

Adding new content to a dictionary is done by adding new key/value pairs, modifying or deleting existing key/value pairs as in the following examples

How to delete elements in python dict

clear() method is used to clear all the data in the dictionary, because it is an in-place operation, so it returns None (also can be interpreted as no return value)

python crawler how to get cookies

Cookie, refers to the data (usually encrypted) that some websites store on the user's local terminal in order to identify the user and perform session tracking. For example, some websites require login to access a page, before login, you want to grab the

How OpenCV tracks objects in video

Each frame of the video is a picture, tracking an object in the video, decomposition down, in fact, is to find that object in each frame of the picture.

How does FastAPI close the interface documentation?

FastApi comes with interface documentation, which saves us a lot of work when developing back-end interfaces. It automatically identifies the parameters of the interface based on your code and also generates a description of the interface based on your