Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

njs and dynamic http2 server push #22

Closed
CarterLi opened this issue Jun 24, 2018 · 4 comments
Closed

njs and dynamic http2 server push #22

CarterLi opened this issue Jun 24, 2018 · 4 comments

Comments

@CarterLi
Copy link

I am trying using njs to add Link header to enable dynamic http2 server push

// This code is based on the official example in http://nginx.org/en/docs/http/ngx_http_js_module.html
if (r.headersIn.cookie) {
    s += "Cookies:\n";
    var cookies = parse(r.headersIn.cookie); // https://github.com/jshttp/cookie/blob/master/index.js#L49
    for (var c in cookies) {
        s += "  cookie '" + c + "' is '" + cookies[c] + "'\n";
        r.headersOut.link = '</api/' + cookies[c] + '/test>; rel=preload; as=fetch; crossorigin';
    }
}

Which results in

$ nghttp -vs 'https://test.eoitek.net/summary' -H'cookie: a=1'                                 日  6/24 12:07:18 2018
[ERROR] Could not connect to the address ::1
Trying next address 127.0.0.1
[  0.009] Connected
The negotiated protocol: h2
[  0.011] recv SETTINGS frame <length=18, flags=0x00, stream_id=0>
          (niv=3)
          [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):128]
          [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65536]
          [SETTINGS_MAX_FRAME_SIZE(0x05):16777215]
[  0.012] recv WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
          (window_size_increment=2147418112)
[  0.012] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
          (niv=2)
          [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
          [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[  0.012] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
          (niv=0)
[  0.012] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
          (dep_stream_id=0, weight=201, exclusive=0)
[  0.012] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
          (dep_stream_id=0, weight=101, exclusive=0)
[  0.012] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
          (dep_stream_id=0, weight=1, exclusive=0)
[  0.012] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
          (dep_stream_id=7, weight=1, exclusive=0)
[  0.012] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
          (dep_stream_id=3, weight=1, exclusive=0)
[  0.012] send HEADERS frame <length=50, flags=0x25, stream_id=13>
          ; END_STREAM | END_HEADERS | PRIORITY
          (padlen=0, dep_stream_id=11, weight=16, exclusive=0)
          ; Open new stream
          :method: GET
          :path: /summary
          :scheme: https
          :authority: test.eoitek.net
          accept: */*
          accept-encoding: gzip, deflate
          user-agent: nghttp2/1.32.0
          cookie: a=1
[  0.012] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
          (niv=0)
[  0.012] recv (stream_id=13) :status: 200
[  0.012] recv (stream_id=13) server: nginx/1.15.0
[  0.012] recv (stream_id=13) date: Sun, 24 Jun 2018 04:08:06 GMT
[  0.012] recv (stream_id=13) content-type: text/plain
[  0.012] recv (stream_id=13) content-length: 327
[  0.012] recv (stream_id=13) link: </api/1/test>; rel=preload; as=fetch; crossorigin
[  0.012] recv HEADERS frame <length=124, flags=0x04, stream_id=13>
          ; END_HEADERS
          (padlen=0)
          ; First response header
JS summary

Method: GET
HTTP version: 1.1
Host: test.eoitek.net
Remote Address: 127.0.0.1
URI: /summary
Headers:
  header 'host' is 'test.eoitek.net'
  header 'accept' is '*/*'
  header 'accept-encoding' is 'gzip, deflate'
  header 'user-agent' is 'nghttp2/1.32.0'
  header 'cookie' is 'a=1'
Cookies:
  cookie 'a' is '1'
Args:
[  0.012] recv DATA frame <length=327, flags=0x01, stream_id=13>
          ; END_STREAM
[  0.012] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
          (last_stream_id=0, error_code=NO_ERROR(0x00), opaque_data(0)=[])
***** Statistics *****

Request timing:
  responseEnd: the  time  when  last  byte of  response  was  received
               relative to connectEnd
 requestStart: the time  just before  first byte  of request  was sent
               relative  to connectEnd.   If  '*' is  shown, this  was
               pushed by server.
      process: responseEnd - requestStart
         code: HTTP status code
         size: number  of  bytes  received as  response  body  without
               inflation.
          URI: request URI

see http://www.w3.org/TR/resource-timing/#processing-model

sorted by 'complete'

id  responseEnd requestStart  process code size request path
 13      +855us       +238us    616us  200  327 /summary

The response header Link is added but no PUSH_PROMISE is given. I've set http2_push_preload on; and have tested add_header "Link ...", which works fine.

@xeioex xeioex added the bug label Jun 26, 2018
@xeioex xeioex self-assigned this Jun 26, 2018
@xeioex
Copy link
Contributor

xeioex commented Jun 26, 2018

The Link (a well as a couple of others) header should be treated in a special way inside nginx internal headers structure to work properly. Will be fixed soon.

@CarterLi
Copy link
Author

CarterLi commented Jun 26, 2018

@xeioex Could I push resources directly in njs without Link headers ( like http2_push url )? Sometimes browsers won't use pushed resources with Link headers for unknown reason

@xeioex
Copy link
Contributor

xeioex commented Jun 27, 2018

@CarterLi No, currently it is not possible to push resources directly.

@xeioex
Copy link
Contributor

xeioex commented Oct 12, 2021

FYI, HTTP/2 Push is dead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants