最近在优化网站的访问速度,为网站开启http2协议,这个协议有什么优点呢?如下:
#进入/usr/local/src,一般软件包都放这里
cd /usr/local/src
#下载安装包
wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz
tar -zxvf openssl-1.0.2t.tar.gz
cd openssl-1.0.2t
./config shared zlib
#默认安装找/usr/local/ssl
make && make install
#先备份之前的老版本
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old
#建立软连接
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/ssl/include/openssl /usr/include/openssl
#把动态库加入系统配置路径
echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
#查看动态库是否生效
ldconfig -p
#检查openssl版本
openssl version
cd /usr/local/src
wget http://tengine.taobao.org/download/tengine-2.2.2.tar.gz
tar tengine-2.2.2.tar.gz
cd tengine-2.2.2
#这里需要修改一下tengine的代码,因为我们是手动编译的openssl,依赖库路径和原来系统安装的不太一样,所以需要手动指定
vim auto/lib/openssl/conf
#在大概32行配置原来配置如下:
CORE_INC
CORE_DEP
CORE_LIB
CORE_LIB
#修改成如下,保存退出
CORE_INC
CORE_DEP
CORE_LIB
CORE_LIB
#先安装一些依赖(我是升级,其实不需要安装依赖了,如果是首次安装tenginx,就需要安装依赖)
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake jemalloc jemalloc-devel
#开始编译tenginx
cd /usr/local/src/tengine-2.2.2 && ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_concat_module --with-jemalloc --with-http_v2_module --with-http_secure_link_module --with-openssl=/usr/local/ssl
make
##注意,如果是第一安装tegninx,只需要只需执行以下命令
make install
#但是我是安装过了,所以需要备份老的tenginx
cp -af /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak
cp -af /usr/local/nginx/sbin/dso_tool /usr/local/nginx/sbin/dso_tool_bak
#拷贝编译好的tenginx到对应目录
cp /usr/local/src/tengine-2.2.2/objs/nginx /usr/local/nginx/sbin/
cp /usr/local/src/tengine-2.2.2/objs/dso_tool /usr/local/nginx/sbin/
#然后重启Tenginx,就算编译安装完成啦
#配置http2很简单,如下:
server {
#http 不支持http2的传输协议,所以80端口不变
listen 80
# listen在原https配置文件基础上添加http2
listen 443 ssl http2;
server_name www.oneq.work;
.....
}
#另外附上一份完整的支持http2的Tenginx配置
upstream server_backend {
server ip:80 weight=10;
server ip:80 weight=10;
keepalive 800;
#下面检测端口的配置需要Tenginx的才有效,不是Tenginx需要安装额外的插件或者直接注释即可
check interval=5000 rise=3 fall=3 timeout=5000 type=tcp;
}
server {
listen 80;
listen 443 ssl http2;
server_name xxx.xxx.xxx;
req_status server;
ssl_certificate /usr/local/nginx/certs/xxx.xxx.xxx.crt;
ssl_certificate_key /usr/local/nginx/certs/xxx.xxx.xxx.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header User-Agent;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
proxy_http_version 1.1;
access_log logs/access.log main;
location / {
proxy_pass http://server_backend/;
access_log logs/server_backend.log main;
}
error_page 404 /404.html;
location = /404.html {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
1.第一次安装tenginx和升级步骤有所区别,需要注意下
2.http不支持http2的传输协议,所以80端口还是使用http1.1的协议,https使用http2的传输协议
作者博客 青叶的博客
审核编辑 黄昊宇
全部0条评论
快来发表一下你的评论吧 !