升級nginx以支持http2的方法
本文介紹了升級nginx以支持http2的方法,分享給大家,具體如下:
一鍵升級腳本
https://github.com/whisshe/upHttp2.git
HTTP/2 的優(yōu)勢
相比 HTTP/1.x,HTTP/2 在底層傳輸做了很大的改動和優(yōu)化:
- HTTP/2 采用二進(jìn)制格式傳輸數(shù)據(jù),而非 HTTP/1.x 的文本格式。二進(jìn)制格式在協(xié)議的解析和優(yōu)化擴(kuò)展上帶來更多的優(yōu)勢和可能。
- HTTP/2 對消息頭采用 HPACK 進(jìn)行壓縮傳輸,能夠節(jié)省消息頭占用的網(wǎng)絡(luò)的流量。而 HTTP/1.x 每次請求,都會攜帶大量冗余頭信息,浪費(fèi)了很多帶寬資源。頭壓縮能夠很好的解決該問題。
- 多路復(fù)用,直白的說就是所有的請求都是通過一個 TCP 連接并發(fā)完成。HTTP/1.x 雖然通過 pipeline 也能并發(fā)請求,但是多個請求之間的響應(yīng)會被阻塞的,所以 pipeline 至今也沒有被普及應(yīng)用,而 HTTP/2 做到了真正的并發(fā)請求。同時,流還支持優(yōu)先級和流量控制。
- Server Push:服務(wù)端能夠更快的把資源推送給客戶端。例如服務(wù)端可以主動把 JS 和 CSS 文件推送給客戶端,而不需要客戶端解析 HTML 再發(fā)送這些請求。當(dāng)客戶端需要的時候,它已經(jīng)在客戶端了。
升級HTTP2必要條件
- 1.10.0版本以上的nginx基于1.0.2以上版本的openssl編譯
- 必須支持https
軟件安裝位置
openssl
1、編譯位置
/usr/local/openssl
2、軟鏈接位置
/usr/bin/openssl
/usr/include/openssl
nginx
1、編譯及配置文件位置
/etc/nginx
升級所有游戲服務(wù)器的nginx和OpenSSL
升級openssl
1、將舊版本的openssl備份
mv /usr/bin/openssl /usr/bin/openssl.old mv /usr/include/openssl /usr/include/openssl.old
2、下載1.1版本的openssl,并解壓編譯
wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz tar zxvf openssl-1.1.0g.tar.gz cd openssl-1.1.0g/ ./config --prefix=/usr/local/openssl shared zlib make depend make && make install ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl ln -s /usr/local/openssl/include/openssl /usr/include/openssl(首選) cd /usr/local/openssl/lib ln -s libssl.so.1.1 libcrypto.so.1.1 /lib/x86_64-linux-gnu/ echo /usr/local/openssl/lib >> /etc/ld.so.conf
3、驗(yàn)證openssl版本信息
openssl version
if [[ `openssl version |awk '{print $2}'` == 1.1.0g ]];then
echo ok
fi
nginx升級(源碼編譯)
原配置文件不會丟失
1、下載源碼包
wget http://nginx.org/download/nginx-1.12.2.tar.gz
2、解壓編譯安裝
編譯安裝 nginx 時,--with-openssl 參數(shù)默認(rèn)只支持OpenSSL的源代碼,不支持已編譯好的 OpenSSL??梢栽趎ginx的解壓目錄下修改auto/lib/openssl/conf
sed "s/.openssl\///" c
將文件中的.openssl去掉,就可以支持編譯之后的openssl路徑了
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"
正式編譯
./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-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-openssl=/usr/local/openssl
修改nginx配置
listen 443 ssl http2;
出現(xiàn)的問題及解決
centos7下openssl version報(bào)錯openssl: error while loading shared libraries: libssl.so.1.1
echo /usr/local/openssl/lib >> /etc/ld.so.conf # 將openssl編譯路徑的lib庫加入到/etc/ld.so.conf ldconfig -v|grep libssl # 檢查是否添加成功
nginx重啟出現(xiàn)錯誤,nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed
ls -l /var/cache/nginx/client_temp#檢查該文件夾是否存在和文件夾的權(quán)限 mkdir -p /var/cache/nginx/client_temp #文件夾不存在則生成文件夾 chown -R www-data:www-data /var/cache/nginx/client_temp # 將文件夾所有者和組改為nginx的運(yùn)行用戶,具體見nginx.conf
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx實(shí)現(xiàn)動態(tài)封禁IP的步驟指南
在日常的生產(chǎn)環(huán)境中,網(wǎng)站可能會遭遇惡意請求、DDoS 攻擊或其他有害的訪問行為,為了應(yīng)對這些情況,動態(tài)封禁 IP 是一項(xiàng)十分重要的安全策略,本篇博客將介紹如何通過 NGINX 實(shí)現(xiàn)動態(tài)封禁 IP,從配置到自動化的實(shí)現(xiàn)步驟,需要的朋友可以參考下2025-02-02
nginx:?[emerg]?unknown?directive報(bào)錯誤的問題
本文主要介紹了nginx:?[emerg]?unknown?directive報(bào)錯誤的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
CentOS 7.0下nginx實(shí)現(xiàn)每天定時分割日志
大家都知道Nginx產(chǎn)生的日志都是存在一個文件,隨著網(wǎng)站運(yùn)行時間越長,日志文件的大小也在不斷增長,所以這個時候就需要實(shí)現(xiàn)定時分割,這篇文章主要介紹了在CentOS 7.0下nginx實(shí)現(xiàn)每天定時分割日志的相關(guān)資料,需要的朋友可以參考下。2017-04-04
Nginx實(shí)現(xiàn)負(fù)載均衡的配置步驟
Nginx是一個高性能的HTTP和反向代理服務(wù)器,它以其穩(wěn)定性、豐富的功能集、低系統(tǒng)資源消耗和簡單的配置而廣受歡迎,在大型網(wǎng)站和分布式系統(tǒng)中,Nginx常被用作負(fù)載均衡器,本文給大家介紹了Nginx負(fù)載均衡的配置步驟,需要的朋友可以參考下2024-06-06
詳解nginx實(shí)現(xiàn)ssl反向代理實(shí)戰(zhàn)
本篇文章主要介紹了nginx實(shí)現(xiàn)ssl反向代理實(shí)戰(zhàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01

