WebFaction
Community site: login faq

I'm running a django app, and whenever apache is down I get a 502 – Bad Gateway page. How do I customize that page? I read about using ErrorDocument but I'm not sure where to put that.

asked 27 Feb '13, 12:05

floorlamp
1112
accept rate: 0%


If you want to use a custom ErrorDocument, you have to proxy your back-end server through Apache (via a static/cgi/php app).

For example:

  1. Create a static/cgi/php app.
  2. Create a file named down.html in the new app's directory - this will be your custom error page, so make it say whatever you want it to say.
  3. Create a .htaccess file in the new app's directory with the following, and change "12345" to the port number assigned to your back-end server (ie your Django app, Node app, whatever).
    ErrorDocument 502 /down.html
    ErrorDocument 503 /down.html
    
    RewriteEngine on
    RewriteBase /
    RewriteCond   %{REQUEST_URI}  !^/down.html
    RewriteRule ^(.*)$ http://127.0.0.1:12345/$1 [P,L]
  4. In the control panel, configure your site to serve the static/cgi/php app instead of the back-end server app.
  5. When all of this is done, requests to your site will be proxied through our shared Apache instance. If your back-end app is down, then Apache will serve up `down.html` as the error message. Hope that helps!
permanent link

answered 01 Dec '14, 18:57

seanf
12.1k21636
accept rate: 37%

Sean, thanks, this was easy to set up but I have one weird result when redirecting. All pages successfully redirect to my django app when it's running EXCEPT the home page (just "/" or the base domain name). For that page only, I get "The requested URL /index.html was not found on this server." This is not the custom 404 page I've implemented in my django app, so it seems like /index.html is getting requested somehow before it gets to the django app, but only at that base URL. All others, like mydomain.com/otherpage/ are fine. Any idea how to fix that?

(15 Feb, 20:41) Danos2000

And to follow up, it looks like all 404s are not my custom one, so I suppose those get handled differently. Maybe then the only problem is that when I go to the base site for the proxy app, http://mydomain.com/, it's being auto-redirected from / to /index.html because it's a static app?

(15 Feb, 21:21) Danos2000

This is not possible for a 502 as it would require modifying the front-end nginx server. ErrorDocument can be used for other errors such as 404.

permanent link

answered 27 Feb '13, 22:04

johns ♦♦
5.2k211
accept rate: 23%

Is there any other way I can show a custom message to users in case of unexpected downtime?

(28 Feb '13, 11:51) floorlamp

You can run a script via cron that does the following:

  • Check the response code of your site
  • If it gets a 502 response, switch your site over to a static-only app to serve your custom error page
  • Attempt to restart your processes
  • Switch the site back to your Django app when it detects that your app is running correctly.

The second and last steps can be done via our control panel API.

Hope that helps!

As it turns out, this solution is impractical because it takes a couple of minutes for the web server config to pick up the changes made by the script - so, by the time the error page is being served. Please refer to my other answer for a more practical solution.

(28 Feb '13, 12:34) seanf
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×221
×21

question asked: 27 Feb '13, 12:05

question was seen: 3,326 times

last updated: 15 Feb, 21:21

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM