简体   繁体   中英

Enable HTTP2 Server Push Nginx

I installed nginx with HTTP2 support. My nginx is behind application load balancer and SSL termination is happening at LB only.

I first enabled HTTP2_PUSH like below:

http2_push_preload on;
location /mx {
       http2_push https://example.com/css/style.css;
       http2_push https://example.com/js/main.js
}

But it did not work. My browser debugger network tab was showing initiator is "index", and nghttp also did not show anything.

Another approach I tried is:

http2_push_preload on;
    location /mx {
           add_header Link "<https://example.com/css/style.css>; rel=preload; as=style,<http2_push https://example.com/js/main.js>; rel=preload; as=script";
    }

Next approach changed initiator from index to other in network tab, but nghttp tool still confirms that no server push is happening.

My understanding is the AWS Application Load Balancer is a level 7 (HTTP) load balancer and only supports HTTP/2 on the front end and not to your Nginx back end.

You can check this by adding $server_protocol to your Nginx log_format :

log_format my_log_format '$remote_addr - $remote_user [$time_local] $server_protocol "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
access_log /usr/local/nginx/nginx-access.log my_log_format;

I'd guess the traffic is coming in as HTTP/1.1.

Even if AWS ALB does support HTTP/2 to the backend now, that doesn't mean it will support HTTP/2 Push. This gets complicated when multiple pieces of infrastructure is involved like this (what if the ALB and Nginx both support push but client doesn't) and best advice is to push from the edge node, especially if it supports that via HTTP preload header instructions.

You are probably better using a level 4 (TCP) load balancer to get this to work.

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