最近在優化網站的訪問速度,爲網站開啓http2協議,這個協議有什麼優勢呢?以下:html
#進入/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_INCS="$CORE_INCS $OPENSSL/.openssl/include" CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h" CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a" CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a" #修改爲以下,保存退出 CORE_INCS="$CORE_INCS $OPENSSL/include" CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h" CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a" CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a" #先安裝一些依賴(我是升級,其實不須要安裝依賴了,若是是首次安裝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 #可是我是安裝過了,因此須要備份老的tengine 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/ #而後重啓Tengine,就算編譯安裝完成啦
#配置http2很簡單,以下: server { #http 不支持http2的傳輸協議,因此80端口不變 listen 80 # listen在原https配置文件基礎上添加http2 listen 443 ssl http2; server_name www.qingye.info; ..... } #另外附上一份完整的支持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和升級步驟有所區別,須要注意下nginx
2.http不支持http2的傳輸協議,因此80端口仍是使用http1.1的協議,https使用http2的傳輸協議c++