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

Receive duplicate response header `Connection: upgrade`

Open outsinre opened this issue 11 months ago • 2 comments

I want to simulate an HTTP 101 status code as follows.

# nginx.conf

worker_processes  1;
error_log logs/error.log debug;

events {
    worker_connections 1024;
}

http {
    lua_code_cache on;
    lua_package_path 'assets/?.lua;;';

    server {
        server_name www.example.com;
        listen 80;

        default_type text/html;

        access_log logs/access.log combined;

        location / {
            header_filter_by_lua_block {
                ngx.status = ngx.HTTP_SWITCHING_PROTOCOLS
                ngx.header["Connection"] = 'upgrade'
                ngx.header["Upgrade"] = 'foo/1, webscocket,bar/2'
                return ngx.exit(ngx.HTTP_OK)
            }
        }

    }
}

But got duplicate Connection: upgrade header:

root@73c270625dcb:/# curl -i localhost
HTTP/1.1 101 Switching Protocols
Server: openresty/1.25.3.2
Date: Fri, 07 Feb 2025 12:11:28 GMT
Content-Length: 0
Connection: upgrade
Connection: upgrade
Upgrade: foo/1, webscocket,bar/2

curl: (52) Empty reply from server

outsinre avatar Feb 07 '25 12:02 outsinre

This is because nginx will default to adding the Connection header https://github.com/nginx/nginx/blob/ecb809305e54ed15be9f620d56b19ff4e4be7db5/src/http/ngx_http_header_filter_module.c#L559 when sending headers, but openresty continues to add another Connection header in header output logic, So we do not need to add connection header again to avoid this unclear situation.

I am not sure if it is necessary to add additional restrictions in the openresty code to avoid this situation? ngx.header["Connection"] = 'upgrade' @zhuizhuhaomeng ?

oowl avatar Feb 08 '25 18:02 oowl

@oowl then this seems to be related to Nginx, not OpenResty. We can bypass the issue by not offering the Connection: upgrade part.

outsinre avatar Feb 10 '25 02:02 outsinre