nginxでSSI

表題の通り。Server Side Include (SSI)を試してみる。

■ 環境

  • nginx 1.11.5
  • Docker 1.12.1
  • Mac OSX El Capitan

■ SSI

単純にファイルをincludeしたりしたい。がPHPだのrubyだのでテンプレートを、、というのは面倒なのでSSIで対応する。

Module ngx_http_ssi_module
https://nginx.org/en/docs/http/ngx_http_ssi_module.html

設定ファイル(default.conf)は下記のようにした。

  1 server {
  2     listen       80;
  3     server_name  localhost;
  4     ssi on;
  :

これを反映させる。nginxはDockerで動かしている。

$ docker run -d --name nginx -v `pwd`:/usr/share/nginx/html:ro -v `pwd`/default.conf:/etc/nginx/conf.d/default.conf -p 10080:80 nginx

ここから本題のHTMLである。

2つのファイルを用意した。`include`する側のHTMLファイルと、`include`される側のHTMLファイルである。

`include`する側。

$ cat test.html
<!--#include virtual="/test2.html" -->
<div class="contents">ここはトップページです。(index.html)</div>
$

`include`される側。

$ cat test2.html
<!-- test2.html -->
<div class="header">ahahahaha</div>
<!-- test2.html --> 
$

実際にブラウザでアクセスすると`include`されているのがわかるであろう。ここでは`curl`で取得してみる。

$ curl localhost:10080/test.html
<!-- test2.html -->
<div class="header">ahahahaha</div>
<!-- test2.html -->
<div class="contents">ここはトップページです。(index.html)</div>
$

性能としてはどの程度になるのであろうか?いずれ計測してみよう。

以上。

■ 関連