というより、nginxに乗り遅れている自分へのメモ。
nginx コマンドでの 起動,停止 等
http://nginx.org/en/docs/beginners_guide.html
| コマンド | 内容 |
|---|---|
| ./sbin/nginx | START |
| ./sbin/nginx -s stop | STOP |
| ./sbin/nginx -s quit | GRACEFUL STOP |
| ./sbin/nginx -s reload | 設定ファイルの再読み込み |
| ./sbin/nginx -s reopen | ログファイルの再open |
| ./sbin/nginx -V | ver. とconfigureオプションを表示 |
| ./sbin/nginx -t | 設定ファイルのテスト |
| ./sbin/nginx -h | ヘルプ表示 |
error_logに出力するログレベル
http://nginx.org/en/docs/ngx_core_module.html
エラーログのログレベルには debug | info | notice | warn | error | crit | alert | emerg があり、 デフォルトは以下のようにerrorです
error_log logs/error.log error;
access_logの書式
http://nginx.org/en/docs/http/ngx_http_log_module.html http://httpd.apache.org/docs/2.2/ja/mod/mod_log_config.html
apacheではcombined に 処理時間(micro sec)を追加出力し、利用していましたが、 同様のログ出力にする場合、次のように記載します。
apacheの例
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" combined
nginxの例
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';
apache x nginx の比較
| 項目 | apache | nginx |
|---|---|---|
| リモートホスト | %h | $remote_addr |
| クライアント識別子 | %l | なし "-"固定 |
| 認証ユーザー名 | %u | $remote_user |
| 時刻 | %t | $time_local |
| リクエスト | %r | $request |
| STATUS | %s | $status |
| BYTE SIZE | %b | $body_bytes_sent |
| リファラー | %{Referer} | $http_referer |
| ユーザエージェント | %{User-Agent} | $http_user_agent |
| 処理時間 | %D (micro sec) | $request_time (milli sec) |
| X-Forwarded-For | %{X-Forwarded-For} | $http_x_forwarded_for |
| クッキー | %{cookie} | $cookie_XXX (cookie名=XXX) |
リバースプロキシ時にバックエンドサーバにクライアントのIPを渡す
nginx設定ファイルに以下の記載を行うことで、「X-Forwarded-For」に元々のクライアントIPが入ります。
server {
listen 80;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
:
}
}
ロードバランス
http://nginx.org/en/docs/http/load_balancing.html
upstream backend {
ip_hash; #アクセス元が同じ場合、同じバックサーバへ
server back-server-1:8080;
server back-server-2:8080;
server back-server-3:8080 backup; #バックサーバダウン時にのみ利用
#(sorry pageの表示等に使用)
}
BASIC認証
http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}
WAF (Web Application Firewall)として利用する
ModSecurityを利用します。 https://www.modsecurity.org/ http://www.atmarkit.co.jp/ait/articles/1409/18/news010_4.html
サーバ-ステータス
apacheの例 ( mod_status )
LoadModule status_module modules/mod_status.so
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from localhost
</Location>
nginxの例 ( stub_status )
server {
listen 80;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
file open数の上限設定
サーバ自体と、nginxの両方の設定が必要です
# vi /etc/sysctl.conf fs.file-max=320000
worker_rlimit_nofile 4096;
TIME_WAIT時にRST受信した場合、TIME_WAIT期間の終了を待たずにクローズ
http://d.hatena.ne.jp/end0tknr/20110724/1311490171 以前のエントリに同様の内容がありますが、次の方法もあるようでs.
# vi /etc/sysctl.conf net.ipv4.tcp_rfc1337 = 1