im writing a project using django.
while it was in a development stage i used DEBUG=TRUE, but now i want to upload it to heroku for production.
i set DEBUG=FALSE and kept getting an internal server error, so i further investigated and found that i need to set ALLOWED_HOSTS = ['localhost', '127.0.0.1'], so i did..
i ran it with the localhost but i still kept getting internal server error.
what am i doing wrong? cause i can't figure it out?
also, what should i put in allowed host for heroku site?
thanks
try this
python manage.py collectstatic
Remember that static files directory when debug = true is no the same when debug is false
heroku uses whitenoise for static files.
You need the domain you're accessing your server from in ALLOWED_HOSTS.
Example:
ALLOWED_HOSTS = ['www.example.com', ]
Yet, improperly configured ALLOWED_HOSTS should not result in internal server error responses (HTTP code 500) but in bad request responses (HTTP code 400).
Have you configured a way to log exceptions on your production server in order to investigate this?
Related
I've followed this tutorial and i'm running into an ERR_SSL_PROTOCOL_ERROR that i just can't figure out.
I must mention that i have set inside settings.py:
DEBUG = FAlSE
SECURE_SSL_REDIRECT = False
And my Allowed_hosts is ['*'].
I have a number of questions which reading alot of blog posts hasn't answered:
In the nginx configuration, if i'm running the server from a laptop connected to a router, and the external IP is 12.34.56.78 and the port is 50000, what am i supposed to put at server_name?
In the gunicorn configuration, everyone states that this is the config command: gunicorn --bind 0.0.0.0:8800 AWESOME.wsgi:application what IP should i put there in my case? 12.34.56.78:50000 or just leave it like it is?
As i understand the situation, given the setting in django settings.py my server is not serving HTTPS so the error does not come from that. I've also read about certbot but since i don't have even those 2 above questions figured out i cannot understand how to configure certbot..
EDIT
In fact i've tried to:
gunicorn --bind 12.34.56.78:50000 KYng.wsgi:application but i'm getting invalid address error
1.) You actually don't need to put anything in there, as it will default to an empty string.
2.) You should put localhost (127.0.0.1) if you want it to run locally, or 0.0.0.0 if you want it to run publicly.
Concerning the ssl error, check your nginx configuration more closely...that you didn't reference https instead of http somewhere...
After setting DEBUG = False, and SECURE_SSL_REDIRECT = True and deploying a version on my app to the server, I wish now to develop further locally. The problem is, I think at one point I forgot to remove the SECURE_SSL_REDIRECT = True from settings.py, and I ran the local dev server with heroku local. My local browser always tries not to connect to localhost with SSL, so it just hangs.
I tried removing the site-specific cookies for localhost in the browser settings (Chrome) but localhost now still always tries to establish an SSL connection.
I am trying just to get back to using a non-SSL local connection for development. Any Ideas?
Django version 1.10.2
Heroku
Thanks
EDIT
Seems if I clear ALL the cache and cookies and restart the browser then it will not ask for SSL again. So it seems to be a browser problem. Anyway if anyone has an idea of how to accomplish this without having to clear all the data in Chrome, that would be appreciated.
UPDATE
I have learned a better way to handle this situation. I have set up some code to automatically sense if the software is running on the local environment or the cloud production environment, like this:
if os.environ.get('LOCAL'):
DEBUG = True
SECURE_SSL_REDIRECT = False
else:
DEBUG = False
SECURE_SSL_REDIRECT = True
Of course you have to take care of setting up the environ object, which happens automatically in heroku.
I am using custom settings both for local (development) and production environments.
For instance:
myproject/settings/dev.py
DEBUG = True
SECURE_SSL_REDIRECT = False
...
myproject/settings/production.py
DEBUG = False
SECURE_SSL_REDIRECT = True
...
And then I specify the settings I want to use. On localhost like this:
python myproject/manage.py runserver --settings=settings.dev
and for production using the Heroku Procfile:
web: gunicorn myproject.wsgi.production --log-file -
Content of myproject/wsgi/production.py is:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.production")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
If you use heroku local for your local development, you can create similar Procfile.local with local wsgi file.
I have a Django site that uses Gunicorn and Nginx. Occasionally, I'll have a problem that I need to debug. In the past, I would shut down Gunicorn and Nginx, go to my Django project directory and start the Django development server ("python ./manage.py runserver 0:8000"), and then restart Nginx. I could then insert set_trace() commands and do my debugging. When I fixed the problem I'd shut down Nginx and then restart Gunicorn and Nginx. I'm pretty sure this was working.
Recently, though, I've begun having problems. What happens now is that when I've stopped at a breakpoint, after a couple of minutes the web page that I've stopped on will change and display "404 Not Found" and if I take another step in the debugger, I'll see this error:
- Broken pipe from ('127.0.0.1', 43742)
This happens on my development, staging, and production servers which I'm accessing via their domain names, e.g. "web01.example.com" (not really example).
What is the correct way to debug my Django application on my remote servers?
Thanks.
I figured out the problem. First I observed that when I stopped at a breakpoint, the page always timed out after exactly one minute which suggested that the Nginx connection to the web server was timing out if the web server took more than 60 seconds to respond. I then found an Nginx proxy_read_timeout directive which defines this timeout. Then it was merely a matter of changing the length of the timeout in my Nginx config file:
# /etc/nginx/sites-enabled/example.conf
http {
server {
...
location #django {
...
# Set timeout to 1 hour
proxy_read_timeout 3600s;
...
}
...
}
}
Once you've made this change you need to reload Nginx, not restart it, in order to this change to take effect. Then you start Django as I indicated above and you can now debug your Django application without it timing out. Just be sure to remove the timeout setting when you're done debugging, reload Nginx again, and restart Gunicorn.
Running django on nginx on my staging server I've got Server Error (500). Following this question I've set
ALLOWED_HOSTS = ['*']
But that did not solve my problem.
Does anyone know how I can see djangos exceptions?
Django sends an E-Mail to all ADMINS, when DEBUG is False and some EMAIL_* settings are properly set.
See:
https://docs.djangoproject.com/en/1.10/howto/error-reporting/
You can also setup an appropriate logging to do that:
https://docs.djangoproject.com/en/1.10/topics/logging/
for more information.
I'm using django 1.5.0 and apache 2.2.22. I can't seem to get my site running as a named vhost (I've got other django sites already running fine on this server).
I'm getting the standard apache 500 error page. My error log suggests that've I've got a problem with my ALLOWED_HOSTS setting -
[Tue Jun 04 10:25:22 2013] [error] [client 31.52.39.247] SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): sub_domain.mydomain.com
But my ALLOWED_HOSTS setting looks ok to me -
python manage.py shell
>> from django.conf import settings
>> settings.ALLOWED_HOSTS
['sub_domain.mydomain.com', 'livedomain.com']
Also - I don't know why I'm getting the apache 500 page rather than the django debug page -
>> settings.DEBUG
True
Looking at the full stack trace in the error log, I can see that the wsgi app is running - it's definitely running django code.
Any suggestions?
Firstly, ALLOWED_HOSTS should be ignored altogether when you have DEBUG=True so that's strange that you are getting validation errors.
I was having an issue similar to this with nginx where I had an underscore in my host name which meant Django didn't validate it. This might be an issue particularly if you are having Apache rewriting your host header (this was the case for me as nginx was reverse proxying) or your subdomain includes an _.
You can see how Django validates hostnames in the source, furthermore, I wrote a quick blog post on this also which might help