编译安装Nginx开启http/2教程

By | 2016年12月19日

虽然绝大多数网站仍然在使用 http/1.1,但 http/2 所带来的高性能和高安全性无疑会吸引越来越多的网站加入。简单的说,http/2 强制要求 https 连接并且能够防止线程阻塞,因而能够防止网络窃听并且缩短加载时间。不过使用一键包的朋友完全没必要自行编译,目前大多数 lnmp 一键包已经对 http/2 提供了良好的支持,通常只需更改虚拟主机配置文件即可。不过 reizhi 在这里只需用到 Nginx 作为反代服务器,并不需要安装其他组件,于是只好自行编译。在此写下编译安装 Nginx 并开启 http/2 的教程,以备不时之需。

以下教程基于 Debian 8,并通过实践验证。

在开始编译之前,需要确保已经安装好编译工具:


apt-get install build-essential libtool gcc g++

随后编译并安装 pcre,用于支持 rewrite

cd /usr/local/src

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz

tar -zxvf pcre-8.39.tar.gz

cd pcre-8.39

./configure

make && make install

完成后编译并安装 zlib,用于支持 gzip

cd /usr/local/src

wget http://zlib.net/zlib-1.2.8.tar.gz

tar -zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure

make && make install

然后我们下载 openssl 源码为编译 Nginx 做准备,打开这个链接后点击当前最新的版本 “OpenSSL_1_1_0c” 下面的 hash 值,在页面中复制完整的 hash ,稍后需要用到。

cd /usr/local/src

git clone https://github.com/openssl/openssl.git

cd openssl

git checkout 91eaf079b7430cb4ebb7f3ccabe74aa383b27c4e

在最后一步中,填入你所复制的 hash。这里我们不需要编译 openssl ,随后在编译 Nginx 时会顺带处理。

cd /usr/local/src

wget http://nginx.org/download/nginx-1.11.7.tar.gz

tar -zxvf nginx-1.11.7.tar.gz


cd nginx-1.11.7

./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 --with-openssl=/usr/local/src/openssl --user=www-data --group=www-data --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-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6

make && make install

mkdir /var/cache/nginx

如果一切顺利,编译完成后执行 nginx -V 可以看到以下信息:

此时 Nginx 就编译安装完成了,不过这样安装的 Nginx 是无法开机自动启动的,我们在 /etc/init.d 中添加一个服务项,将这个文件扔进去即可,记得授予执行权限。

除此之外,还需要在 /etc/rc.local 的 exit 0 之前添加一行 “service nginx start” ,这样 Nginx 就可以开机自启了。初次之外还可以使用 “/etc/init.d/nginx start” 或是 “/etc/init.d/nginx stop” 来手动启动或者停止。

接下来我们定位到目录 “/etc/nginx” ,在 nginx.conf 文件的 http 区段添加 “include /etc/nginx/sites-available/*;” 。保存后在 /etc/nginx 目录新建 “sites-available” 并添加虚拟主机配置文件即可。

最后一步,只需对 server 段略作修改即可开启 http/2 。在默认的 “listen 443 ssl;” 后添加 http2 即可,即:”listen 443 ssl http2″。在保存重启 Nginx 后,你的网站便成功开启了 http/2 。如果还需要 php-fpm 或是 数据库的话,自行编译即可。

One thought on “编译安装Nginx开启http/2教程

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

滑动到最右以进行验证 *