Http2作为新的协议,相比http1.2性能更好,使用Nginx开启Http2也很简单,只需要满足下面的条件即可

  • 已开启Https

然后

  • 在监听配置后面加上http2
  • 加入ssl_protocols TLSv1 TLSv1.2;
  • 加入ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

如下:

root@l:~/data/nginx# cat conf.d/liumou.conf 
upstream halo {
 	server 127.0.0.1:8080;
}
upstream https {
	server cx.liumou.site:443;
}

server {	
    listen 443 ssl http2; # 加入http2
    server_name liumou.site; 
    ssl_certificate  /usr/share/nginx/html/liumou.site_bundle.crt; 
    ssl_certificate_key /usr/share/nginx/html/liumou.site.key; 
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_protocols TLSv1  TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
    	proxy_pass http://halo;
	proxy_set_header HOST $host;
    	proxy_set_header X-Forwarded-Proto $scheme;
    	proxy_set_header X-Real-IP $remote_addr;
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /admin {
	deny    all;
    }
    location /arch {
	deny all;
    }

    access_log  /var/log/nginx/access-liumou.log  main;
}

server {
  listen 80;
  listen [::]:80;
  server_name liumou.site;
  client_max_body_size 1024m;
  location / {
    proxy_pass http://halo;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
   if ($host ~* "^liumou.site$") {
       rewrite ^/(.*)$ https://liumou.site/ permanent;
    }
  error_page 404  https://liumou.site;
  error_page 497  https://liumou.site;
  error_page 301  https://liumou.site;
}

验证

建议先清除浏览器缓存再验证

使用edge浏览器访问,打开开发者模式,然后进入

网络->全部->鼠标放到名称那一行->右键->勾选: 协议

具体看演示图,然后协议显示h2即表示成功开启

在这里插入图片描述