升级NGINX支持HTTP/2服务端推送
2023-09-11 14:14:35 时间
内容概览
NGINX从1.13.9版本开始支持HTTP/2服务端推送,上周找时间升级了下NGINX,在博客上试验新的特性。
升级工作主要包括:
- 升级NGINX
- 修改NGINX配置
- 修改wordpress主题
升级NGINX到1.14.0
1、配置nginx官方的yum源。创建配置文件/etc/yum.repos.d/nginx.repo
,写入如下内容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2、更新nginx
yum update nginx
3、重启nginx
systemctl restart nginx
4、验证nginx版本
$ curl -I 127.0.0.1
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
修改NGINX配置
在原有的配置上,加上http2_push_preload on;
。当nginx检测到link
响应首部时,会主动往客户端推送资源。
location ~ \.php$ {
# ...省略其他配置
http2_push_preload on; # 加上这行
}
修改WordPress主题
NGINX的http2_push_preload
需要应用服务的配合。比如我要主动推送index.js
这个文件,那么需要加上如下响应首部:
link: </index.js>; as=script; rel=preload
也可以同时推送多个文件,比如:
link: </index.js>; as=script; rel=preload, </index.css>; as=style; rel=preload
具体到WordPress,可以加上如下代码:
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' );
浏览器验证
升级之前,不支持服务端推送。
升级之后,支持服务端推送。
相关链接
https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/
http://nginx.org/en/docs/http/ngx_http_v2_module.html#http2_push_preload
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
相关文章
- windows下使用Nginx
- Nginx负载均衡
- Nginx -HTTP和反向代理服务器简单配置
- 在 Ubuntu 16.04 为 Nginx 服务器安装 LEMP 环境(MariaDB,PHP 7 并支持 HTTP 2.0)
- Nginx: ngx_http_geoip_module
- nginx location中uri的截取的实现方法
- NGINX限制对HTTP资源的访问
- ERROR [] - method [PUT], host [http://localhost:9200], URI [/_template/alarm_record], status line [HTTP/1.1 400 Bad Request]
- Nginx搭建负载均衡集群
- 《精通Nginx》——2.4 Http的server部分
- Nginx解读内置非默认模块 ngx_http_stub_status_module
- Nginx安装lua-nginx-module模块
- Linux-019-Centos Shell 安装 Nginx 后启动时提示找不到Lua模块的libluajit-5.1.so.2文件,具体提示信息:./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
- Nginx配置同一个域名http与https两种方式都可访问
- nginx做正向代理(Centos7,支持http和https)
- Nginx + PHP (FastCGI)搭建胜过Apache十倍的Web服务器
- Nginx_HTTP 499 状态码 nginx下 499错误
- nginx配置https
- Nginx配置http请求重定向到https、Nginx防止被域名恶意解析的配置-Nginx 的 default_server 指令
- 浅析Nginx常用配置:如何根据user-agent适配PC/移动设备、如何配置https、如何开启静态服务、如何设置图片防盗链、如何配置请求过滤、如何配置静态文件的缓存时间、如何配置http转发到https、如何配置泛域名路径分离及泛域名转发
- windows下流媒体nginx-rmtp-module服务器搭建及java程序调用fmpeg将rtsp转rtmp直播流【转】
- 使用NGINX+LUA实现WAF功能 和nginx 防盗链
- 中间件:Nginx总结
- Nginx虚拟主机别名的配置
- nginx学习笔记(7)Nginx如何处理一个请求---转载
- Nginx压测和并发预估
- Nginx没有启动文件、nginx服务不支持chkconfig、nginx无法自启