我服与Apache一个Django应用程序。在Django的settings.py我有 DEBUG =假,所以我不得不让一些主机,如: ALLOWED_HOSTS = ['.dyndns', 'localhost'的] 。这工作得很好,但是我想有通过其内部的IP地址在本地网络上访问服务器,以及像: 192.168.0.X 或 127.0.0.1 ,等我怎么可以定义 192。* 或 127。* 在 ALLOWED_HOSTS ,如果我想用 ALLOWED_HOSTS =避免开放接入完全['*'] ?
I'm serving a Django app with Apache. In Django's settings.py I have DEBUG = False, therefore I had to allow some hosts, like: ALLOWED_HOSTS = ['.dyndns', 'localhost']. This works fine, however I would like to have the server accessible on the local network via its internal IP address as well, like: 192.168.0.x, or 127.0.0.1, etc. How could I define 192.* or 127.* in ALLOWED_HOSTS, if I'd like to avoid opening up the access entirely by ALLOWED_HOSTS = ['*']?
推荐答案继@rnevius的建议,并根据从@AlvaroAV中的如何设置自定义的中间件在Django ,我已经成功与该中间件解决:
Following the recommendation from @rnevius, and based on the guidelines from @AlvaroAV in how to setup custom middleware in django, I've managed to solve with this middleware:
from django.http import HttpResponseForbidden class FilterHostMiddleware(object): def process_request(self, request): allowed_hosts = ['127.0.0.1', 'localhost'] # specify complete host names here host = request.META.get('HTTP_HOST') if host[len(host)-10:] == 'dyndns': # if the host ends with dyndns then add to the allowed hosts allowed_hosts.append(host) elif host[:7] == '192.168': # if the host starts with 192.168 then add to the allowed hosts allowed_hosts.append(host) if host not in allowed_hosts: raise HttpResponseForbidden return None和 settings.py 设置 ALLOWED_HOSTS = ['*'] 不再开辟了在所有主机不受控制的方式。
and setting ALLOWED_HOSTS = ['*'] in settings.py no longer opens up for all hosts in an uncontrolled way.
谢谢你们! :)
更多推荐
Django的ALLOWED
发布评论