How to upgrade nginx to support HTTP/2 server push

WBOY
Release: 2023-05-13 21:25:13
forward
937 people have browsed it

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
Copy after login

2. Update nginx

yum update
Copy after login

3. Restart nginx

systemctl restart nginx
Copy after login

4. Verify nginx version

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

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; # 加上这行
}
Copy after login

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
Copy after login

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

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

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; );
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!