LoginSignup
22
20

More than 5 years have passed since last update.

nginx 変数の値を確認したい時は add_header ディレイクティブを使うと超絶簡単

Last updated at Posted at 2016-03-24

add_header ディレイクティブについて

Module ngx_http_headers_module

Syntax:   add_header name value [always];
Default:  —
Context:  http, server, location, if in location

Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables.

HTTP ステータスコードが、200、201、204、206、301、302、303、304、または307で提供された HTTP レスポンスヘッダーに指定されたフィールドを追加します。値は変数を含む事ができます。

使い方

前提

下記の記事内容を最低限把握している程で実証をするため、インストール手順や公開ディレクトリの作成、設定後の再起動等は省略します。

nginx と php-fpm の仕組みをちゃんと理解しながら PHP の実行環境を構築する - Qiita

設定

set $variable "hoge";$variable に "hoge" をセットしています。 $request_filename は定義済みの変数です。
$request_filename 以外にも定義済みの変数が多数あります。確認したい方は下記を参照して下さい。

Module ngx_http_core_module

/etc/nginx/nginx.conf
location / {
    set $variable "hoge";
    add_header variable $variable;
    add_header request_filename $request_filename;
}

変数の値を確認

cURL コマンドに -I --head オプションをつけることで、HTTP レスポンスヘッダのみ取得する事ができます。

$ curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 24 Mar 2016 08:39:04 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
Connection: keep-alive
ETag: "5315bd25-264"
variable: hoge
request_filename: /usr/share/nginx/html/index.html
Accept-Ranges: bytes
22
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
20