Setting Content-Type response header

200 views
Skip to first unread message

edo888

unread,
Apr 7, 2016, 7:49:48 PM4/7/16
to openresty-en
Hi,

I'm trying to override Content-Type response header by using:

header_filter_by_lua 'ngx.header.content_type = "text/html"';

However the charset is not being replaced and stays in the response.

How can I set/remove charset in Content-Type header?

Thanks!

Robert Paprocki

unread,
Apr 7, 2016, 9:32:27 PM4/7/16
to openre...@googlegroups.com
Your example seems fine:

root@soter:/usr/local/openresty/nginx# grep default_type conf/nginx.conf
    default_type  text/plain;

root@soter:/usr/local/openresty/nginx# cat conf/nginx.conf
[...snip...]
location /html-test {
content_by_lua 'ngx.say("<html><head><title>Hello</title></head><body>Hello, world!</body></html>")';

header_filter_by_lua 'ngx.header.content_type = "text/html"';
}


poprocks@soter:~$ curl localhost/html-test
<html><head><title>Hello</title></head><body>Hello, world!</body></html>
poprocks@soter:~$ curl -I localhost/html-test
HTTP/1.1 200 OK
Server: openresty/1.9.7.4
Date: Fri, 08 Apr 2016 01:31:11 GMT
Content-Type: text/html
Connection: keep-alive


Can you post a full, reproducible example of your configuration>

--
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/d/optout.

edo888

unread,
Apr 7, 2016, 10:39:08 PM4/7/16
to openresty-en, rob...@cryptobells.com
The header is Content-Type: text/html;charset=XXX

After it is replaced, it becomes

Content-Type: text/html; charset=XXX

If I force charset

header_filter_by_lua 'ngx.header.content_type = "text/html; charset=UTF-8"';

it becomes

Content-Type: text/html; charset=UTF-8; charset=XXX

Thanks!

Robert Paprocki

unread,
Apr 7, 2016, 11:00:40 PM4/7/16
to openre...@googlegroups.com
Sorry, your example isn't particularly clear. Can you please post a simple example of your -entire- config file, not just snippets, so we can examine the problem? Also  note that "charset" is relatively meaningless in this context. We will care only about the entire contents of the Content-Type header. Thanks!

Yichun Zhang (agentzh)

unread,
Apr 15, 2016, 4:14:11 PM4/15/16
to openresty-en
Hello!
See the standard "charset" directive of nginx:

http://nginx.org/en/docs/http/ngx_http_charset_module.html#charset

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