Amazon EC2 上の nginx の 設定を確認する

8 月に nginx をインストールした(nginx を aws EC2 にインストールする - asa nisi masa)。今日はログを確認し、ちょっとだけ設定を変更してみる。

参考にした URL

実施したこと

  • 様々なファイルの在り処、設定内容を確認する
  • access.log を確認する
  • document root を変更する
  • 宿題を積む
    • HTTPS を設定したい
    • モニタリング等のために、ログに出力しておくと便利な項目があるか確認する
    • ログを S3 に出力したい
    • レスポンスを gzip 圧縮したい

設定を確認する

設定の確認方法を調べる

-V オプションで nginx のバージョン、コンパイルバージョン、設定パラメータを参照できる

man nginx

-V Print the nginx version, compiler version, and configure script parameters.

設定を確認する

以下。見易いよう、改行している。

$ nginx -V


nginx version: nginx/1.2.9
built by gcc 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)
TLS SNI support enabled
configure arguments:
--prefix=/usr/share/nginx
--sbin-path=/usr/sbin/nginx
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi
--pid-path=/var/run/nginx.pid
--lock-path=/var/lock/subsys/nginx
--user=nginx
--group=nginx
--with-file-aio
--with-ipv6
--with-http_ssl_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module
--with-http_image_filter_module
--with-http_geoip_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_degradation_module
--with-http_stub_status_module
--with-http_perl_module
--with-mail
--with-mail_ssl_module
--with-pcre
--with-google_perftools_module
--with-debug
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
--with-ld-opt=' -Wl,-E'

モジュールがテンコ盛りww

パス類をまとめてみる
prefix /usr/share/nginx
sbin-path /usr/sbin/nginx
conf-path /etc/nginx/nginx.conf 設定ファイル
error-log-path /var/log/nginx/error.log エラーログ
http-log-path /var/log/nginx/access.log アクセスログ
http-client-body-temp-path /var/lib/nginx/tmp/client_body
http-proxy-temp-path /var/lib/nginx/tmp/proxy
http-fastcgi-temp-path /var/lib/nginx/tmp/fastcgi
http-uwsgi-temp-path /var/lib/nginx/tmp/uwsgi
http-scgi-temp-path /var/lib/nginx/tmp/scgi
pid-path /var/run/nginx.pid
lock-path /var/lock/subsys/nginx
設定ファイルを確認する

/etc/nginx/nginx.conf

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

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

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
root /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

目についた箇所をメモ。

  • document root は、/usr/share/nginx/html
  • http のポートは、デフォルトのまま 80
  • https は、空けてない( SSL 証明書まだだし・・)
  • http ステータスとして 404, 500, 502, 503, 504 が返った場合は nginx で予め用意されたエラーページが表示される
  • アクセスログは、 main という名前で指定した項目を出力する。出力項目は、
    • $remote_addr
    • $remote_user
    • $time_local
    • $request
    • $status
    • $body_bytes_sent
    • $http_referer
    • $http_user_agent
    • $http_x_forwarded_for
  • gzip圧縮は、 OFF

ドキュメントルートを変更する

conf ファイルを編集する

http コンテキスト、server ディレクティブの、location 配下の root を /var/www に変更する。


http{
(略)
server {
(略)
location / {
root /var/www; index index.html index.htm;
}
}
}

コンテンツを用意する

nginx のデフォルトのページをコピーしておく。

sudo mkdir /var/www
sudo cp /usr/share/nginx/html /var/www


ドキュメントルートが変わったことを確認できるよう、index.html を変更してみる。

hello

nginx を 再起動する。

sudo service nginx stop
sudo service nginx start

以下は参考。再起動しなくても、reload あるいは force-reload でも行けそうな予感。configtest というのもあるのか。。

Usage: /etc/init.d/nginx {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}

ブラウザから確認する

OK.

あとでやりたい

HTTPS を設定したい

Configuring HTTPS servers

モニタリング等のために、ログに出力しておくと便利な項目があるか確認する

参考:Module ngx_http_log_module

現在の設定で出力されているログの例。404.html が 200 で返っているのはわざとです。。

124.35.xxx.xxx - - [15/Sep/2013:07:24:46 +0000] "GET /404.html HTTP/1.1" 200 3696 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36" "-"

(あ、タイムゾーンの設定していなかった。orz..)

main に含む
$body_bytes_sent クライアントに返すレスポンスボディのバイト数(レスポンスヘッダを除く)。Apache の mod_log_config の %B パラメータと互換性がある。 (0.3.10 前までは、$apache_bytes_sent と呼ばれていた)
$bytes_sent クライアントに返すバイト数
$connection コネクションのシリアル番号
$connection_requests コネクションの間のリクエスト数 (1.1.18)
$msec ログを出力した時刻(マイクロ秒の精度)
$pipe リクエストがパイプラインの場合(どゆこと?)は"p"と出力する。そうでない場合は、"."と出力する。
$request_length reques のバイト数 (request line, header, request body を含む)
$request_time nginx がリクエストを返すために要した時間。秒単位(ミリ秒の精度)で出力される。 最初のバイトがクライアントに読まれてから、最後のバイトがクライアントに返されてログに書き込まれるまでの時間。(0.5.19以前は、秒単位で表示)
$status レスポンスのステータスコード
$time_iso8601 ISO 8601 フォーマットのローカル時間。例) 2011-03-21T18:52:25+03:00 (0.9.6以降)
$time_local common log フォーマットのローカル時間(タイムゾーン指定したやつと思う)
$request
$remote_addr
$remore_user
$http_referer
$http_user_agent
$http_x_forwarded_for

$bytes_sent とか、 $request_length は魅力的・・
ただし、以下に挙げた fluentd を使用する際に、設定が面倒になると困るな。

ログを S3 に出力したい

fluentd を挟むといいよ、という記事が複数あった。
http://nozaq.hatenablog.com/entry/2012/03/12/002350

レスポンスを gzip 圧縮したい

言ってみたけど、対応していないブラウザって、どれだっけ?てのを確認してから。
設定自体は、ON にするだけでいいのかな。
nginx連載3回目: nginxの設定、その1 - インフラエンジニアway - Powered by HEARTBEATS