[Fixed]-How to setup health check page in django

0πŸ‘

βœ…

NOT RECOMMENDED – this was a quick and dirty way to get Django up and running with ELB, but in most cases a health check response should be returned directly from your application code. See the rest of the answers for a better solution.


This is something better handled by nginx, so the fact that it’s serving a django app should make no difference.

You will first need to configure ELB’s health check to ping a specific URL of your instance, say /elb-status. You can follow the instructions here: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-healthchecks.html#update-health-check-config

After that, all you need to do is set up nginx to always send back an HTTP 200 status code. You can add something like this to your server block in nginx.conf:

location /elb-status {
    access_log off;
    return 200;
}

See this answer for more details.

πŸ‘€gbs

18πŸ‘

Here is another solution using Django Middleware.

from django.http import HttpResponse


class HealthCheckMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if request.path == '/health':
            return HttpResponse('ok')
        return self.get_response(request)

And put your HealthCheckMiddleware in front of other middlewares in settings.py

MIDDLEWARE = [
    'yourdjangoapp.middleware.HealthCheckMiddleware',
    ......
]

And your application will always respond to path /health with ok as long as your app is running regardless of any configurations and authentication.

πŸ‘€Watt Iamsuri

10πŸ‘

You can use django-health-check 3rd party app.

Install it using pip –

pip install django-health-check 

Configue URL as –

urlpatterns = [
    # ...
    url(r'^health_check/', include('health_check.urls')),
]

and add the health_check applications to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'health_check',                             # required
]

More details : https://github.com/KristianOellegaard/django-health-check

Now you can confiure ELB health check url as – /health_check/

πŸ‘€Aniket Thakur

1πŸ‘

Had the same issue at one point and we ended up creating django-heartbeat:

https://github.com/pbs/django-heartbeat

It provides an endpoint for making sure your app is up and running.
Plus some other useful "checkers" that you can enable, out of the box, to surface information about like:

  • build version
  • python, django or other dependencies
  • database(s), cache(s) information, etc

https://github.com/pbs/django-heartbeat#available-checkers

If you find it useful, feel free to ⭐ it.

If you need any additional functionalities or want to contribute – let us know or just go ahead and open a PR.

Thanks!

0πŸ‘

@app.route('/elb-health-check')
def check_httpd_service():
    server = xmlrpclib.Server('http://'+supervisor_host+':'+supervisor_rpc_port+'/RPC2')

    state = server.supervisor.getProcessInfo(process)
    print state['statename']
    if state['statename'] == 'RUNNING':
        return state['statename'], 200
    else:
        return state['statename'], 500

Define the variables accordingly.

The above code is an example to create a custom api without authentication. It can be invoked by HTTP GET request method and It can be used to perform health check from ELB.

It is just an example, you can use your own idea with flask/django to create a custom health check service for ELB.

Here I am control my application with supervisor, a process control system. From supervisor I can track the status of the application. Supervisor reloads the application, if it is exited.

http://supervisord.org/

If the service is running, the server will return 200 response code. If the service is not running on the host, it will return 500 response code. Based on this response code, we can route the traffic in the load balancer.

Leave a comment