【nginx运维基础8】配置支持http2协议

栏目: 服务器 · Nginx · 发布时间: 6年前

内容简介:。通过 `/usr/local/nginx/sbin/nginx -V` (注意是大写的V),查看当前nginx是否支持http2:

了解http2协议

  1. HTTP 2.0 的主要目标是改进传输性能,实现低延迟和高吞吐量。从另一方面看,HTTP 的高层协议语义并不会因为这次版本升级而受影响。 所有HTTP 首部、值,以及它们的使用场景都不会变
  2. 现有的任何网站和应用,无需做任何修改都可以在HTTP 2.0 上跑起来。不用为了利用HTTP 2.0 的好处而修改标记。HTTP 服务器必须运行HTTP 2.0 协议,但大部分用户都不会因此而受到影响

编译nginx的http2模块

通过 `/usr/local/nginx/sbin/nginx -V

` (注意是大写的V),查看当前nginx是否支持http2: --with-http_v2_module

nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2o  27 Mar 2018
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/usr/local/src/lnmp1.5/src/openssl-1.0.2o

如果没有的话在编译nginx时要加上这一行

./configure \
--user=www \
--group=www \
--with-http_v2_module \
--with-http_ssl_module \
--with-stream \
--with-openssl=./openssl-OpenSSL_1_1_0e \
--with-pcre=./pcre-8.40 --with-pcre-jit \
--with-zlib=./zlib-1.2.11
make && make install

生成证书

可以参考我上一篇博文,申请免费证书。也可以手动生成一个伪证书

cd /usr/local/nginx/conf/
mkdir key && cd key
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
 (根据提示随意的输入)
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

最终生成

[root@zhouzhou_01 key]# ll
总用量 8
-rw-r--r-- 1 root samba   0 6月  14 10:41 server.crt
-rw-r--r-- 1 root samba 749 6月  14 10:41 server.csr
-rw-r--r-- 1 root samba 963 6月  14 10:39 server.key

配置nginx

主要配置段如下

server {
    listen 80;
    server_name site.com www.site.com;
    add_header Strict-Transport-Security max-age=31536000;
    return 301 https://www.site.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name  www.site.com;
    root   /var/www/html/site;
    index  index.php index.html index.htm;
    
    access_log  /var/log/dnmp/nginx.site.access.log  main;
    error_log  /var/log/dnmp/nginx.site.error.log  warn;

    ssl on;
    ssl_certificate /etc/nginx/conf.d/certs/site/www.site.com.crt;
    ssl_certificate_key /etc/nginx/conf.d/certs/site/www.site.com.key;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";
    add_header Strict-Transport-Security max-age=31536000;
}

验证

  • 使用Chrome访问启用http2的站点。
  • 新开TAB页,在地址栏中输入chrome://net-internals/#http2,检查HTTP/2 sessions下的表格。
  • 确认表格里是否出现了上一步访问的主机地址。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Agile Web Development with Rails 4

Agile Web Development with Rails 4

Sam Ruby、Dave Thomas、David Heinemeier Hansson / Pragmatic Bookshelf / 2013-10-11 / USD 43.95

Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details. Tens of thousands of deve......一起来看看 《Agile Web Development with Rails 4》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具