Skip to main content

Automatic Maintenance Page for Nginx+Django app

If you've used Django with Nginx, you are probably familiar with how to configure the Nginx process group to reverse proxy to a second Gunicorn or uWSGI Django process group.  (The proxy_pass Nginx parameter passes traffic through Nginx to Django.)

One benefit of this approach is that if your Django process crashes or if you are preforming an upgrade and take Django offline, Nginx can still be available to serve static content and offer some sort of "the system is down" message to users.  With just a few lines of configuration, Nginx can be set to automatically display a splash page in the above scenarios.

If the Django process running behind the reverse proxy becomes unavailable, a 502 error will be emitted by Nginx.  By default, that 502 will be returned to the browser as an ugly error message.  However, Nginx can be configured to catch that 502 error and respond with custom behavior.  Specifically, if a 502 is raised by Nginx, Nginx can check for a custom html error page document and serve up that document if it exists.  If that document does not exist, Nginx can just return the default ugly 502 error message.  The Nginx configuration below executes this described behavior.

This Nginx example is stripped down to just demonstrate the concept described.  It only shows the server section portion of the Nginx config.  This example assumes that you have created your custom error page at the following location: /sites/joejasinski/htdocs/50x.html.

server.conf
...

# reverse proxy boilerplate
upstream app_server {
    server unix:/sites/joejasinski/var/run/django.socket;

}

server {
    listen   443;
    server_name  joejasinski.com www.joejasinski.com;
    ...

    # if Django is unreachable, a 502 is raised...
    error_page 502 @502;
    location @502 {
      root   /sites/joejasinski/htdocs/;
      # try to load a file called 50x.html at the document root
      # or re-raise a generic 502 if no such file is present.
      try_files /50x.html =502;
   }

    # reverse proxy boilerplate for hosting the Django app 
    location / {
        try_files $uri @proxy_to_app;
    }

    # reverse proxy boilerplate for hosting the Django app
    location @proxy_to_app {
        ....
       proxy_pass http://app_server;
    }
}


It's worth mentioning that, by default, if your Django app raises a 502 error behind the reverse proxy, Nginx will not catch it and thus will not respond with the custom Nginx 502 page.  The Nginx 502 error page is only displayed for 502 errors that originate from Nginx; an unavailable reverse proxy application qualifies as such.

This behavior is desirable since Django has it's own error handler for handling 50x errors within the application; we want Nginx to display an error page when Django is down, but Django should handle all errors generated by itself.

Note: you can change this behavior to have Nginx handle any error generated by the reverse proxied application.  This can be done with the proxy_intercept_errors Nginx parameter.

Note: using a similar approach to the above, you can also create error Nginx-based error handlers for other error codes generated by Nginx.

In summary, this approach allows you to automatically show users a friendly error page if your Django process crashes or is down for maintenance.

Comments

  1. I think you misguide with article title. Because for maintenance page more suitable is 503 status code.

    ReplyDelete

  2. This blog website is pretty cool! How was it made !
    App Maintenance

    ReplyDelete
  3. Nice list of bloggers. From this blog I learned more and got to know more .Thank you for sharing this great post. Website Designer San Antonio

    ReplyDelete
  4. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.


    Java Training in Chennai

    Java Course in Chennai

    ReplyDelete
  5. You have shared a nice article here about this topic. Your article is very informative and nicely describes about heaters. Thanks for sharing this article here.Branding Companies Vancouver

    ReplyDelete
  6. Wonderful Article, which define very interesting information about the topic.Thanks for taking your time to share Maintenance Web Page If you wants more related services then must visit DP Websolutions.

    ReplyDelete
  7. I read your post and got it quite informative. I couldn't find any knowledge on this matter prior to. I would like to thank you for sharing this article here. web design clark county wa

    ReplyDelete
  8. This post is really awesome. Genuinely i like this blog. It gives me more useful information. I hope you share lots of things with us .Internet marketing company

    ReplyDelete
  9. Great!so fantastic, so inspiring .this is really amazing. i learned new information. thank you so much for sharing this great post. nidhi software company in india

    ReplyDelete
  10. I am attracted by the info which you have provided in the above post. It is genuinely good and beneficial info for us. Continue posting, Thank you.Miami website designer

    ReplyDelete
  11. This post is so useful and valuable to increase our knowledge. I am happy that you have shared great info with us. software company in india Grateful to you for sharing an article like this.

    ReplyDelete
  12. Thank you for providing me with such valuable information about magento conversion rate optimization. This article provided me with some useful knowledge. Continue to make posts like this.

    ReplyDelete
  13. Thanks for your post. It's very helpful post for us. You can also visit web design Vancouver WA for more Victor Steel related information. I would like to thanks for sharing this article here.

    ReplyDelete
  14. I read the above article and I got some different kind of information from your article about a mattress. It is a helpful article to enhance our knowledge for us. Thankful to you for sharing an article like this.Website Designing Course in Delhi

    ReplyDelete
  15. Great blog ! I am impressed with suggestions of author. handyman services ottawa

    ReplyDelete
  16. You have worked nicely with your insights that makes our work easy. The information you have provided is really factual and significant for us. Keep sharing these types of article, Thank you.Social Media Marketing Company in Fort St John

    ReplyDelete

Post a Comment

Popular posts from this blog

Docker: Run as non root user

It's good practice to run processes within a container as a non-root user with restricted permissions.  Even though containers are isolated from the host operating system, they do share the same kernel as the host. Also, processes within a container should be prevented from writing to where they shouldn't be allowed to as extra protection against exploitation. Running a Docker process as a non-root user has been a Docker feature as of version 1.10. To run a Docker process as a non-root user, permissions need to be accounted for meticulously.  This permission adjustment needs to be done when building a Dockerfile. You need to be aware of where in the filesystem your app might write to, and adjust the permissions accordingly.  Since everything in a container is considered disposable, the container process really shouldn't be writing to too many locations once build. Here is an annotated example of how you might create a Dockerfile where the process that runs within runs a

Django: Using Caching to Track Online Users

Recently I wanted a simple solution to track whether a user is online on a given Django site.  The definition of "online" on a site is kind of ambiguous, so I'll define that a user is considered to be online if they have made any request to the site in the last five minutes. I found that one approach is to use Django's caching framework to track when a user last accessed the site.  For example, upon each request, I can have a middleware set the current time as a cache value associated with a given user.  This allows us to store some basic information about logged-in user's online state without having to hit the database on each request and easily retrieve it by accessing the cache. My approach below.  Comments welcome. In settings.py: # add the middleware that you are about to create to settings MIDDLEWARE_CLASSES = ( .... 'middleware.activeuser_middleware.ActiveUserMiddleware' , .... ) # Setup caching per Django docs. In actuality, you

Django Docker and Celery

I've finally had the time to create a Django+Celery project that can be completely run using Docker and Docker Compose. I'd like to share some of the steps that helped me achieve this. I've created an example project that I've used to demo this process. The example project can be viewed here on Github. https://github.com/JoeJasinski/docker-django-demo/tree/blogpost To run this example, you will need to have a recent version of Docker and Docker Compose installed. I'm using Docker 17.03.1 and Docker Compose 1.11.2. Let's take a look at one of the core files, the docker-compose.yml .   You'll notice that I have defined the following services:   db - the service running the Postgres database container, needed for the Django app rabbitmq  - service running the RabbitMQ container, needed for queuing jobs submitted by Celery app  - the service containing Django app container worker  - the service that runs the Celery worker container web  - the servic