Home  >  Article  >  Operation and Maintenance  >  How to upgrade nginx to support HTTP/2 server push

How to upgrade nginx to support HTTP/2 server push

WBOY
WBOYforward
2023-05-13 21:25:13954browse

Upgrade nginx to 1.14.0

1. Configure the official yum source of nginx. Create the configuration file /etc/yum.repos.d/nginx.repo and write the following content

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2. Update nginx

yum update

3. Restart nginx

systemctl restart nginx

4. Verify nginx version

$ curl -i 127.0.0.1
http/1.1 301 moved permanently
server: nginx/1.14.0

Modify nginx configuration

Add http2_push_preload on; to the original configuration. When nginx detects the link response header, it will actively push resources to the client.

location ~ \.php$ {
  # ...省略其他配置
  http2_push_preload on; # 加上这行
}

Modify the WordPress theme

nginx’s http2_push_preload requires the cooperation of application services. For example, if I want to actively push the file index.js, I need to add the following response header:

link: </index.js>; as=script; rel=preload

You can also push multiple files at the same time, such as:

link: </index.js>; as=script; rel=preload, ; as=style; rel=preload

Specific To wordpress, you can add the following code:

function add_http2_push_header() {
  $preload_resource_array = array(
    &#39;</index.js>; as=script; rel=preload&#39;,
    &#39;</index.css>; as=style; rel=preload&#39;
  );
  $preload_link_value = join( &#39;, &#39;, $preload_resource_array );

  header( &#39;link: &#39;.$preload_link_value ); 
}
add_action( &#39;send_headers&#39;, &#39;add_http2_push_header&#39; );

Browser verification

Before upgrading, server push is not supported.

How to upgrade nginx to support HTTP/2 server push

After the upgrade, server push is supported.

How to upgrade nginx to support HTTP/2 server push

The above is the detailed content of How to upgrade nginx to support HTTP/2 server push. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete