0

When starting my Django app on Azure I get a log message saying "Invalid HTTP_HOST header: " then some ip and "You may need to add" sayd ip "to allowed hosts". Now I assumed to put that ip hard coded is not the answer but tried anyway. I then redeployed and got the same message again but with another Ip. My ALLOWED_HOSTS are

[os.environ['WEBSITE_HOSTNAME'], os.environ['DB_SERVER']. 

Can I get that ip dynamically and add it to allowed hosts?

Yannick Widmer
  • 1,286
  • 3
  • 19
  • 30
  • Allowed hosts are the servers/users/bots IPs through which they can access your application. From my point of view, it is very common for someone to view your server IP through DNS query and then directly hit the server. By doing that, it will log that someone with an unlisted IP tried to access it. However, the server IP should not be visible to the user for security reasons, and one must add a proxy to the server IP to hide it from users and hackers. – K14 Sep 28 '22 at 13:05
  • Your comment seems related to my question but I don't understand what I should do. I get an error and a suggestion to add an ip but since after each deployment the ip changes it I would need to log on the server each time to change that. – Yannick Widmer Oct 11 '22 at 14:23
  • This is what I did when I faced that problem. Obviously, you can look for an alternative, or someone else can better guide you. However, if you have a domain, like www.somethingrandom.com, and you have access to this domain, then (1) create an account on cloudflare.com. 2. Transfer your domain or change your namespace server to Cloudflare to take advantage of free Cloudflare services such as the proxy I mentioned earlier. After that, you need to add the DNS record of your server and turn the proxy on against that DNS record. – K14 Oct 11 '22 at 17:17

1 Answers1

0

At a minumum, you should have your Azure VM's IPV4 address, and the domain(s) you have registered (assuming DNS is pointing to the IP). If you are using Azure's built in DNS service and have something like your-app-name.eastus.cloudapp.azure.com, you can add that as well.

ALLOWED_HOSTS = ['000.00.00.00', 'yourdomain.com', 'www.yourdomain.com']

More information here in the Django Docs.

Milo Persic
  • 985
  • 1
  • 7
  • 17
  • But I don't have the VM's IPV4 this changes each time I update my app, meaning if I get the ip then change it in the code and restart my webb app it changes or at least that is what I assume from the fact that the error gives me a new IP missing in ALLOWED_HOSTS. This is why I tried with the os.environ but I don't know what these variables actually are. – Yannick Widmer Nov 16 '22 at 13:01
  • Perhaps this answer will help you: https://stackoverflow.com/questions/40488443/how-can-i-determine-the-ip-address-of-an-azure-hosted-webapp – Milo Persic Nov 16 '22 at 15:01