Work-around for co-socket api’s not being available in log_by_lua giving 404 error

I’ve been banging my head on this problem for a few days now, and have gotten a bit stuck, so I thought I’d ask here. Would really appreciate any help.

Context:

The issue that I’m currently facing with my site is that we are getting a lot of nginx 499 errors being raised when clients disconnect before hitting a refresh_token endpoint. (The purpose of this endpoint is to generate a new access token and refresh token from the provided refresh token in the request, and return it to the client). We also block the refresh_token being used to make this request. The problem is, that when the client disconnects, their local storage does not get updated with the new token, and so, the next time they reconnect they are forcibly logged out, as their local storage token is treated as blocked when it hits the refresh_token endpoint the following time.

High level solution:

What I wanted to do to approach this problem was to essentially catch the 499 error raised by nginx when it occurs, and hit another endpoint on my backend to cache this token. On subsequent hits for this user to the refresh_token endpoint, it can also check if the token exists in the cache, and generate a new one in those cases.

The Problem:

So, from what I’ve found, the phase in which I can catch the nginx response status at the end of the request is log_by_lua (specifically in my case, log_by_lua_file). The problem is that log_by_lua_file does not have access to cosocket apis, as per these docs, and therefore I am unable to make request to new endpoints with lua scripts here. Now the workaround that the docs recommend is to use ngx.timer.at (Context for the workaround is in the same place as the cosocket apis information docs I linked to previously). I have implemented this workaround using resty-http, however, am currenlty getting 404 errors on the endpoint. Is there something I am missing? To be sure that I am using the right endpoint, I also made a curl request from the nginx pod directly to the endpoint, and it worked. (I also logged the url to ensure that it is resolving to the correct url, since I know that ngx.timer doesn’t have access to ngx.var, so I want to make sure that data.authenticator field had been passed correctly).

My script that I am trying to use with log_by_lua_file is below: 
-- Function to send the original request data to the internal endpoint on 499 error
local function send_499_notification(premature, data)
    local http = require "resty.http"
    local cjson = require "cjson"

    if premature then
        ngx.log(ngx.WARN, "Premature timer trigger")
        return
    end
    ngx.log(ngx.WARN, "Request url: ", "http://" .. data.authenticator .. "/handle_499_error/")
    ngx.log(ngx.WARN, "Request method: ", data.method)
    ngx.log(ngx.WARN, "Request body: ", data.body)
    ngx.log(ngx.WARN, "Request headers: ", cjson.encode(data.headers))
    ngx.log(ngx.WARN, "Request args: ", cjson.encode(data.args))

    local httpc = http.new()
    local res, err = httpc:request_uri("http://" .. data.authenticator .. "/handle_499_error/", {
        method = data.method,
        body = data.body,
        query = data.args,
        headers = data.headers,
    })

    if not res then
        ngx.log(ngx.WARN, "Failed to notify internal endpoint: ", err)
    else
        ngx.log(ngx.WARN, "Successfully notified internal endpoint for 499 error, res status ", res.status)
    end
end

-- Function to handle 499 error
local function handle_499()
    local req_body = ngx.ctx.req_body or ""
    local req_headers = ngx.ctx.req_headers or {}
    local req_args = ngx.ctx.req_args or {}
    local req_method = ngx.ctx.req_method or ""

    local data = {
        authenticator = ngx.var.authenticator,
        method = req_method,
        body = req_body,
        args = req_args,
        headers = req_headers,
    }

    local ok, err = ngx.timer.at(0, send_499_notification, data)
    if not ok then
        ngx.log(ngx.ERR, "Failed to create timer: ", err)
    end
end

-- Retrieve NGINX status code
local nginx_status = ngx.status

if nginx_status == 499 then
    handle_499()
end

ngx.log(ngx.WARN, "NGINX status code: ", nginx_status)

While I cannot share the whole log sequence, I do see the final log, with:

2024/07/22 11:15:59 [warn] 7#0: *13017 [lua] handle_499.lua:27: Successfully notified internal endpoint for 499 error, res status 404, context: ngx.timer, client: 10.76.170.103, server: 0.0.0.0:80

Also, just for reference, the ngx.ctx fields were created in the access_by_log phase, I am aware that access request methods and fields are not available in log_by_lua, which was why I saved them in ngx.ctx earlier.

New contributor

Arman Jasuja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật