2

I was trying to deploy my django app on pythonanywhere.

Traceback from apache error log:

2013-07-23 09:31:35,058 :Internal Server Error: /favicon.ico
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 89, in get_response
    response = middleware_method(request)
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py", line 55, in process_request
    host = request.get_host()
  File "/usr/local/lib/python2.7/dist-packages/django/http/__init__.py", line 179, in get_host
    "Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): %s" % host)
SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): monty_singh.pythonanywhere.com

I tried ['*'] and ['monty_singh.pythonanywhere.com'] as values for ALLOWED_HOSTS but the app is still not working. No matter what value I set for ALLOWED_HOSTS I get the same error.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    Check [this blog](http://timmyomahony.com/blog/2013/04/24/suspiciousoperation-invalid-http_host-header-django/) and [this post](http://stackoverflow.com/questions/15238506/djangos-suspiciousoperation-invalid-http-host-header) – karthikr Jul 23 '13 at 14:48
  • 1
    @karthikr I've already read those posts(before posting the question). First one is related to `nginx`, so I am not sure how to use that for apache server, and second one also didn't help. – Ashwini Chaudhary Jul 23 '13 at 14:56
  • One trivial question - did you restart apache after changing the settings ? Also, i would recommend using nginx on top of apache for better performance – karthikr Jul 23 '13 at 15:00
  • So you tried already removing the `_` from your hostoname? Doesn't it validate if you use a wildcard instead? eg: `*.pythonanywhere.com`. Also, just `.pythonanywhere.com` should be fine and working for you – Samuele Mattiuzzo Jul 23 '13 at 15:00
  • @SamueleMattiuzzo Nop, neither of them worked, but I get a longer error when I use `*.pythonanywhere.com`. – Ashwini Chaudhary Jul 23 '13 at 15:07
  • @karthikr Yes I reload my server every time I modify any setting. – Ashwini Chaudhary Jul 23 '13 at 15:09
  • The blog to which karthikr pointed suggests that using an underscore in the HTTP request will confuse the server. Therefore, regardless of the setting of `ALLOWED_HOSTS`, try to access the server using a different host name (change it in the DNS or whatever). – Antonis Christofides Jul 23 '13 at 15:15
  • PythonAnywhere dev here: It looks like Django doesn't like having an underscore in the host. I'll have a look at the Django source and see WHY... – aychedee Jul 23 '13 at 15:22
  • 1
    @karthikr Now that is so embarrassing, missed the most import line in that post: *Make sure that the host name you are passing to your application server doesn't have an `_` in it.* – Ashwini Chaudhary Jul 23 '13 at 15:48

2 Answers2

6

When Django validates the host from the HTTP request it attempts to match it against the following regex which you can find in the source here:

host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$")

The underscore in your username is causing Django to reject the host name on the request. You can create a new PythonAnywhere account with a new username or use a different web framework.

aychedee
  • 24,871
  • 8
  • 79
  • 83
  • Was already creating a new account, I think pythonanywhere should add a warning related to that when someone chooses a username. Thanks for your help. – Ashwini Chaudhary Jul 23 '13 at 15:49
  • 1
    We did discuss just not letting them in usernames. But you make a good point! And it should certainly go in the new wiki. This is the first time it has actually bitten someone. – aychedee Jul 23 '13 at 16:51
  • 1
    Actually, looking through the code. We don't allow usernames with underscores to be created any more. We made this change about a year ago. However some older accounts may still have `_` in them. – aychedee Jul 24 '13 at 10:49
  • Yup, my account was around 1.5 years old. – Ashwini Chaudhary Jul 24 '13 at 11:50
1

From your pyhonanywhere home go to files and delete 'cache' if there is any then, locate the settings.py file inside your project folder, add asters or the link that seems unallowed to: ALLOWED_HOSTS = ['*'] then come back to dashboard>>web and press 'Reload' button, then click on the top above Reload button.