lnmp1.3中配置Nginx启用HTTP/2.0 + ALPN

作者:matrix 被围观: 4,589 次 发布时间:2017-04-17 分类:零零星星 | 无评论 »

这是一个创建于 2528 天前的主题,其中的信息可能已经有所发展或是发生改变。

http2.0早就开始实行了,忽然间才看到其实很多网站都有使用了http2.0协议,aliyun.com都有了,其他巨头是在打瞌睡吗?
图片3654-lnmp1.3中配置Nginx启用http2.0
图中显示的Request完全和http1.1的请求完全不同 这,就是高科技!

要求

若想使用http2.0,浏览器和服务器端也都有要求。浏览器用最新版Chrome或其他,服务器端网站配置就麻烦多了。
服务器端OpenSSL库的版本要支持ALPN(1.0.2+ 目前最新为1.1.0e),之前是用SPDY,NPN,后来google只支持ALPN,也就是说未来就是HTTP/2 + ALPN
为什么我们应该尽快支持 ALPN?
需要给网站域名配置证书
nginx版本需要1.9.5+(目前最新版本1.12.0)
若你的服务器openssl或nginx本来就达不到要求,建议都重新安装升级才对。之前只是把openssl升级到最新版本,且Lnmp1.3中的nginx是1.10的版本完全符合要求(其中也有必须的httpv2和ssl模块)就没有给nginx做升级操作,以为可以用http2.0 结果给vhost的conf文件添加了listen 443 ssl http2;重启nginx N次都没有任何反应,最后还是更新nginx才解决。就是因为之前安装nginx的时候openssl没有达到版本要求,就算升级了服务器openssl也没有卵用。

> openssl version  #查看openssl 版本
> nginx -V #查看nginx版本以及模块

服务器配置

环境 ubuntu 14 64bit  
LNMP 1.3 NGINX 1.10

升级openSSL库

>openssl version -a #查看openssl 信息 用于升级
>cd #进入默认目录
>wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz
>tar -zxvf ./openssl-1.1.0e.tar.gz
>cd openssl-1.1.0e/
>./config --prefix=/usr/lib/ssl --openssldir=/usr/lib/ssl 
#注意--prefix=/usr/lib/ssl为之前openSSL的安装目录,--openssldir表示安装目录 默认值就为/usr/lib/ssl
>make && make install
>mv /usr/bin/openssl /usr/bin/openssl-old
>mv /usr/include/openssl /usr/include/openssl-old
>ln -s /usr/lib/ssl/bin/openssl /usr/bin/openssl
>ln -s  /usr/lib/ssl/include/openssl /usr/include/openssl  
>echo "/usr/lib/ssl/lib">>/etc/ld.so.conf
>ldconfig 
#现在看看版本号就是最新的了

参考:https://www.douban.com/note/563948878/

若升级openssl导致ss服务无法使用参考:

更新OpenSSL库至最新版本导致sss服务无法启动

配置证书

不详细说明,之前有方法安装Let’s Encrypt证书

安装&升级nginx

最新版本1.12.0
本来是使用Lnmp安装目录中自带的upgrade.sh进行自动升级操作,后来发现有问题,升级时出现openssl library not found,不过就算是自动升级也不敢保证LNMP编译nginx的时候是否使用的最新版本openssl库,还是手动升级操作。

>wget -O nginx-ct.zip -c https://github.com/grahamedgecombe/nginx-ct/archive/v1.3.2.zip
>unzip nginx-ct.zip
>cd lnmp nginx-1.12.0
>./configure --add-module=../ngx_brotli --add-module=../nginx-ct-1.3.2 --with-openssl=../openssl-1.1.0e --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
#这里http_v2_module,http_ssl_module,with-openssl才是必须要配置的,其他module看个人实际安装情况
>cd /usr/local/nginx/sbin  
>cp nginx nginx.old  #备份旧版nginx
>cd ~/nginx-1.12.0
>make upgrade #升级操作

修改vhost中域名配置文件

文件位置:/usr/local/nginx/conf/vhost
修改对应域名的配置文件,在server段中添加listen 443 ssl http2;就可以了
参考 hhtjim.com:

server
    {
        listen 80;
    listen 443 ssl http2;
        #listen [::]:80;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/hhtjim.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/hhtjim.com/privkey.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; #加密算法(CloudFlare 推荐的加密套件组) 
    ssl_prefer_server_ciphers on; #优化 SSL 加密套件 
    ssl_session_timeout 10m; #客户端会话缓存时间 
    ssl_session_cache builtin:1000 shared:SSL:10m; #SSL 会话缓存类型和大小 
    ssl_buffer_size 1400;
    add_header X-UA-Compatible "IE=edge,chrome=1"; #IE 用最新内核渲染页面 

    server_name hhtjim.com www.hhtjim.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/hhtjim.com;

        include other.conf;
        #error_page   404   /404.html;
        include enable-php.conf;
        include wordpress.conf;

        if ($http_host !~ "^www.hhtjim.com$"){
                rewrite  ^(.*)    https://www.hhtjim.com$1 permanent;
            }
        if ($scheme = "http"){ 
            rewrite ^(.*)$  https://$host$1 permanent;  
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/hhtjim.com.log;
    }


重启nginx

>lnmp nginx restart

之后再访问就可以看到使用http2.0协议了

FAQ

若安装失败,请参照 Nginx 配置之完整篇

参考:
https://imququ.com/post/my-nginx-conf.html
http://www.cnblogs.com/shiv/p/5271711.html
http://www.jb51.net/article/47755.htm

其他文章:
本文固定链接:https://www.hhtjim.com/lnmp1-3-configured-in-nginx-enable-http-2-0-alpn.html
matrix
本文章由 matrix 于2017年04月17日发布在零零星星分类下,目前没有通告,你可以至底部留下评论。
转载请注明:lnmp1.3中配置Nginx启用HTTP/2.0 + ALPN-HHTjim'S 部落格
关键字:, ,

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!