【问题标题】:ALLOWED_HOSTS not working in my Django App deployed to Elastic BeanstalkALLOWED_HOSTS 在部署到 Elastic Beanstalk 的 Django 应用程序中不起作用
【发布时间】:2019-08-21 08:47:06
【问题描述】:

我将一个 Django 应用程序部署到 AWS Elastic Beanstalk,但即使我已将其添加到允许的主机设置中,我也会收到“无效的 HTTP_HOST 标头”错误。

我收到此错误:

Invalid HTTP_HOST header: 'recordings-env.kf4qfzaijd.us-west-2.elasticbeanstalk.com'. You may need to add 'recordings-env.kf4qfzaijd.us-west-2.elasticbeanstalk.com' to ALLOWED_HOSTS.

这就是我的 settings.py 中的内容

settings.py:

ALLOWED_HOSTS = [
    '127.0.0.1',
    'localhost',
    '.amazonaws.com',
    '.elasticbeanstalk.com',
    'recordings-env.kf4qfzaijd.us-west-2.elasticbeanstalk.com',
]

我认为问题在于我的设置没有更新,因为如果我输入 ALLOWED_HOSTS = ['*'] 也不起作用。我离开了DEBUG = True,在请求信息中我得到了:ALLOWED_HOSTS: ['localhost']

修改后我运行 eb deploy 没有错误。

【问题讨论】:

    标签: django amazon-web-services settings amazon-elastic-beanstalk


    【解决方案1】:

    我意识到我的更改没有被部署,因为我需要先提交,但我不知道(我第一次部署到 eb)。所以,这就是问题所在。

    【讨论】:

    • 你如何提交?是git commit -m "some message" 吗?
    • 是的,和往常一样。
    • 这解决了我的问题!谢谢!此致,另一个 ElasticBeanstalk 新手。
    【解决方案2】:

    这有点棘手,因为您需要使用 EB 动态声明您的 ALLOWED_HOSTS。这个article 在 Gotcha #3 中提供了一些关于如何实现这一目标的好信息

    我会创建一个单独的设置文件,名为 settings_production.py,然后您可以在其中放置以下代码:

    mysite/settings_production.py

    from mysite.settings import *
    
    
    def is_ec2_linux():
        """Detect if we are running on an EC2 Linux Instance
           See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
        """
        if os.path.isfile("/sys/hypervisor/uuid"):
            with open("/sys/hypervisor/uuid") as f:
                uuid = f.read()
                return uuid.startswith("ec2")
        return False
    
    
    def get_linux_ec2_private_ip():
        """Get the private IP Address of the machine if running on an EC2 linux server"""
        from urllib.request import urlopen
        if not is_ec2_linux():
            return None
        try:
            response = urlopen('http://169.254.169.254/latest/meta-data/local-ipv4')
            return response.read().decode("utf-8")
        except:
            return None
        finally:
            if response:
                response.close()
    
    
    # ElasticBeanstalk healthcheck sends requests with host header = internal ip
    # So we detect if we are in elastic beanstalk,
    # and add the instances private ip address
    private_ip = get_linux_ec2_private_ip()
    if private_ip:
        ALLOWED_HOSTS += [private_ip, 'your-django-env.elasticbeanstalk.com']
    
    # Other production overrides
    DEBUG = False
    
    
    

    现在您将“DJANGO_SETTINGS_MODULE”环境变量设置为mysite.production_settings,用于您的生产环境(即您的EB 环境)。

    更新:

    我决定用它来进行测试,并设法让它运行起来。不过我发现了一些东西。上述代码将每个实例的内部 IP 添加到 ALLOWED_HOSTS。这纯粹是为了健康检查,以便 AWS 控制台可以在内部 ping 实例并接收 200OK 响应。我将离开上述解决方案,因为它仍然适用于此目的。但它不会解决您的特定错误。要为您服务,只需添加您的 EB 网址即可。您可以在 AWS 控制台(下面以红色突出显示)或通过键入 eb status 并检查 CNAME 属性在 cli 中找到它。

    配置:

    这是我在源代码中手动创建的基本配置文件:

    .ebextensions/django.config

    
    option_settings:
      aws:elasticbeanstalk:container:python:
        WSGIPath: mysite/wsgi.py
      aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: mysite.settings_production
    

    .ebextensions/db-migrate.config

    
    container_commands:
      01_migrate:
        command: "django-admin.py migrate"
        leader_only: true
    option_settings:
      aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: mysite.settings_production
    

    【讨论】:

    • 首先感谢您的回答。我已经尝试过这个解决方案,但没有奏效。但我认为问题在于我的设置没有更新,因为如果我输入ALLOWED_HOSTS = ['*'] 也不起作用。我离开了DEBUG = True,在请求信息中我得到:ALLOWED_HOSTS: ['localhost']
    • 我意识到我的更改没有被部署,因为我需要先提交但我不知道(我第一次部署到 eb)。所以,这就是问题所在。这个解决方案现在应该可以工作了。谢谢!
    • 不客气。我对上述解决方案进行了全面测试,并设法让我的简单 Django 应用程序启动并运行。我发布的原始代码中有一些错误,所以我对其进行了编辑,但它对于运行状况检查是可选的。只需将我的 EB URL 添加到 ALLOWED HOSTS,我就能让我的应用程序正常工作。我也包括了我的配置,以防它有帮助。这一切都已经过全面测试并且可以正常工作。
    • 正如你所说,起初,在提交之后,我尝试了 ALLOWED_HOSTS = ['*'] 以及我的 eb url 并且工作。然后,为了改进它,我用 settings_production 文件做了你告诉我的事情,并且也工作了:)
    • 谢谢@JosefinaEstevez。 . .我正在将我的文件提交到 github 。 . .我想我有点认为 zip 包是从我的 localhost 发送的,显然 aws 从 repo 获取代码。 . .哈!!
    猜你喜欢
    • 2015-01-25
    • 2016-07-01
    • 2013-09-06
    • 2020-05-20
    • 2016-04-17
    • 2012-06-23
    • 2015-09-21
    • 2020-09-02
    • 2022-12-09
    相关资源
    最近更新 更多