Nginx Lua adds the Content-Type header to responses

2,330 views
Skip to first unread message

avi...@adallom.com

unread,
Jun 5, 2013, 7:32:43 AM6/5/13
to openre...@googlegroups.com
Hi,

I'm implementing a proxy using Nginx and Nginx-Lua.
When the remote server (to which I'm proxying) sends a response without a "Content-Type" header, the Nginx-Lua module adds it (and sets its value to the default Nginx type).

When a browser receives a response with no "Content-Type" header, it assumes that it is "text/html", while the default Nginx type is usually "application/octet-stream". 
In such cases, the addition of the header would causes the browsers to download the HTML files and not to load them.

Viewing (and debugging) the code, I've notice that this happens in two places in the code - the function ngx_http_lua_ngx_header_set and the function ngx_http_lua_send_header_if_needed. Removing these lines solves the problem...

Why is there an addition of the "Content-Type" header in the response headers if it is not set? If there is a good reason, is it okay to add a configuration directive stating whether or not we want to set this field?

I'll be happy to post a patch (or a pull request) :-)

Regards
Aviram

David | StyleFlare

unread,
Jun 5, 2013, 7:35:40 AM6/5/13
to openre...@googlegroups.com
Why not simply set the default content type to text/html

if thats what the browser is expecting anyway?
> --
> You received this message because you are subscribed to the Google
> Groups "openresty-en" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to openresty-en...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

avi...@adallom.com

unread,
Jun 5, 2013, 7:43:19 AM6/5/13
to openre...@googlegroups.com
That may cause Nginx issues, of the Nginx server treating stuff as "text/html" when they should be "application/octet-stream"...

David | StyleFlare

unread,
Jun 5, 2013, 7:56:52 AM6/5/13
to openre...@googlegroups.com
I see. But cant you isolate that to your context? Seems it would not be a nginx global setting. Just the default for cases when you dont have content/type header.

agentzh

unread,
Jun 7, 2013, 12:00:06 AM6/7/13
to openresty-en
Hello!

On Wed, Jun 5, 2013 at 6:32 AM, aviram wrote:
> I'm implementing a proxy using Nginx and Nginx-Lua.
> When the remote server (to which I'm proxying) sends a response without a
> "Content-Type" header, the Nginx-Lua module adds it (and sets its value to
> the default Nginx type).
>

Yes, this is the intended behavior of ngx_lua. Maybe we could add an
nginx directive to disable this functionality? Patches welcome :)

Also, as suggested by David, you can just configure the default_type
directive to "text/html" in your specific location blocks.

Best regards,
-agentzh

Yichun Zhang (agentzh)

unread,
Jul 12, 2015, 12:23:38 AM7/12/15
to openresty-en
Hello!

On Fri, Jun 7, 2013 at 12:00 PM, agentzh wrote:
> On Wed, Jun 5, 2013 at 6:32 AM, aviram wrote:
>> I'm implementing a proxy using Nginx and Nginx-Lua.
>> When the remote server (to which I'm proxying) sends a response without a
>> "Content-Type" header, the Nginx-Lua module adds it (and sets its value to
>> the default Nginx type).
>
> Yes, this is the intended behavior of ngx_lua. Maybe we could add an
> nginx directive to disable this functionality? Patches welcome :)
>

Just for the record, the following directive can be used to control this:

https://github.com/openresty/lua-nginx-module#lua_use_default_type

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages