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( '</index.js>; as=script; rel=preload', '</index.css>; as=style; rel=preload' ); $preload_link_value = join( ', ', $preload_resource_array ); header( 'link: '.$preload_link_value ); } add_action( 'send_headers', 'add_http2_push_header' );
Browser verification
Before upgrading, server push is not supported.
After the upgrade, server push is supported.
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!