Django作为反向代理 [英] Django as reverse proxy
问题描述
我的客户端 - 服务器应用程序主要基于专用的http服务器,它以类似Ajax的方式与客户端通信,即。客户端GUI在异步http请求/响应周期时刷新。
My client-server application is mainly based on special purpose http server that communicates with client in an Ajax like fashion, ie. the client GUI is refreshed upon asynchronous http request/response cycles.
特殊用途http服务器的可扩展性有限,随着应用程序的增长,需要越来越多的标准功能例如,由Django提供。
Evolvability of the special purpose http server is limited and as the application grows, more and more standard features are needed which are provided by Django for instance.
因此,我想添加一个Django应用程序作为外观/反向代理,以隐藏非标准的特殊用途服务器,并能从Django获得。我想将Django应用程序作为网关而不是出于安全原因使用http重定向并隐藏复杂性。
Hence, I would like to add a Django application as a facade/reverse-proxy in order to hide the non-standard special purpose server and be able to gain from Django. I would like to have the Django app as a gateway and not use http-redirect for security reasons and to hide complexity.
然而,我担心的是通过隧道传输流量关于服务员的Django可能破坏性能。这是一个有效的问题吗?
However, my concern is that tunneling the traffic through Django on the serer might spoil performance. Is this a valid concern?
是否有替代方案可以解决问题?
Would there be an alternative solution to the problem?
推荐答案
我继续建造了一个简单的原型。它相对简单,我只需要设置一个映射,映射我想要重定向的所有URL。视图函数如下所示:
I went ahead and built a simple prototype. It was relatively simple, I just had to set up a view that maps all URLs I want to redirect. The view function looks something like this:
def redirect(request):
url = "http://%s%s" % (server, request.path)
# add get parameters
if request.GET:
url += '?' + urlencode(request.GET)
# add headers of the incoming request
# see https://docs.djangoproject.com/en/1.7/ref/request-response/#django.http.HttpRequest.META for details about the request.META dict
def convert(s):
s = s.replace('HTTP_','',1)
s = s.replace('_','-')
return s
request_headers = dict((convert(k),v) for k,v in request.META.iteritems() if k.startswith('HTTP_'))
# add content-type and and content-length
request_headers['CONTENT-TYPE'] = request.META.get('CONTENT_TYPE', '')
request_headers['CONTENT-LENGTH'] = request.META.get('CONTENT_LENGTH', '')
# get original request payload
if request.method == "GET":
data = None
else:
data = request.raw_post_data
downstream_request = urllib2.Request(url, data, headers=request_headers)
page = urllib2.urlopen(downstream_request)
response = django.http.HttpResponse(page)
return response
所以它实际上非常简单并且性能足够好,特别是如果重定向转到同一主机上的环回接口。
So it is actually quite simple and the performance is good enough, in particular if the redirect goes to the loopback interface on the same host.
这篇关于Django作为反向代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!