node.jsでSPDY対応ウェブサーバ

提供: Node.js/JavaScript入門
移動: 案内検索
スポンサーリンク

node.jsでSPDYに対応したウェブサーバを作成してみます。SPDYは、HTTPを速くするためのもので、HTTPSで利用します。node.jsでSPDYを扱うには、spdyモジュールを使用します。

読み方

SPDY
すぴーでぃ、えすぴーでぃーわい

概要

SPDYに対応したHTTPSサーバ(ウェブサーバ)を作る方法は、node.jsでHTTPSサーバを作成する要領と変わりません。

インストール

spdyモジュールをインストールします。

$ sudo node install -g spdy

ソースコード

/*
 * spdy1.js
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
var spdy = require('spdy');
var fs = require('fs');
var ssl_server_key = 'server_key.pem';
var ssl_server_crt = 'server_crt.pem';
var port = 8443;
 
var options = {
        key: fs.readFileSync(ssl_server_key),
        cert: fs.readFileSync(ssl_server_crt)
};
 
var server = spdy.createServer(options, function(req,res){
        res.writeHead(200);
        res.end("Hello SPDY\n");
});
server.listen(port);

実行例

spdyに対応したウェブサーバを起動します。

$ node spdy1.js

curlコマンドでアクセスしてみます。

$ curl https://localhost:8443/ --insecure
Hello SPDY

-vでcurlの詳細を出してみましたが、これだとわからないですね。

$ curl -v https://localhost:8443/ --insecure
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8443 (#0)
* successfully set certificate verify locations:
*   CAfile: /usr/local/share/certs/ca-root-nss.crt
  CApath: none
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / AES256-GCM-SHA384
* Server certificate:
*        subject: C=JP; ST=Tokyo; L=Musashino-shi; O=Foo; OU=Bar; CN=foo.bar.com
*        start date: 2014-08-31 06:58:09 GMT
*        expire date: 2014-08-31 06:58:09 GMT
*        issuer: C=JP; ST=Tokyo; L=Musashino-shi; O=Foo; OU=Bar; CN=foo.bar.com
*        SSL certificate verify result: self signed certificate (18), continuing anyway.
> GET / HTTP/1.1
> User-Agent: curl/7.36.0
> Host: localhost:8443
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 31 Aug 2014 11:10:24 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
<
Hello SPDY
* Connection #0 to host localhost left intact

spdycat を使って、見てみましょう。

$ spdycat -v https://localhost:8443/
[  0.004] NPN select next protocol: the remote server offers:
          * spdy/3.1
          * spdy/3
          * spdy/2
          * http/1.1
          * http/1.0
          NPN selected the protocol: spdy/3.1
[  0.012] Handshake complete
[  0.012] recv SETTINGS frame <version=3, flags=0, length=20>
          (niv=2)
          [4(1):100]
          [7(1):1048576]
[  0.013] send SYN_STREAM frame <version=3, flags=1, length=217>
          (stream_id=1, assoc_stream_id=0, pri=3)
          :host: localhost:8443
          :method: GET
          :path: /
          :scheme: https
          :version: HTTP/1.1
          accept: */*
          accept-encoding: gzip, deflate
          user-agent: spdylay/1.2.5
[  0.013] recv WINDOW_UPDATE frame <version=3, flags=0, length=8>
          (stream_id=0, delta_window_size=983040)
[  0.111] recv SYN_REPLY frame <version=3, flags=0, length=55>
          (stream_id=1)
          :status: 200 OK
          :version: HTTP/1.1
          date: Sun, 31 Aug 2014 11:09:56 GMT
Hello SPDY
[  0.111] recv DATA frame (stream_id=1, flags=0, length=11)
[  0.111] recv DATA frame (stream_id=1, flags=1, length=0)
[  0.111] send GOAWAY frame <version=3, flags=0, length=8>
          (last_good_stream_id=0)

opensslコマンドをクライアントモードで使用して、TLSの情報をひっぱってみました。

$ openssl s_client -connect localhost:8443 < /dev/null |ag -i tls
...
New, TLSv1/SSLv3, Cipher is AES256-GCM-SHA384
    Protocol  : TLSv1.2
    TLS session ticket lifetime hint: 300 (seconds)
    TLS session ticket:

関連項目




スポンサーリンク