openresty / openresty/lua-resty-websocket

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

Open
#96 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
523
Forks
112
Avg merge
1d 16m
Merged PRs (30d)
2

Description

🐛 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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the resty.websocket.client entry point and the client:connect call shown in the reproduction, then inspect how Lua-created upstream connections relate to the listed ngx.var.upstream_* values. Reproduce the nil values for ws://127.0.0.1:9001/ws; completion is not concretely defined because the issue offers hooks, ngx.ctx metrics, or upstream tracking as alternatives.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
backend, networking
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.