Forums

Invalid HTTP_HOST header

Because I cannot find my question in the thread where I posted it, here it comes again: I cannot access my Webpage, I get the error "Invalid HTTP_HOST header, you may need to add 'myrentindextracker.pythonanywhere.com' to ALLOWED_HOSTS".
I checked a couple of times: In the settings.py ALLOWED_HOSTS has the right value. But somehow the server doesn't read the updated settings.py. In the error notice it says, that only a local host is allowed. I changed the settings.py again locally, pushed it to git, pulled it again to Pythonanywhere - nothing changed. I still get the error message. How can I get the server to read the updated settings.py?

What, exactly have you set ALLOWED_HOSTS to? and have you reloaded your web app since changing the code?

Hey, thx for your answer. I set the Allowed_Hosts to ALLOWED_HOSTS = ['myrentindextracker.pythonanywhere.com']. And yes, I reloaded the page. Again and again :/. And I tried different browsers. You can see the error message when you call http://myrentindextracker.pythonanywhere.com/. And you can see that it says, ALLOWED_HOSTS is a local url. But in the settings.py-file, its just what its meant to be: ALLOWED_HOSTS = ['myrentindextracker.pythonanywhere.com']

You posted your last message from a different account to your first message. Are you sure that the code you're editing is in the correct account? Also, check that the settings file that you're editing is the one that your web app is actually using and that you do not have another setting later that undoes it. The settings that were in effect when the request was made are shown at the bottom of that error page, so perhaps you can use that to track down what is happening.

Hey, thanks again. Yes, what I said about the settings.py is correct: In the App Myrentindextracker the Allowed_hosts is ['myrentindextracker.pythonanywhere.com']. All I did was to set up another account to see if it works there. (It worked, because I pushed a settings.py with the right Allowed_hosts to git and pulled it to pa) But it does not work if I change the settings.py in pa or if I pull a newer version of the settings.py with the right value. How can I find out if the app is using the settings.py? There is only one in my App. It seems as if pa is using an older version, but I can`t get it to use the newer one. Do I have to do anything else than clicking on the safe-button of the file? Do I have to tell pa to re-check all files of the App if there have been any changes?

Reloading the site using the green button on the "Web" page will stop the site from running, then start it again using the latest versions of all of your files. So you need to do that each time you make a change.

Hi, thank you again, I didn't know that I had to reload the page not in my browser but on pa. At least the error about invalid host is gone, now it's a server error :).

What is the error in your logs?

Hello friends, the same error.log: Invalid HTTP_HOST header: 'www.xxxxxx.net'. You may need to add 'www.xxxxxx.net' to ALLOWED_HOSTS.

and in settings.py I have: ALLOWED_HOSTS = ['www.xxxxxx.net'] I think the problem is not that because when I put: ALLOWED_HOSTS = ['*'] the problem persists and before this proyect I constructed other 'hello world' proyect with the same parameters and that ran correctly.

I have reloaded many times in Web page and the settings.py file is unique. Help!

@Davidd -- I see your web app works. Where do you see those errors? You should read the error log from the bottom up, to see the most recent and actual errors (if any).

OK people I know what is the problem and I have resolved. The problem is in WSGI configuration file: /var/www/www_xxxxxx_net_wsgi.py file you can find in Web page. The problem is created when you change the name of proyect from mysite to another, we name now 'another_name' If you make this change:

First: Be aware that the directory from your username and the directory inside which has settings.py, wsgi.py.... files have the same name in this case 'another_name'

Second: after this you must change WSGI configuration file two lines:

project_home = '/home/username/another_name'

os.environ['DJANGO_SETTINGS_MODULE'] = 'another_name.settings'

Glad you got that working! It's generally documented on our help pages.

For me it does not work...

This is my wsgi.py file: """ WSGI config for app project.

It exposes the WSGI callable as a module-level variable named application.

For more information on this file, see https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/

"""

import os
import sys

from django.core.wsgi import get_wsgi_application

path = '/home/SirJude/Blog/app'
if path not in sys.path:
    sys.path.insert(0, path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

application = get_wsgi_application()

And this is my settings:

"""
Django settings for app project.

Generated by 'django-admin startproject' using Django 5.1.1.

For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

from dotenv import load_dotenv
from pathlib import Path
import logging
import os
import sys

load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

SECRET_KEY = os.getenv("SECRET_KEY")

Logger = logging.getLogger(__name__)
Logger.error(Path.cwd())

IS_PRODUCTION = os.getenv('DJANGO_PRODUCTION', 'False') == 'True'
Logger.error(f"IS_PRODUCTION: {IS_PRODUCTION}")


if IS_PRODUCTION:
    DEBUG = False
    ALLOWED_HOSTS = ['sirjude.pythonanywhere.com']
    Logger.error(f"ALLOWED_HOSTS: {ALLOWED_HOSTS}")

    # # Security settings for production
    # SECURE_HSTS_SECONDS = 31536000  # 1 year
    # SECURE_HSTS_INCLUDE_SUBDOMAINS = True
    # SECURE_HSTS_PRELOAD = True
    SECURE_SSL_REDIRECT = True
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True
else:
    DEBUG = True
    ALLOWED_HOSTS = ['localhost', '127.0.0.1']

I keep receiving the same error:

2024-10-08 13:19:14,906: Invalid HTTP_HOST header: 'sirjude.pythonanywhere.com'. You may need to add 'sirjude.pythonanywhere.com' to ALLOWED_HOSTS.
Traceback (most recent call last):
  File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__
    response = self.process_request(request)
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'sirjude.pythonanywhere.com'. You may need to add 'sirjude.pythonanywhere.com' to ALLOWED_HOSTS.
2024-10-08 13:19:15,151: Error running WSGI application
2024-10-08 13:19:15,152: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2024-10-08 13:19:15,152:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 124, in __call__
2024-10-08 13:19:15,153:     response = self.get_response(request)
2024-10-08 13:19:15,153: 
2024-10-08 13:19:15,153:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 140, in get_response
2024-10-08 13:19:15,153:     response = self._middleware_chain(request)
2024-10-08 13:19:15,153: 
2024-10-08 13:19:15,153:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 57, in inner
2024-10-08 13:19:15,154:     response = response_for_exception(request, exc)
2024-10-08 13:19:15,154: 
2024-10-08 13:19:15,154:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 140, in response_for_exception
2024-10-08 13:19:15,154:     response = handle_uncaught_exception(
2024-10-08 13:19:15,154: 
2024-10-08 13:19:15,155:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 181, in handle_uncaught_exception
2024-10-08 13:19:15,155:     return debug.technical_500_response(request, *exc_info)
2024-10-08 13:19:15,155: 
2024-10-08 13:19:15,155:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 69, in technical_500_response
2024-10-08 13:19:15,156:     html = reporter.get_traceback_html()
2024-10-08 13:19:15,156: 
2024-10-08 13:19:15,156:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 428, in get_traceback_html
2024-10-08 13:19:15,156:     c = Context(self.get_traceback_data(), use_l10n=False)
2024-10-08 13:19:15,156: 
2024-10-08 13:19:15,156:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 397, in get_traceback_data
2024-10-08 13:19:15,156:     "settings": self.filter.get_safe_settings(),
2024-10-08 13:19:15,156: 
2024-10-08 13:19:15,156:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 156, in get_safe_settings
2024-10-08 13:19:15,157:     settings_dict[k] = self.cleanse_setting(k, getattr(settings, k))
2024-10-08 13:19:15,157: 
2024-10-08 13:19:15,157:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 98, in __getattr__
2024-10-08 13:19:15,157:     raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

See https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/

Hello there add myrentindextracker.pythonanywhere.com to your allowed host like this

ALLOWED_HOSTS = ['myrentindextracker.pythonanywhere.com ', 'localhost', '127.0.0.1']