Report this

What is the reason for this report?

Django-Gunicorn-Nginx Why do I get an Internal server error every time I restart Gunicorn?

Posted on September 22, 2014

I set up a droplet on digital ocean using the one-click installer. I host my code on a git repo. and I use git pull to merge changes, followed by service gunicorn reload to restart gunicorn. The problem is that everytime I do this and try to visit my site I get an ‘internal server error’ messages, and after I refresh once or twice the actual page loads.

It is strange because I get the message even if I wait for a while (15 minutes) before visiting the web page, so I’m not sure if I get this because gunicorn was still restarting or for some other reason. Any hints on what might be going on ?

P.S. I asked the same question on StackOverflow but got no hits



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This is quite an old question but for anyone else who may be experiencing this problem, the first thing I would suggest anytime you are receiving an error from your web server is to check the web server’s logs. In this case, reviewing the logs in /var/log/nginx/error.log would likely lead to the underlying cause of the internal server error.

The issue you’re describing is likely related to the services (Gunicorn or NGINX) not restarting gracefully or encountering errors during the restart process. To identify the root cause, checking the error logs of the involved services is crucial.

Key Steps to Diagnose the Issue:

  1. Check Gunicorn Logs:

    • Gunicorn might be failing during the reload process due to configuration issues, unhandled errors in your code, or problems with dependencies.
    • You can view the Gunicorn logs to identify the problem:
sudo journalctl -u gunicorn
  1. Check NGINX Logs:

    • If NGINX is failing to communicate with Gunicorn during or immediately after the restart, it might throw errors.
    • Check the NGINX error log for relevant messages:
sudo tail -f /var/log/nginx/error.log
  1. Check Your Application Logs:

    • If Gunicorn is starting correctly but your application code has issues (e.g., a missing migration, syntax error, or dependency), it may result in an “Internal Server Error.”
    • Locate and review your Django log files, typically found in the logs/ directory of your project or configured in your settings.

Potential Causes:

  • Gunicorn Overlap: During the reload, old processes may still handle some requests before the new ones fully take over.
  • Database or Migration Issues: If migrations or database changes are not applied, your application may behave inconsistently during restarts.
  • Cached Browser Error: If you’re refreshing the page quickly after a reload, your browser might briefly cache a failed request.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.