服务器由于重定向循环而关闭

server is down due to redirection loop

我正在使用 wagtail 钩子将主页重定向到另一个页面,在我的例子中是 LibraryPage 这是代码:

@hooks.register("before_serve_page")
def redirect_to_library_page(
    page, request, serve_args, serve_kwargs
):
    if request.path == "/":
        site = Site.find_for_request(request)
        library = site.root_page.get_children().type(LibraryPage).first()
        return redirect(library.get_full_url())

服务器运行了几分钟,我可以浏览不同的页面和链接,然后几分钟后服务器关闭了,我收到了入口错误 这是日志示例

10.140.0.51 - - [04/Feb/2021:09:47:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"                                                                                                                     
10.140.0.49 - - [04/Feb/2021:09:47:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"                                                                                                                     
10.140.0.51 - - [04/Feb/2021:09:47:34 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"                                                                                                                     
10.140.0.51 - - [04/Feb/2021:09:47:35 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"                                                                                                                     
10.4.5.1 - - [04/Feb/2021:09:47:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"                                                                                                                        
10.140.0.50 - - [04/Feb/2021:09:47:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:48:20 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:48:26 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:48:31 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:48:31 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:48:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:48:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:48:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:48:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:48:34 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:48:35 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:48:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:48:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:49:20 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:49:26 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:49:31 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:49:31 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:49:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:49:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:49:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:49:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:49:34 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:49:35 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:49:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:49:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:50:20 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:50:26 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:50:31 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:50:31 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:50:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:50:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:50:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.49 - - [04/Feb/2021:09:50:33 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:50:34 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.51 - - [04/Feb/2021:09:50:35 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.4.5.1 - - [04/Feb/2021:09:50:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"
10.140.0.50 - - [04/Feb/2021:09:50:38 +0900] "GET / HTTP/1.1" 302 0 "-" "GoogleHC/1.0"

我不知道代码有什么问题任何帮助都会非常有用

我正在使用设置文件中的这些详细信息更新问题:

BASE_URL = WAGTAILAPI_BASE_URL = "https://homesite.com"
REDIRECT_HOSTS = ["homesite.com"]
ALLOWED_HOSTS = [
    
    "*",
    "realte.homesite.com",
    "mirai-i.homesite.com",
    "sum.homesite.com",
    
]

正在检查 shell:

>>> for site in Site.objects.all():                                                                                                                                                                       
...     library = site.root_page.get_children().type(LibraryPage).first()                                                                                                                                 
...     print(library.get_full_url())                                                                                                                                                                     
...
https://realte.homesite.com/library/
https://mirai-i.homesite.com/library/
https://sum.homesite.com/library/
>>> Site.objects.count()
3
>>> site = settings.REDIRECT_HOSTS[0]
>>> site
'homesite.com'
>>> Site.objects.first().__dict__
{'_state': <django.db.models.base.ModelState object at 0x7fe795296670>, 'id': 3, 'hostname': 'realte.homesite.com.com', 'port': 443, 'site_name': 'realte', 'root_page_id': 1890, 'is_default_site': False}
>>> Site.objects.filter(hostname=site)
<QuerySet []>
>>>

我真正想看到的是这些重定向将您发送到哪里。尝试 curl 怎么样:curl --head --location http://example.com

终于解决了这个问题,循环是由于HTTP_HOST没有在ALLOWED_HOSTS设置中产生的 在这里我更新了代码,现在我没有任何重定向循环

@hooks.register("before_serve_page")
def redirect_to_library_page(page, request, serve_args, serve_kwargs):
    # this method will be removed once we implement the homepage designs
    if request.META.get("HTTP_HOST") in settings.REDIRECT_HOSTS:
        site = Site.objects.first().find_for_request(request)
        library = site.root_page.get_children().type(LibraryPage).first()
        return HttpResponseRedirect(library.get_full_url())
    elif (
        request.META.get("PATH_INFO") == "/"
        and request.META.get("HTTP_HOST") in settings.ALLOWED_HOSTS
    ):
        site = Site.find_for_request(request)
        library = site.root_page.get_children().type(LibraryPage).first()
        return HttpResponseRedirect(library.get_full_url())