Conditionally changing MIME type in nginx

Posted by Peter on Server Fault See other posts from Server Fault or by Peter
Published on 2010-04-06T23:18:14Z Indexed on 2010/04/06 23:23 UTC
Read the original article Hit count: 541

Filed under:
|
|

I'm using nginx as a frontend to Rails. All pages are cached as .html files on disk, and nginx serves these files if they exist. I want to send the correct MIME type for feeds (application/rss+xml), but the way I have so far is quite ugly, and I'm wondering if there is a cleaner way.

Here is my config:

location ~ /feed/$ {
  types {}
  default_type application/rss+xml;
  root /var/www/cache/;
  if (-f request_filename/index.html) {
    rewrite (.*) $1/index.html break;
  }
  if (-f request_filename.html) {
    rewrite (.*) $1.html break;
  }
  if (-f request_filename) {
    break;
  }
  if (!-f request_filename) {
    proxy_pass http://mongrel;
    break;
  }

}

location / {
  root /var/www/cache/;
  if (-f request_filename/index.html) {
    rewrite (.*) $1/index.html break;
  }
  if (-f request_filename.html) {
    rewrite (.*) $1.html break;
  }
  if (-f request_filename) {
    break;
  }
  if (!-f request_filename) {
    proxy_pass http://mongrel;
    break;
  }
}

My questions:

  1. Is there a better way to change the MIME type? All cached files have .html extensions and I cannot change this.
  2. Is there a way to factor out the if conditions in /feed/$ and /? I understand that I can use include, but I'm hoping for a better way. Putting part of the config in a different file is not that readable.
  3. Can you spot any bugs in the if conditions?

I'm using nginx 0.6.32 (Debian Lenny). I prefer to use the version in APT.

Thanks.

© Server Fault or respective owner

Related posts about nginx

Related posts about mod-rewrite