On Wed, May 2, 2012 at 12:03 AM, Roman Shishkin <r.sh...@gmail.com> wrote:
>
> Now I am on this way. Step by step ;)
> Heres is my current Lua code:
>
> -- Disable gzipped response
> ngx.req.clear_header("Accept-Encoding");
>
> -- Catch Nginx proxy pass location
> local res = ngx.location.capture("/proxy-pass");
>
> -- Perform redirect
> if res.status == 301 or res.status == 302 then
> return ngx.redirect(res.header["Location"], res.status);
> end
>
> -- Put headers from subrequest
> if res.header then
>
> -- Fix Charset
> if res.header["Content-Type"] then
> local newheader, n = ngx.re.sub(res.header["Content-Type"],
> "charset=ISO-8859-1", " UTF-8", "io");
> if newheader then
> res.header.content_type = newheader;
> end
> end
>
> -- Put headers from subreq to main req
> for key, value in pairs(res.header) do
> ngx.header[key] = value
> end
> end
> ngx.print(res.body)
>
It should not be that complex. I think the following should be sufficient:
local res = ngx.location.capture("/proxy")
ngx.status = res.status
for k,v in pairs(res.header) do
ngx.header[k] = v
end
ngx.print(res.body)
Also, put the following line into your /proxy location:
proxy_set_header Accept-Encoding "";
>
> So I have the following issues:
>
> 1. The ngx_http_charset_module doesn't change charset in Content-Type
> header, so I have to do it manually
>
That's weird. Consider the following example:
location /lua {
charset GBK;
content_by_lua '
ngx.header.content_type = "text/xml; charset=UTF-8"
ngx.say("hi")
';
}
Then GET /lua gives the response header "Content-Type: text/xml;
charset=UTF-8" as expected.
Also consider the following:
location /lua {
charset GBK;
content_by_lua '
ngx.header.content_type = "text/xml"
ngx.say("hi")
';
}
And then we get the response header "Content-Type: text/xml;
charset=GBK", also as expected.
These two examples both come from the ngx_lua test suite. Could you
please try them out on your side?
> 2. In the response body completely dissapeared two string:
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> <meta http-equiv="Content-Language" content="en-us" />
>
> Is there any magic with response body in the ngx.location.capture?
No, ngx_lua did not modify the response body of a subrequest at all.
Are these two lines are added by any of other Nginx output filter
modules? If yes, then you should configure those modules in the
location configured by ngx_lua, rather than the original location
accessed via subrequests.
> May be the ngx_http_charset_module incompatible with ngx_lua or some
> problen in lua?
>
Can you provide a minimal configuration that can reproduce this
problem? As I've mentioned above, it should work :) Just ensure that
you configure them in the location configured by ngx_lua because the
ngx_charset output filter runs *after* ngx_lua's subrequest capturing
filters.
Best regards,
-agentzh