empty_gif

nginxにおいて表題のディレクティブに関して。

■ 環境

  • Nginx 1.12.2
  • Amazon Linux

■ empty_gif

empty_gif“というディレクティブがあるようだ。1×1ピクセルのgifを返してくれるらしい。ELB配下のAmazonLinuxにおいて、healthcheckに使用してみている。

location = /healthcheck.html {
    empty_gif;
    access_log off;
    break;
}

上記を”server“ディレクティブ配下に設定している。”location“ディレクティブなのでそこしかないが…。アクセスログにも出力させないようにしてしまった。これで少し幸せになれた。

以上。

nginx + ModSecurityでcore dump

表題の通り core dump を起こしていた。

■ 環境

  • ModSecurity 2.9.2
  • nginx 1.12.1
  • CentOS 6.9

■ error.log

下記のようなエラーログが出力された。

2017/11/09 19:03:34 [alert] 47480#47480: worker process 47482 exited on signal 11 (core dumped)

最近この環境を作り直した時に気がついたのだが、ModSecurityの公式のページに下記のような記載が増えていた。

NOTE: Some instabilities in the Nginx add-on have been reported (see the Github issues page for details). Please use the "nginx_refactoring" branch where possible for the most up to date version and stay tuned for the ModSecurity version 4.

とのことなので、”nginx_refactoring“を試してみることにした。

■ nginx_refactoring

GitHubからzipでダウンロードし展開したところ、”configure“がない。`autogen.sh`があるので実行してみた。

$ ./autogen.sh
./autogen.sh: line 11: libtoolize: コマンドが見つかりません
./autogen.sh: line 12: autoreconf: コマンドが見つかりません
./autogen.sh: line 13: autoheader: コマンドが見つかりません
./autogen.sh: line 14: automake: コマンドが見つかりません
./autogen.sh: line 15: autoconf: コマンドが見つかりません
$

これらのパッケージをインストールする。対象パッケージは下記の通り。

$ sudo yum install libtool automake autoconf

再度`autogen.sh`を実行したところ問題なく”configure“が生成された。後は通常通り。

$ ./configure --enable-standalone-module --disable-mlogc
$make

nginxもリコンパイルしてインストールしなおしたところ、前回と同様のアクセスをしたがcoreは吐かれなくなった。とりあえずその場しのぎではあるのだが解決としておく。

以上。

■ 関連

nginx + ModSecurity

server_names_hash_bucket_size

nginxで表題に関するwarningが出ていた。

■ 環境

  • nginx 1.12.1
  • CentOS 6.9

■ server_names_hash_bucket_size

設定を変更したので`configtest`で確認してみたところ、下記のwarningが出力されていた。

$ /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/etc/nginx.conf
nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_size
nginx: the configuration file /usr/local/nginx/etc/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/etc/nginx.conf test is successful
$

今回大量に”server“コンテキストを追加していたので、”server_names_has_max_size 512“に引っかかったのかと思ったが、大量とはいえどもそこまで追加していない。どうやら”server_names_hash_bucket_size 64“の方に引っかかっていたようである。”server_name“に長い名前が付いているとこのメッセージが出るようだ。

nginx.conf“の”http“コンテキストに下記を追加したところwarningは出力されなくなった。

server_names_hash_bucket_size 128;

以上。

nginxで処理時間をログに出力

表題の通り。以前のエントリでは”Apache“での設定だったので”nginx“でも。

■ 環境

  • nginx 1.4.6
  • Ubuntu 14.04

■ nginx

ちょうどこれが適用できる環境の”nginx“が少々古いがまぁ仕方がない。このversionで確認してしまう。

■ request_time

Module ngx_http_core_module
https://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_time

log_format“の最後に”$request_time“を下記のように追加してみた。

  log_format main '$http_x_forwarded_for - $remote_user [$time_local] '
                  '"$request" $status $body_bytes_sent '
                  '"$http_referer" "$http_user_agent" $request_time';

この設定を適用する。

$ sudo service nginx reload

ログには下記のように出力されていた。

XX.XX.XX.XX - - [17/May/2017:19:08:51 +0900] "GET /login HTTP/1.1" 200 2175 "http://example.com/login/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" 0.043

マニュアルによると”seconds with a milliseconds resolution“とあるので、上記は”0.043“秒といったところであろう。

以上。

■ 関連

access_logに”処理に掛かった時間”を記載

upstream_response_time

nginx + ModSecurity

表題の通り。nginxをソースコンパイルしてModSecurityを組み込む。

■ 環境

  • nginx 1.12.0
  • ModSecurity 2.9
  • CentOS 6.7

■ ModSecurity

ModSecurity: Open Source Web Application Firewall
http://www.modsecurity.org/

nginxへのインストールは下記を参照する。

Installion for NGINX
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Mannual#Installation_for_NGINX

まずは依存パッケージをインストールする。

$ sudo yum install httpd httpd-devel pcre pcre-devel libxml2-devel

httpd“自体もインストールしなければならないのか…。”pcre“あたりは理解できるが…。ソースはModSecurityのページから取得する。現在の最新versionは”2.9“のようである。

$ wget https://www.modsecurity.org/tarball/2.9/modsecurity-2.9.tar.gz

展開して`configure`。

$ tar zxf modsecurity-2.9.tar.gz
$ cd modsecurity-2.9
$ ./configure --enable-standalone --disable-mlogc
  :
$ make
  :
$

あとはソースコンパイルだけである。先日のエントリのようにやる。

$ cd nginx-1.12.0
$ configure --add-module=../modsecurity-2.9/nginx/modsecurity
  :
$ make
  :
$ sudo make install
  :
$

CRSとかも入れないと意味無いが、とりあえず今日はここまで。

以上。

■ 関連

nginxをソースインストール

nginx + ModSecurity