分享互联网知识,建站、IT话题杂谈

编译Nginx新版本增加HTTP/2支持

新版本的Nginx已经官方支持HTTP/2了,也就是以前PageSpeedMod的升级版。启用HTTP/2能有效进行减少网站连接数等优化,从而极大加快网站载入速度。下面将编译的过程记录下来,以供日后参考。

编译并升级Nginx

编译过程相对于以前的PageSpeed来说差不多,但是配置文件方面似乎少了很多(毕竟是官方支持而非第三方模块)。
如果以前添加有SPDY模块,记得在编译选项中去掉,Nginx配置文件中也去掉,根据报错信息来操作即可
# Install basic dependencies
# 安装依赖包
apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
# Update Nginx 升级Nginx
# check http://nginx.org/ for latest version 从http://nginx.org/获取最新版本
NGINX_VERSION=1.9.5
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar zxf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}/

# "Nginx -V" to check all the configure arguments, copy all the things behind "configure arguments:" here and add "–with-http_v2_module"
# "Nginx -V" 查看原有Nginx编译参数,将"configure arguments:"后面的复制到这里并添加 "–with-http_v2_module",因为包含有文件,日志等路径信息,不要简单复制粘贴
./configure –prefix=/etc/nginx –sbin-path=/usr/sbin/nginx –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock –http-client-body-temp-path=/var/cache/nginx/client_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/nginx/scgi_temp –user=nginx –group=nginx –with-http_ssl_module –with-http_realip_module –with-http_addition_module –with-http_sub_module –with-http_dav_module –with-http_flv_module –with-http_mp4_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_random_index_module –with-http_secure_link_module –with-http_stub_status_module –with-http_auth_request_module –with-mail –with-mail_ssl_module –with-file-aio –with-cc-opt='-g -O2 -fstack-protector –param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' –with-ld-opt='-Wl,-z,relro -Wl,–as-needed' –with-ipv6 –with-http_v2_module
#生成可执行文件
make

# Backup old nginx and replace it with new one, restart nginx 备份旧Nginx,替换成新的并重启。注意检查你自己的Nginx路径并修改
service nginx stop; mv /usr/sbin/nginx /usr/sbin/nginx.old; cp ~/nginx-${NGINX_VERSION}/objs/nginx /usr/sbin/; service nginx restart
[/raw]

编译Nginx过程中可能遇到的问题

  • ./configure: error: the HTTP rewrite module requires the PCRE library.
    原因及解决办法: 缺少PCRE库,用下列语句安装库后解决问题
    apt-get install libpcre3 libpcre3-dev[/raw]
  • ./configure: error: SSL modules require the OpenSSL library.
    原因及解决办法: 缺少SSL库,用下列语句安装库后解决问题
    apt-get install openssl libssl-dev[/raw]

修改Nginx文件启用HTTP/2

修改Nginx配置文件,在监听443端口后增加http2字样
原来的配置文件:
# Old config line
listen 443 ssl;[/raw] 增加http2后变成这样:
# Modified config line, adding "http2"
listen 443 ssl http2;[/raw]

重启Nginx
service nginx restart
[/raw]

打开chrome浏览器输入此地址检查启用是否成功: chrome://net-internals/#http2

参考资料

Leave a comment

Your email address will not be published. Required fields are marked *

7 thoughts on “编译Nginx新版本增加HTTP/2支持”