01.13 让nginx支持http2

版本支持

目前http2只支持https的网站,openssl版本必须在1.0.2e以上的版本,nginx版本必须在1.9.5版本以上,而且需要编译参数支持:with-http_v2_module、with-http_ssl_module。


让nginx支持http2


openssl

查看openssl版本

<code>openssl  version/<code>

如果系统默认的openssl版本不够支持,可以直接下载源码,不需要安装,只要在nginx编译的时候,指定到对应的目录。

下载源码

<code>wget -c https://www.openssl.org/source/openssl-1.1.1d.tar.gz/<code>

下载PCRE

正则支持,也只是需要源码即可,不需要安装二进制。

<code>wget -c https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz/<code>


让nginx支持http2


下载Zlib

压缩支持,也只是需要源码即可,不需要安装二进制。

<code>wget -c http://zlib.net/zlib-1.2.11.tar.gz/<code>

下载nginx

<code>wget  -c http://nginx.org/download/nginx-1.16.1.tar.gz/<code>

解压

<code>tar zxvf nginx-1.16.1.tar.gz/<code>
<code>tar zxvf zlib-1.2.11.tar.gz/<code>
<code>tar zxvf pcre-8.43.tar.gz/<code>
<code>tar zxvf openssl-1.1.1d.tar.gz/<code>

编译安装

<code>cd nginx-1.16.1/<code>
<code>./configure --prefix=/opt/nginx --with-stream --with-mail --with-http_geoip_module --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl=../openssl-1.1.1d --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.43 --with-http_v2_module/<code>
<code>make/<code>
<code>make install/<code>

配置Server

直接在listen配置上加上http2的关键字即可开启。

<code>server {/<code>
<code>    server_name opcai.top;/<code>
<code>    listen 443 ssl http2;/<code>
<code>    #证书(公钥.发送到客户端的)/<code>
<code>    ssl_certificate /opt/nginx/keys/server.crt;/<code>
<code>    #私钥,/<code>
<code>    ssl_certificate_key /opt/nginx/keys/server.key;/<code>
<code>    access_log logs/opcai.log;/<code>
<code>    location /static/ {/<code>
<code>      expires 1d;/<code>
<code>      alias /data/web/static/;/<code>
<code>    }/<code>
<code>    add_header Access-Control-Allow-Headers $http_access_control_request_headers;/<code>
<code>    location / {/<code>
<code>      proxy_set_header Host $http_host;/<code>
<code>      proxy_set_header X-Real-IP $remote_addr;/<code>
<code>      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;/<code>
<code>      proxy_set_header X-Forwarded-Proto $scheme;/<code>
<code>      proxy_pass http://192.168.1.100:8081;/<code>
<code>    }/<code>
<code>  }/<code>


让nginx支持http2


检查并启动服务

<code>/opt/nginx/sbin/nginx -t /<code>
<code>/opt/nginx/sbin/nginx/<code>


分享到:


相關文章: