启用HTTP2服务器推送Nginx

时间:2019-03-20 11:38:11

标签: nginx http2 nghttp2

我安装了具有HTTP2支持的Nginx。我的nginx在应用程序负载平衡器后面,并且SSL终止仅在LB处发生。

我首先启用了HTTP2_PUSH,如下所示:

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

但是没有用。我的浏览器调试器的“网络”选项卡显示启动器为“索引”,并且nghttp也未显示任何内容。

我尝试过的另一种方法是:

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";
    }

下一种方法在网络标签中将启动器从index更改为other,但是nghttp工具仍然确认没有服务器推送。

1 个答案:

答案 0 :(得分:0)

我的理解是AWS Application Load Balancer是7级(HTTP)负载均衡器,仅在前端支持HTTP / 2,而在Nginx后端不支持。

您可以通过将$server_protocol添加到您的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;

我想流量以HTTP / 1.1的形式进入。

即使AWS ALB现在确实支持HTTP / 2到后端,也不意味着它将支持HTTP / 2 Push。当涉及到这样的多个基础结构时(如果ALB和Nginx都支持推送但客户端不支持),这会变得复杂,最好的建议是从边缘节点进行推送,尤其是如果它通过HTTP preload标头指令支持。

使用4级(TCP)负载平衡器使它正常工作可能会更好。