Server push : how to make it work
nginx/1.18.0 (Ubuntu)
I want to use Server push technology.
nginx.conf
location = /push/ {
http2_push https://slabaya.ru/1/css/style.css;
}
html
<html>
<head>
<link rel="stylesheet" href="https://slabaya.ru/1/css/style.css">
</head>
<body>
<p>Test</p>
</body>
</html>
Result: not working.
https://c.radikal.ru/c21/2105/84/50598f04e294.png
https://d.radikal.ru/d32/2105/ba/7e5d2cf6e02a.png
As you can see from the images, neither Chrome nor Webpagetest signify that the css file is pushed.
Could you help me solve the problem?
Solution 1:
Your location
block only applies to the URL http://slabaya.ry/push/
URL.
However, HTTP/2 push is not easy to implement properly, and that's why it is going to be removed from Chromium (base codebase for Chrome and many other browsers).
The problem with HTTP/2 push is that sometimes the client already has the resource from previous page load, and you need to implement the push conditionally. This is not easy to do.
If I were you, I wouldn't bother trying to implement push. Too much work for little or no gain.
If you still want to fix the configuration, you need to move the http2_push
directive outside the location
block.
Solution 2:
You can only push from the domain the request is served from.
In your example the document is served from one domain and the stylesheet from another.
Therefore the document cannot “push” the stylesheet since it does not own it. It would need to fetch it and then push it, but even if that were possible (which it’s not you can only push relative path sources), the domain would change and so would not be the same as the one referenced in the document HTML so it would have to request that anyway.
Saying that I would agree with everything Tero said about push being difficult, for questionable gains, and it’s been stated it will be removed from Chrome.