Suggestions on how to have error pages based on content type


I am wondering if it is possible to serve up error pages based on the
content type.

I.e. I want to show a different 503 error page for html, xml and
javascript content types.

I have tried to do something like this (with Nginx 0.6 series):

server {
listen 80;

charset off;
server_name errortest.localhost;

root /var/www/foobar/nginx_error_test;

if (-f /var/www/foobar/nginx_error_test/maintenance_on.html) {
  rewrite  ^(/images/.*)$  /images/server-maintenance.png break;
  return 503;

error_page   503 /system/maintenance;

location = /system/maintenance {
  auth_basic off;
  root /var/www/foobar/nginx_error_test/system;

  if ($content_type ~* javascript) {
    rewrite . /system/maintenance.json break;

  if ($content_type ~* xml) {
    rewrite . /system/maintenance.xml break;

  rewrite . /system/maintenance.html break;

if (-f $request_filename) {

if (-f $request_filename.html) {
   rewrite (.*) $1.html break;


Unfortunately, this does not work. All that happens is I get the
standard Nginx 503 html error response.

Any ideas if I can do this and if so what I am doing wrong?



I take it, what I am trying to do is not possible?


How about this (not tested)?

 if ($content_type ~* xml) {

if (-f $request_filename.html) {
rewrite (.*) $1.html last;


Phillip B Oldham
[email protected] mailto:[email protected]


This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to

This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.

Hi Phillip,

On 10/27/2009 05:02 PM, Phillip O. wrote:

if (-f $request_filename.html) {
rewrite (.*) $1.html last;


This is almost there!

Now when I do a request I will get the proper Content-Type response:

wget --header “Content-Type: application/javascript” -S -O -
–12:40:31-- http://errortest.localhost/index.json
=> `-’
Resolving errortest.localhost…
Connecting to errortest.localhost||:80… connected.
HTTP request sent, awaiting response…
HTTP/1.1 503 Service Temporarily Unavailable
Server: nginx/0.6.35
Date: Wed, 28 Oct 2009 03:40:31 GMT
Content-Type: application/javascript
Content-Length: 102
Connection: keep-alive
12:40:31 ERROR 503: Service Temporarily Unavailable.

But unfortunately, the output is not my maintenance.json output, it is
nothing in the case of wget and the default 500 internal server error in
a browser. Interestingly enough, the Content-Length is correct for the
maintenance.(json|xml|html) for the proper setting.

Any ideas why it would not show the rewritten maintenance page?


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs