简体   繁体   中英

H2PushResource directive not working for http2 server push in Apache

I'm using Apache HTTP server with h2 enabled. H2PushResource is not working as expected. I want to test Server Push.

I've added the below extra config lines in httpd.conf file.

Protocols h2 http/1.1
H2PushResource on
<If "%{DOCUMENT_URI} == '/testhttp2.html'">
        H2PushResource add hello3.jpg
</If>

I'm not seeing PUSH for this resource in the Initiator field of chrome dev-tools.

The add Link header for preload is working fine. The initiator for preload is coming as "Other" (as expected).

Also, can someone explain HTTP2_SESSION_RECV_PUSH_PROMISE and HTTP2_SESSION_SEND_RST_STREAM to me? I can see it for the pushed resource in chrome://net-internals/

t=62561 [st=   4]    HTTP2_SESSION_RECV_PUSH_PROMISE
                     --> :scheme: https
                         :authority: newapache.com:442
                         :path: style1.css
                         :method: GET
                         cache-control: no-cache
                         user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
                         accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
                         accept-encoding: gzip, deflate, br
                         accept-language: en-US,en;q=0.9
                         host: newapache.com:442
                     --> id = 1
                     --> promised_stream_id = 4
t=62561 [st=   4]    HTTP2_SESSION_SEND_RST_STREAM
                     --> description = "Invalid pushed request headers."
                     --> error_code = "7 (REFUSED_STREAM)"
                     --> stream_id = 4

t=62562 [st=   5]    HTTP2_SESSION_RECV_RST_STREAM
                     --> error_code = "7 (REFUSED_STREAM)"
                     --> stream_id = 4

You are not using it right.

This command:

    H2PushResource add hello3.jpg

should have the full path relative to the base directory so should at least have a leading slash:

    H2PushResource add /hello3.jpg

The PUSH_PROMISE message is a message sent from the server to the client saying "I know you didn't ask for it, but I'm going to send you this resource now, let me know if that's a problem." It's structure is similar to a HTTP Request (a HEADER Frame in HTTP/2) except it comes from the server instead of the client - it's basically the fake request the server is responding to with the pushed resource.

The RST_STREAM message is used to reset a stream - which means to end an inflight download. In this case the client (the web browser) is saying "I don't want the pushed resource because I believe some of the headers are invalid so this is a bad request that I'm going to ignore anyway". The header that is invalid is the :path pseudo header which should be /hello3.jpg and not hello3.jpg .

Once you fix this, there's a few other things you should be aware of:

  1. The resource must be used by the page, otherwise it will be pushed but not show in the Network tab and will be a pointless, wasteful, push. Though it will show in chrome://net-internals.

  2. Chrome does not allow caches (including push cache) to be used for insecure connections (eg with a self-signed certificate that is not trusted). So unless you have a green padlock showing in Chrome, pushed resources will be ignored.

  3. Apache remembers which resources it has pushed on that connection so shouldn't push again if on the same connection. Use this command to turn this functionality off to make this easier to test:

     H2PushDiarySize 0 

Push can be quite complicated so check out my chapter on this in my upcoming book for more information: https://livebook.manning.com/#!/book/http2-in-action/chapter-5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM