问题
I have a webapp which required authentication to access any page of it. But for my ELB to work I have to setup health-check page for ELB so that ELB discover django app.
This page should return HTTP 200 and no auth required. How do I setup this with django/nginx world.
回答1:
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.
回答2:
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/
回答3:
@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.
来源:https://stackoverflow.com/questions/32920688/how-to-setup-health-check-page-in-django