nginx如何啟用對HTTP2的支持 | nginx如何驗證HTTP2是否已啟用


nginx啟用HTTP2特性

查看當前nginx的編譯選項

1 #./nginx -V
2  
3 nginx version: nginx/1.9.15
4 built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
5 built with OpenSSL 1.0.2g  1 Mar 2016
6 TLS SNI support enabled
7 configure arguments: --prefix=/home/jackie/software/nginx --with-openssl=/home/jackie/Downloads/nginx/openssl-1.0.2g --with-pcre=/home/jackie/Downloads/nginx/pcre-8.38 --with-zlib=/home/jackie/Downloads/nginx/zlib-1.2.8 --with-http_ssl_module --with-threads --with-debug

 

啟用http2支持

  1. 修改編譯選項
    configure的選項中加入--with-http_v2_module,由於HTTP2需要SSL的支持,因此如缺少--with-http_ssl_module選項,還需要加入--with-http_ssl_module。 如下:

     

    ./configure --prefix=/home/jackie/software/nginx \ --with-openssl=/home/jackie/Downloads/nginx/openssl-1.0.2g \ --with-pcre=/home/jackie/Downloads/nginx/pcre-8.38 \ --with-zlib=/home/jackie/Downloads/nginx/zlib-1.2.8 \ --with-http_ssl_module \ --with-threads \ --with-debug \ --with-http_v2_module 
  2. 編譯&升級
    make & make install 
  3. 修改配置文件,啟用HTTP2,如下:
    server {
        listen       8443 ssl http2 default_server; # 增加 http2 default_server server_name 192.168.0.107; ... } 
  4. 驗證配置文件
    #./nginx -t
    nginx: the configuration file /home/jackie/software/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /home/jackie/software/nginx/conf/nginx.conf test is successful
    
  5. 啟動nginx
    #./nginx
    

驗證HTTP2是否已啟用

方法一

使用高版本如56.0.2924.87的Chrome,按照如下步驟操作

  1. 使用Chrome訪問啟用http2的站點,比如Jackie的環境為https://192.168.0.107:8443。
  2. 新開TAB頁,在地址欄中輸入chrome://net-internals/#http2,檢查HTTP/2 sessions下的表格。
  3. 確認表格里是否出現了上一步訪問的主機地址,比如192.168.0.107:8443。

方法二

使用curl命令,參考HTTP/2 with curl,執行如下命令,確認站點返回的協議是否為HTTP

curl --http2 -I 192.168.0.107:8443

如執行上述命令時遇到如下錯誤,說明系統當前安裝的curl還不支持HTTP2協議。

curl https://192.168.0.107:8443/ --http2 curl: (1) Unsupported protocol 

可以執行如下命令,檢查系統當前安裝的curl支持的特性列表,確認是否包含HTTP2。

curl -V curl 7.47.0 (i686-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets 

從前述輸出信息可以了解到,當前安裝的curl還不支持HTTP2。
這時可參考如何啟用curl命令HTTP2支持重新編譯curl,加入HTTP2的支持。

方法三

安裝Chrome插件HTTP/2 and SPDY indicator,安裝完畢后訪問啟用HTTP2的站點,如果地址欄出現藍色的閃電,說明站點已啟用HTTP2。
不過Jackie身在牆內,安裝總是失敗,所以沒有驗證該方法的有效性。

完整的配置文件

如下是完整的配置文件,刪除了一些與HTTP2特性不相關的內容。

01 worker_processes  1;
02 error_log  logs/error.log  debug;
03  
04 events {
05     worker_connections  1024;
06 }
07  
08 http {
09     include       mime.types;
10     default_type  application/octet-stream;
11     server_tokens off;
12  
13     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
14                       '$status $body_bytes_sent "$http_referer" '
15                       '"$http_user_agent" "$http_x_forwarded_for"';
16  
17     access_log  logs/access.log  main;
18  
19     sendfile        on;
20     tcp_nopush     on;
21  
22     keepalive_timeout  65;
23     charset utf-8;
24  
25     server {
26         listen 8080;
27         server_name  192.168.0.107;
28         if ($scheme ~ http) {
29             return https://$server_name:8443$request_uri;
30         }
31     }
32  
33     server {
34         listen       8443 ssl http2 default_server;
35         server_name  192.168.0.107;
36  
37         ssl_certificate      /home/jackie/software/nginx_conf/server.crt;
38         ssl_certificate_key  /home/jackie/software/nginx_conf/server.key;
39  
40         ssl_session_cache    shared:SSL:1m;
41         ssl_session_timeout  5m;
42  
43         ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA';
44         ssl_prefer_server_ciphers  on;
45  
46         add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" always;
47         add_header X-Frame-Options SAMEORIGIN always;
48         add_header X-XSS-Protection "1; mode=block" always;
49         add_header X-Content-Type-Options nosniff;
50  
51         location ~ / {
52             index  index.jsp;
53             proxy_pass http://127.0.0.1:18080;
54             proxy_set_header        referer           '';
55             include proxy.conf;
56         }
57  
58         #error_page  404              /404.html;
59  
60         # redirect server error pages to the static page /50x.html
61         #
62         error_page   500 502 503 504  /50x.html;
63         location = /50x.html {
64             root   html;
65         }
66  
67         location ~ /\. {
68             deny all;
69             access_log off;
70             log_not_found off;
71         }
72     }
73  

74 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM