Skip to content

ngx.var.upstream_* variables are not populated when using lua-resty-websocket #96

@qkuinn

Description

@qkuinn

🐛 Bug Report: ngx.var.upstream_* variables are not populated when using lua-resty-websocket

Module: [openresty/lua-resty-websocket](https://github.com/openresty/lua-resty-websocket)
Problem: When using lua-resty-websocket to initiate a WebSocket connection to an upstream server manually, all of the following variables are missing or always nil:

ngx.var.upstream_addr
ngx.var.upstream_response_time
ngx.var.upstream_connect_time
ngx.var.upstream_header_time
ngx.var.upstream_bytes_received

🔍 Expected Behavior

These upstream-related variables should be available (or have an alternative way to be collected) so that access logs and metrics are still meaningful, even when the upstream connection is established in Lua.

📋 Actual Behavior

All upstream-related variables return nil or are completely unset:

ngx.log(ngx.ERR, ngx.var.upstream_addr)              -- nil
ngx.log(ngx.ERR, ngx.var.upstream_response_time)     -- nil
ngx.log(ngx.ERR, ngx.var.upstream_connect_time)      -- nil
ngx.log(ngx.ERR, ngx.var.upstream_header_time)       -- nil
ngx.log(ngx.ERR, ngx.var.upstream_bytes_received)    -- nil

📎 Context

We are using lua-resty-websocket to implement a WebSocket reverse proxy behavior with dynamic upstream selection and load balancing (e.g., round-robin / ip-hash in Lua). But since the upstream connection is made via Lua (and not via proxy_pass), OpenResty doesn’t populate any of the $upstream_* variables in access logs, which makes it difficult to monitor or troubleshoot.

✅ Suggestion

Even if these variables cannot be automatically populated, perhaps the library could expose hooks or utility functions to manually record metrics into ngx.ctx, or better integration with OpenResty’s upstream tracking mechanism (if possible).

🧪 Steps to Reproduce

local wb = require("resty.websocket.client")
local client, err = wb:new()
local ok, err = client:connect("ws://127.0.0.1:9001/ws")
-- Send/receive some data...
ngx.log(ngx.ERR, ngx.var.upstream_addr) -- nil

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions