[英]django ALLOWED_HOSTS not working
我的 settings.py 文件包含:
DEBUG = False
ALLOWED_HOSTS = [u'mydomainxxx.com']
但是,我能夠像這樣發出 curl 請求: curl -X GET https://mydomainxxx.com/api/ -H 'Authorization: Token some token'
並得到響應。
我希望使用ALLOWED_HOSTS
會阻止像 curl 這樣的命令從我的 API 獲得響應。這是正常行為嗎?
您將ALLOWED_HOSTS
設置與其他內容混淆。 它表示服務器將偵聽的主機名; 不是連接主機的主機名。 沒有內置的方法來阻止它,但您可以輕松編寫中間件來檢查連接主機名。
您當前的設置將阻止此響應:
curl -X GET http://another_domainxxx.com/api/ -H 'Authorization: Token some token'
即使mydomainxxx.com
和another_domainxxx.com
都將解析為相同的IP地址。
對於想要過濾referer url而不是ip地址的人,我們可以使用以下中間件:
from django.conf import settings
from django import http
class AllowHostsMiddleware(object):
def process_request(self, request):
referer_url = request.META.get('HTTP_REFERER','')
if referer_url.startswith(settings.ALLOWED_REFERER_URL):
return None
return http.HttpResponseForbidden('<h1>Forbidden</h1>')
將您的域或 ip 添加到Allowed_Hosts ,然后鍵入以下命令
sudo systemctl restart nginx
然后
sudo systemctl restart gunicorn and
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.