nginxのメモ。

インストール

ダウンロード:

wget http://nginx.org/download/nginx-0.8.52.tar.gz

展開:

tar xvfz nginx-0.8.52.tar.gz
cd nginx-0.8.52

コンパイルとインストール:

./configure --prefix=/usr/local/nginx
make
make install

起動と停止

起動。

/usr/local/nginx/sbin/nginx

停止。

/usr/local/nginx/sbin/nginx -s stop

コンフィグ変更時

コンフィグの確認

nginx -t

コンフィグの再読込

/etc/init.d/nginx reload

コンフィグサンプル

server_nameを複数割り当てて、リダイレクトで一つにまとめる

serverディレクティブ内でserver_name, rewriteを使って頑張る。 example.comに別名dev.example.comをつけて、devでアクセスされたらリダイレクトするには次のようにする。

server_name example.com dev.example.com;
if ($host = 'hoge.example.com') {
    rewrite ^/(.*)$  http://example.com/$1 permanent;
}

未定義のHostヘッダでサクセスされたら何も返さない

serverディレクティブで許可するホスト名を列挙し、マッチしない場合には444を返す。

if ($host !~ ^(example.com|www.example.com)$ ) {
    return 444;
}

もしくはserver_name _(アンダースコア)として定義以外のリクエスト全てを受け取るようにする。

server {
    listen 80 default_server;
    server_name _;
    access_log  off;
}

特定のアクセス先へのログを出力しない

locationの中で条件付けしてaccess_log off;する。

location / {
    fastcgi_pass 127.0.0.1:8000;

    if ( $request_uri ~ /favicon.ico ) { access_log off; return 204; }
    if ( $request_uri ~ /healthcheck ) { access_log off; }
}

リダイレクト

rewriteディレクティブを使う。permanentを付けると301, redirectを付けると302になる。

server {
    server_name old.example.com;
    rewrite ^(.*) http://new.example.com$1 permanent;
}

ifディレクティブで複合条件を使う

ググってみると変数を組み立てて処理するのがポピュラーみたい。

# メンテナンスフラグ
if (-f /some/path/lock/maintenance) {
  set $maintenance 1;
}
# ヘルスチェックURLは、メンテナンスモードの場合にfakeレスポンスを返す
if ($uri ~ "^/healthcheck/") {
  set $fake_healthresponse "${maintenance}1";
}
...
if ($fake_healthresponse = "11") {
  return 200;
}
if ($maintenance = 1) {
  error_page 503 @maintenance;
  return 503;
}

認証情報の作成

apacheのhtpasswdコマンドを使用します。

ユーザの追加::

htpasswd -b /etc/nginx/.htpasswd username password

ユーザの削除::

htpasswd -D /etc/nginx/.htpasswd username

BASIC認証

/etc/nginx/nginx.conf::

location ~ ^/(hogehoge|foobar)(.*)$ {
    auth_basic  "basic authentication";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

fastcgi_params

djangoで使うときはSCRIPT_NAMEが渡ると誤作動するようです。

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

# fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
# fastcgi_param  REDIRECT_STATUS    200;

somedomain.conf

user  nobody; 
worker_processes  1; # CPUコア数と同じにします 

#error_log  logs/error.log debug;
#error_log  logs/error.log  info;
error_log  logs/error.log  notice;

pid        logs/nginx.pid;

events {
    worker_connections  1024; # 受け付けるコネクションの最大数
}

http {
    include       mime.types;

    #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  logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80; # bindポート
        server_name  192.168.100.40; # bind IPアドレス

        charset utf-8; # デフォルトキャラクタセット

        access_log  logs/host.access.log main;

        location / {
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:8000;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_pass_header Authorization;
            fastcgi_intercept_errors off;
        }
    }
}

ファイルの有無で分岐(メンテナンスモードとか)

server {
   if (-f $document_root/system/maintenance.html) {
     rewrite  ^(.*)$  /system/maintenance.html last;
     break;
   }
}

リクエストの実行時間を出力する

ログの項目に、nginxがリクエストを処理するのに費やした時間を取得できる $request_time というパラメタがあるのでコレを使う。

http { 
    log_format performance '$time_local $request $request_time';
}

server {
    access_log /var/log/nginx/example.com-performance.log performance;
}

特定のIPアドレスだけ認証をかけない

認証は先勝ちで評価を抜けるので許可したいIPアドレスを先に並べておく。

server {
    #....
        location / {
            satisfy any;
            allow 100.100.100.100;
            # ここにbasic認証を掛けたくないIPを追加していく
            deny all;
            auth_basic "basic authentication";
            auth_basic_user_file /etc/nginx/.htpasswd;
        }
}

参考(というかまんま) http://d.hatena.ne.jp/podhmo/20110311/1299817584

特定のIPアドレスからのアクセス以外は503にする

メンテナンス時に、自社ネットワークからは普通に見えるけど、それ以外はメンテナンスページを表示したい、という場合に利用できます。

if ($remote_addr != "1.2.3.4") {
    return 503;
}

error_page 503 @maintenance;
location @maintenance {
    rewrite ^(.*)$ /error503.html break;
}

tips

リダイレクト

server {
    server_name  www.example.com;
    rewrite ^(.*) http://example.com$1 permanent;
}

monitコンフィグ

check process nginx with pidfile /usr/local/nginx/logs/nginx.pid
  start program = "/usr/local/nginx/sbin/nginx"
  stop program  = "/usr/local/nginx/sbin/nginx -s stop"
  group server

nginx-gridfs

GridFSはmongodbのストレージ、nginx-gridfsはnginxとGridFSをブリッヂするためのモジュール。

使ってわかったこと。

  • nginxコンフィグで指定したmongodbノードが落ちるとフェイルオーバ出来ない
  • nginxコンフィグで指定したmongodbノードが復旧してもリカバリ出来ない
  • 復旧にはnginxをリスタートする必要がある
  • mongodbのslaveノードから読み込めない

mongo-c-driverはフェイルオーバ機能を有しているようだから、 nginx-gridfsのエラーハンドリングが正しくない(あるいは実装されていない)ということか?

https://github.com/mdirolf/nginx-gridfs/issues#issue/11 これかなー。


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2013-04-16 (火) 15:56:25 (34d)