lua-nginx-module icon indicating copy to clipboard operation
lua-nginx-module copied to clipboard

ngx.exit and custom body

Open rest17 opened this issue 6 years ago • 7 comments

Trying to return custom error page from access_by_lua block. nginx conf as below. server { listen 8081; server_name vman-dev-tools222.cisco.com; root /usr/local/tier1app/gceasy.war; set $is_exit ""; set $user_id ""; error_page 503 /5xx.html; access_log /var/log/nginx/access-tools.log toolslog; error_log /var/log/nginx/error-tools.log error; location / { root /tmp/; #log_by_lua_file /etc/nginx/lua/request_logger.lua; if ($request_uri ~* /?userid(.*)$) { access_by_lua_block { local reqcookies = ngx.req.get_headers()["cookie"] local arguserid=ngx.req.get_uri_args()["userid"] ngx.log(ngx.ERR, "The Request cookie is :",reqcookies) ngx.log(ngx.ERR, "The Userid is :",arguserid) if reqcookies ~= nil then ngx.log(ngx.ERR, "This is the request from user second time when cookie is present in the requesy header") reqt = {} for k, v in string.gmatch(reqcookies, "(%-%w+)=(%-%w+)") do reqt[k] = v end local filesession ,errsession= io.open("/tmp/JSESSION.txt", "r") local fileuser,erruser = io.open("/tmp/USER.txt", "r") if errsession == nil and erruser == nill then ngx.log(ngx.ERR, "Inside access block by lua the file is already present") local sessiondata, err = filesession:read(512) local userdata,err1=fileuser:read(512) if reqt.JSESSIONID ~= sessiondata and arguserid ~= userdata then filesession:close() fileuser:close() ngx.log(ngx.ERR, "user id didn't match") ngx.status = 503 return ngx.exit(503) end end else

On browser is still showing default 503 error page

rest17 avatar May 01 '20 23:05 rest17

This is simialr to issue 933

rest17 avatar May 01 '20 23:05 rest17

@rest17 As my test in openresty/1.17.8.1rc1, the code like this is work:

server {
    listen       8004;
    error_page  500  =301 http://example.com/notfound.html;
    location / {
        content_by_lua_block {
            ngx.exit(500)
        }
    }
}

ngx.status can't be set because maybe header is sent, and exit_code will be 200, and not matter response header is sent or exit code is 200, the error page should not take effect.

rainingmaster avatar May 04 '20 13:05 rainingmaster

You can do somehting like this if you want to render a custom message with the error. Or similar with the error handler and lua too

location / {
  access_by_lua_block {
    ngx.status = 500
    ngx.say('hello from custom error code')
    return
  }
}

Cleaned up curl example with the above code

$ curl 127.0.0.1  -v
> GET / HTTP/1.1
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
< Server: openresty/1.15.8.3
< Date: Mon, 04 May 2020 14:04:01 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
hello from custom error code

jamo avatar May 04 '20 14:05 jamo

Thanks Jarmo,

We can close the bug. I was able to render the custom html. With ngx.say the browser was downaloding the message but not showing on browser.

If we do only return nginx.status(500) and then have nginx error_page directive configured for the location. I was able to render custom html.

Regards chitralekha

On Mon, May 4, 2020 at 7:03 AM Jarmo Isotalo [email protected] wrote:

You can do somehting like this if you want to render a custom message with the error. Or similar with the error handler and lua too

location / { access_by_lua_block { ngx.status = 500 ngx.say('hello from custom error code') return } }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/openresty/lua-nginx-module/issues/1702#issuecomment-623483193, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF6OVIFIE7ZRI6QQQBKSGKTRP3DKRANCNFSM4MXNTIRQ .

rest17 avatar May 05 '20 19:05 rest17

browser was downaloding the message but not showing

just set proper content type via nginx, for example default_type text/html; or in lua ngx.header.content_type = "text/html"

hroost avatar May 05 '20 23:05 hroost

Thanks Jarmo, We can close the bug. I was able to render the custom html. With ngx.say the browser was downaloding the message but not showing on browser. If we do only return nginx.status(500) and then have nginx error_page directive configured for the location. I was able to render custom html. Regards chitralekha … On Mon, May 4, 2020 at 7:03 AM Jarmo Isotalo @.***> wrote: You can do somehting like this if you want to render a custom message with the error. Or similar with the error handler and lua too location / { access_by_lua_block { ngx.status = 500 ngx.say('hello from custom error code') return } } — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#1702 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF6OVIFIE7ZRI6QQQBKSGKTRP3DKRANCNFSM4MXNTIRQ .

@rest17 Hi, as you are the reporter, you can close this issue by your self.

rainingmaster avatar May 10 '20 09:05 rainingmaster

just set proper content type via nginx, for example default_type text/html; or in lua ngx.header.content_type = "text/html"

Just in case anyone else ends up here via Google: Chrome was showing ERR_INVALID_RESPONSE because the default Content-Type was application/octet-stream, this solved it.

rltas avatar Sep 15 '23 11:09 rltas