kakakakakku blog

Weekly Tech Blog: Keep on Learning!

headers-more-nginx-module でヘッダー情報を変更する

github.com

nginx でヘッダー情報を変更できる headers-more-nginx-module を試してみた.nginx に標準に入っている Module ngx_http_headers_module と比べて Content-TypeContent-Length などのビルトインヘッダーを消すことができる点にメリットがある.

This is an enhanced version of the standard headers module because it provides more utilities like resetting or clearing "builtin headers" like Content-Type, Content-Length, and Server.

ディレクティブ

headers-more-nginx-module に含まれているディレクティブは計4個ある.

  • more_set_headers
  • more_clear_headers
  • more_set_input_headers
  • more_clear_input_headers

more_set_headersmore_clear_headers はレスポンスヘッダを変更できて,more_set_input_headersmore_clear_input_headers はリクエストヘッダを変更できる.今回は more_set_headersmore_clear_headers を試すことにした.試した環境は nginx 1.4.1 で結構古いけど...w

more_set_headers

more_set_headers を使うと,レスポンスヘッダにヘッダーを追加することができる.実際には追加だけでなく,更新や削除もできる.

まず Server を更新してみたところ Server: Apache と返すことができた.あまりに簡単で,もはや Server の値は信用できなさそうだなと思った.

server {
    location / {
        more_set_headers 'Server: Apache';
    }
}
$ curl --head http://192.168.33.10
HTTP/1.1 200 OK
-Server: nginx/1.4.1
Date: Mon, 09 May 2016 04:11:14 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Last-Modified: Mon, 07 Sep 2015 07:05:50 GMT
Connection: keep-alive
ETag: "55ed374e-0"
+Server: Apache
Accept-Ranges: bytes

また以下のようなフォーマットで書くことで more_clear_headers と同様にヘッダーを削除することができる.ちなみに nginx のバージョンを削除するなら headers-more-nginx-module を使うまでもなく server_tokens off; で良いかと.

  • Name:
  • Name
server {
    location / {
        more_set_headers 'Server:';
    }
}
$ curl --head http://192.168.33.10
HTTP/1.1 200 OK
-Server: nginx/1.4.1
Date: Mon, 09 May 2016 04:11:14 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Last-Modified: Mon, 07 Sep 2015 07:05:50 GMT
Connection: keep-alive
ETag: "55ed374e-0"
Accept-Ranges: bytes

さらに -s-t オプションを付けることで,特定の StatusContent-Type のときに限定してヘッダーを追加することができる.以下の例だと 404 のときに限定して X-Foo を追加している.

server {
    location / {
        more_set_headers -s 404 -t 'text/html' 'X-Foo: Bar';
    }
}
$ curl --head http://192.168.33.10/xxx.html
HTTP/1.1 404 Not Found
Server: nginx/1.4.1
Date: Mon, 09 May 2016 04:23:26 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 168
Connection: keep-alive
+X-Foo: Bar

more_clear_headers

次に more_clear_headers を使うと,レスポンスヘッダからヘッダーを削除することができる.以下のように多くのヘッダーを削除することができた.

server {
    location / {
        more_clear_headers 'Server';
        more_clear_headers 'Date';
        more_clear_headers 'Content-Type';
        more_clear_headers 'Content-Length';
        more_clear_headers 'Last-Modified';
        more_clear_headers 'ETag';
        more_clear_headers 'Accept-Ranges';
    }
}
$ curl --head http://192.168.33.10
HTTP/1.1 200 OK
-Server: nginx/1.4.1
-Date: Mon, 09 May 2016 04:26:43 GMT
-Content-Type: text/html; charset=utf-8
-Content-Length: 0
-Last-Modified: Mon, 07 Sep 2015 07:05:50 GMT
Connection: keep-alive
-ETag: "55ed374e-0"
-Accept-Ranges: bytes

ただし Connection は残ってしまう.Connection を消す場合は nginx のコア部分にパッチを当てる必要があると Limitations に書かれていた.

You cannot remove the Connection response header using this module because the Connection response header is generated by the standard ngx_http_header_filter_module in the Nginx core, whose output header filter runs always after the filter of this module. The only way to actually remove the Connection header is to patch the Nginx core, that is, editing the C function ngx_http_header_filter in the src/http/ngx_http_header_filter_module.c file.

まとめ

予約して買ったまま積読をしていた「nginx 実践入門」を今日から読んでいる.今まで nginx をちゃんと運用したことがなくて,何となく使っていたため,今回を機に形式的な知識が得られれば良いなと思っている.また同時に数多くある nginx のモジュールを試したり,lua-nginx-module で拡張してみたりと,実践的な知識も得たいと思っている.

nginx実践入門 (WEB+DB PRESS plus)

nginx実践入門 (WEB+DB PRESS plus)

シンタックスハイライト

GitHub で使ってるように diff 形式でシンタックスハイライトを載せてみたけど,色が違って見慣れないなーという印象を受けた.普通に緑と赤だったら良かったのにー.