# Serving XHTML as HTML to MSIE browsers in NginX

**URL:** <https://www.ruby-forum.com/t/serving-xhtml-as-html-to-msie-browsers-in-nginx/164801>\
**Category:** NGINX\
**Created:** [April 6, 2009, 6:40am UTC](https://www.ruby-forum.com/t/serving-xhtml-as-html-to-msie-browsers-in-nginx/164801 "2009-04-06T06:40:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottcable](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@elliottcable](https://www.ruby-forum.com/u/elliottcable)\
**Post date:** [April 6, 2009, 6:40am UTC](https://www.ruby-forum.com/t/serving-xhtml-as-html-to-msie-browsers-in-nginx/164801/1 "2009-04-06T06:40:10Z")

</div>

I’m trying to configure my server to serve XHTML files to MSIE browsers  
as text/html.

Here’s the three things I’ve tried; all of them yell at me about a  
syntax error, so I can’t launch my server:

location ~\* .(xhtml|xhtm) {  
if ($http\_user\_agent ~\* MSIE) {  
types { }  
default\_type text/html  
}  
}

if ($http\_user\_agent ~\* MSIE) {  
location ~\* .(xhtml|xhtm) {  
types { }  
default\_type text/html  
}  
}

includes.conf:  
if ($http\_user\_agent ~\* MSIE) {  
include /srv/conf/nginx/msie.conf;  
}

msie.conf:  
location ~\* .(xhtml|xhtm) {  
types { }  
default\_type text/html  
}

I’ve tried everything I can think of at this point. I can’t find  
documentation on what’s allowed inside of an if() clause; it just keeps  
throwing syntax errors at me.

I’ve also tried googling for other examples of the if() clause; all the  
examples I could find only ever used it in this form:

if (-f â€¦)

Is that the only thing the if() clause is useful for?

I’ve got some time sensitive content that _needs_ to get online, and  
moreover, be visible to MSIE users (that’s a first for me). I don’t have  
the time to re-write my CMS to serve HTML to MSIE either; this should  
really be done from the server side anyway. Any help would be much  
appreciated!

Posted at Nginx Forum: [Serving XHTML as HTML to MSIE browsers in NginX](http://forum.nginx.org/read.php?2,839,839#msg-839)

---

<div class="post-metadata">

**Author:** ![elliottcable](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@elliottcable](https://www.ruby-forum.com/u/elliottcable)\
**Post date:** [April 6, 2009, 9:48am UTC](https://www.ruby-forum.com/t/serving-xhtml-as-html-to-msie-browsers-in-nginx/164801/2 "2009-04-06T09:48:02Z")

</div>

Okay, I talked to the nice people in the #nginx IRC room a bit (thanks  
“merlincorey” and “Damn”!). From a more careful reading of the NginX  
wiki’s documentation, it appears that this should be legal:

> <https://github.com/elliottcable/server-configuration/blob/ad48f0b73ea8e2d33fc97bba60ddad2a4abca064/nginx/includes.conf#L7-12>

Here’s the reasoning. The wiki declares that the `types {}` declaration  
is legal within a `location /â€¦/ {}` declaration: [http://tr.im/ijbz?nginx](http://tr.im/ijbz?nginx)

- it also declares that the inside of an `if () {}` declaration should  
inherit context from outside the `if`: [http://tr.im/ijbK?nginx](http://tr.im/ijbK?nginx) (to  
quote, “Configuration inside directive if is inherited from the previous  
level.”). Therefore, having a `types` declaration inside an `if` inside  
a `location` should be legal.

Unfortunately. with the configuration file as linked above, I can’t  
launch NginX; I get an “emergency” level error about syntax that kills  
it on launch.

Is this a bug, then? Or is the documentation wrong? In either case, what  
can I do to get my server fixed to (im)properly serve XHTML to MSIE?

Posted at Nginx Forum: [Re: Serving XHTML as HTML to MSIE browsers in NginX](http://forum.nginx.org/read.php?2,839,842#msg-842)

---

<div class="post-metadata">

**Author:** ![elliottcable](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@elliottcable](https://www.ruby-forum.com/u/elliottcable)\
**Post date:** [April 6, 2009, 11:16am UTC](https://www.ruby-forum.com/t/serving-xhtml-as-html-to-msie-browsers-in-nginx/164801/3 "2009-04-06T11:16:02Z")

</div>

Hello!

On Mon, Apr 06, 2009 at 03:39:13AM -0400, elliottcable wrote:

> Okay, I talked to the nice people in the #nginx IRC room a bit (thanks “merlincorey” and “Damn”!). From a more careful reading of the NginX wiki’s documentation, it appears that this should be legal:
> 
> [server-configuration/nginx/includes.conf at ad48f0b73ea8e2d33fc97bba60ddad2a4abca064 · ELLIOTTCABLE/server-configuration · GitHub](http://github.com/elliottcable/server-configuration/blob/ad48f0b73ea8e2d33fc97bba60ddad2a4abca064/nginx/includes.conf#L7-12)
> 
> Here’s the reasoning. The wiki declares that the `types {}` declaration is legal within a `location /â€¦/ {}` declaration: [http://tr.im/ijbz?nginx](http://tr.im/ijbz?nginx) - it also declares that the inside of an `if () {}` declaration should inherit context from outside the `if`: [http://tr.im/ijbK?nginx](http://tr.im/ijbK?nginx) (to quote, “Configuration inside directive if is inherited from the previous level.”). Therefore, having a `types` declaration inside an `if` inside a `location` should be legal.

Directives allowed within if() blocks has context “if” or “if in  
location” explicitly listed in their documentation. Directives  
types and default\_type aren’t allowed inside if’s.

> Unfortunately. with the configuration file as linked above, I can’t launch NginX; I get an “emergency” level error about syntax that kills it on launch.
> 
> Is this a bug, then? Or is the documentation wrong? In either case, what can I do to get my server fixed to (im)properly serve XHTML to MSIE?

Try something like this:

```
location ~ \.xhtml$ {
    error_page 405 = @html;

    if ($http_user_agent ~* MSIE) {
        return 405;
    }
}

location @html {
    types {};
    default_type text/html;
}

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![elliottcable](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@elliottcable](https://www.ruby-forum.com/u/elliottcable)\
**Post date:** [April 7, 2009, 4:55am UTC](https://www.ruby-forum.com/t/serving-xhtml-as-html-to-msie-browsers-in-nginx/164801/4 "2009-04-07T04:55:46Z")

</div>

## Maxim D. Wrote:

> > 
> 
> the `if`: [http://tr.im/ijbK?nginx](http://tr.im/ijbK?nginx) (to quote,  
> if’s.  
> Try something like this:  
> types {};  
> default\_type text/html;  
> }
> 
> Maxim D.
> 
> > Posted at Nginx Forum:  
> > [http://forum.nginx.org/read.php?2,839,842#msg-842](http://forum.nginx.org/read.php?2,839,842#msg-842)

That seems like â€¦ a horrid hack. Is there seriously no better way around  
this?

Posted at Nginx Forum: [http://forum.nginx.org/read.php?2,839,846#msg-846](http://forum.nginx.org/read.php?2,839,846#msg-846)
