@Konboi memo

主に技術に関してつらつらと。

nginxにカスタムヘッダーのレスポンスを表示する

タイトルのようなことがやりたかった時に色々調べたでそれのメモ。

アプリは仮に下記のようなコードがあったとする

require 'sinatra'

get '/' do
    status 200
    headers \
        “X-Unchi-Status" => ‘BuriBuri’
    body "hello"
end

GETでアクセスしてきたとき

  • ステータスは200
  • カスタムヘッダ X-Unchi-StatusBuriBuri とセットして

と返す

これのカスタムヘッダ X-Unchi-Status の結果をnginxのlogに流すにはnginx.conf 等に

sent_http_xxxxxx の部分に自分で設定したカスタムヘッダを入れてあげる。

今回だと

log_format test ‘unchi-status:$sent_http_x_unchi_status` 

…

access_log /var/log/access.log test;

みたいな感じに設定してあげれば。

すると

log に流れてくる。

快適なlog life を!!

参考ページ

http://nginx.org/en/docs/http/ngx_http_log_module.html

Header lines sent to a client have the prefix “sent_http_”, for example, $sent_http_content_range.

ハイパフォーマンスHTTPサーバ Nginx入門

ハイパフォーマンスHTTPサーバ Nginx入門