為網(wǎng)站添加SSL證書并配置HTTP2(二)

個人博客:https://xiaofengsir.com/,關(guān)注更新文章

一. 為網(wǎng)站配置http2.0協(xié)議

說明:為網(wǎng)站添加SSL證書并配置HTTP2(一)中,已經(jīng)安裝好了SSL證書,并且配置了301重定向?,F(xiàn)在可以開始配置http2.0協(xié)議了,關(guān)于什么是http2.0協(xié)議,以及和http1.1的區(qū)別這里不多說了,自行百度吧。

1. 查看網(wǎng)站協(xié)議

打開瀏覽器,將審查元素打開,點擊Network選項卡,將Protocol調(diào)出來,查看傳輸協(xié)議。目前顯示的為http/1.1,傳輸協(xié)議。


2. 查看Nginx版本,查看OpenSSL版本

目前顯示的版本是nginx/1.10.2OpenSSL 1.0.1e-fips,因為之前安裝Nginx時,使用的時系統(tǒng)默認的源,然而Centos7自帶的Nginx版本并不夠新,想要支持使用http2.0協(xié)議,OpenSS必須升級到2.0版本以上才可以。因此要對Nginx重新編譯。
<br />


3. 編譯安裝Nginx和openssl

說明:編譯Nginx步驟較多,如果出現(xiàn)問題,評論區(qū)留言吧。

1.安裝編譯過程中需要使用的工具

 yum install wget curl perl gcc pcre-devel zlib-devel make -y

代碼說明:
wget: 常用的命令可以自動下載文件的工具,支持通過HTTP、HTTPS、FTP等常見的TCP/IP協(xié)議下載,并可以使用HTTP代理。
curl: 利用URL語法在命令行方式下工作的開源文件傳輸工具。和wget類似但有不同。
perl: 為服務(wù)器安裝perl環(huán)境。
gcc: 為服務(wù)器安裝gcc環(huán)境。
pcre-devel: perl的一個庫,用來處理正則表達式。
zlib-devel: 為服務(wù)器安裝zlib庫。
make: linux中常用的編譯安裝命令。


2.下載Nginx源和openssl源

//下載openssl新版的源和Nginx新版源
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz http://nginx.org/download/nginx-1.11.10.tar.gz
//解壓兩個壓縮包
tar zxvf nginx-1.11.10.tar.gz
tar zxvf openssl-1.0.2l.tar.gz
//重命名nginx和openssl
mv nginx-1.11.10/ nginx
mv openssl-1.0.2l/ openssl

3.編譯nginx

1)首先卸載原先版本的額nginx

//卸載已安裝的nginx版本
yum remove nginx -y

2)配置編譯nginx

//進入nginx和openssl所在目錄。
cd ~
//將nginx和openssl移動到/usr/local/src文件夾
mv nginx/ openssl/ /usr/local/src/
//進入/etc/local/src/nginx目錄下
cd /usr/local/src/nginx
//配置nginx
./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-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-stream_realip_module --with-openssl=/usr/local/src/openssl
//使用make命令編譯一下,時間較長
make
//編譯完成后執(zhí)行下make insatll
make install
//為新編譯的nginx添加用戶,創(chuàng)建配置目錄
useradd nginx && mkdir /etc/nginx/conf.d
//創(chuàng)建nginx緩存目錄,并設(shè)置一些權(quán)限
mkdir /var/cache/nginx && chown nginx:root /var/cache/nginx
//刪除用不著的文件
rm -rf /usr/local/src/nginx && rm -rf /usr/local/src/openssl && rm -rf /var/cache/yum

3)繼續(xù)配置nginx,并啟動它

//配置nginx服務(wù)文件
vim /usr/lib/systemd/system/nginx.service

//為nginx.service添加配置代碼
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target

//啟動下nginx,再設(shè)置開機啟動
systemctl start nginx
systemctl enable nginx
systemctl status nginx

4)編輯nginx.conf文件

//進入nginx目錄
cd /etc/nginx/
//為nginx.conf添加代碼
vim nginx.conf
//在nginx.conf中的gzip on代碼下面一行添加如下代碼
include /etc/nginx/conf.d/*.conf;
//重啟nginx
systemctl restart nginx

4.配置http2.0協(xié)議

//進入網(wǎng)站的nginx配置文件所在目錄
cd /etc/nginx/
//編輯ssl.域名.conf文件
vim ssl.域名.conf
//在第一行l(wèi)isten配置項中添加 ssl http2
server {
    listen 443 ssl http2;
    .....見第一篇中的.conf配置文件
//重啟nginx
systemctl restart nginx

5. 測試

至此,http2.0協(xié)議配置完成,打開瀏覽器查看傳輸協(xié)議。

可以看到,傳輸協(xié)議為h2,配置成功。


總結(jié):配置過程比較繁瑣,測試過程中可能會出現(xiàn)一些問題,可以在評論區(qū)留言。總的來說,主要就是涉及nginx本身以及nginx的配置文件的編譯,編輯等等...留心一點安裝兩篇課程一步一步來,時可以完成全部安裝過程的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容