#freeze
**設定 [#lc948edd]
あまりにこまごまと設定出来てしまうので~
ケース別の超簡単な例のみ(;´д`)

#contents

----
***単体HTTP/HTTPSサーバ [#d0ab72e7]
 user nobody;
 worker_processes  1;
 CPUコア(+HT)数までにしとく。
 
 #worker_cpu_affinity 0001 0010 0100 1000;
 複数CPUへの割り当て。
 
 error_log  logs/error.log;
 pid        logs/nginx.pid;
 
 events {
     worker_connections  1024;
     プロセスあたりのスレッド数=コネクション数
 }
  
 http {
 グローバルな設定はhttpディレクティブ内で。
 
     include       mime.types;
     default_type  application/octet-stream;
 
     access_log  logs/access.log  combined;
     単体だしグローバルで設定してもいいか。
     combinedは規定値で、Apacheのcombinedと同じ。
     
     log_format ssl '[$time_local] $remote_addr $ssl_protocol $ssl_cipher "$request" $body_bytes_sent';
     Apacheのssl_request_log相当を定義。
     HTTPSサーバ内で使用。
     
     sendfile        on;
     #tcp_nopush     on;
  
     #keepalive_timeout  0;
     keepalive_timeout  10;
     短めが好みです( ´ω`)
     
     keepalive_requests 100;
  
     gzip                    on;
     gzip_disable            "msie6";
     gzip_vary               on;
     gzip_min_length         512;
     gzip_buffers            256 8k;
     gzip_comp_level         6;
     gzip_types              text/plain test/html text/xml text/css
                             image/x-icon image/bmp application/atom+xml
                             text/javascript application/x-javascript
                             application/pdf application/postscript
                             application/rtf application/vnd.ms-powerpoint
                             application/msword application/vnd.ms-excel
                             application/vnd.wap.xhtml+xml;
     とりあえず圧縮しておく。
     
     server {
     ホストごとの設定はserverディレクティブ内で。
 
         listen       80;
         server_name  www.example.jp;
  
         #access_log  logs/host.access.log  main;
         ホストごとのログが欲しければserverディレクティブ内で設定。
         勿論ファイル名もユニークに。
          
         location / {
             root   html;
             index  index.html index.htm;
         }
 
         #error_page  404              /404.html;
         error_page  500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
         エラーページの設定。
         設定しなければ規定値が表示される。
         500系エラーはひとまとめにしている。
         locationでファイル位置を指定。
         = がついているので '/50x.html'と完全一致が条件。
         この場合、DocumentRootと同じなのでなくてもいい。
 
         location ~ \.php$ {
             root           html;
             #fastcgi_pass   127.0.0.1:9000;
             fastcgi_pass   unix:/usr/local/var/run/php-fpm/php-fpm.sock
             同ホストならソケットが好きです( ´ω`)
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include        fastcgi_params;
         }
         PHP-FPMとの連携。
         SCRIPT_FILENAMEの指定に注意。
     }
 
     server {
     HTTPSサーバの設定。
     
         listen       443;
         server_name  www.example.jp;
 
         access_log logs/ssl_request.log ssl;
 
         ssl                  on;
         ssl_certificate      cert.pem;
         ssl_certificate_key  cert.key;
 
         ssl_session_timeout  5m;
 
         ssl_protocols TLSv1 SSLv3;
         ssl_ciphers TLSv1:!aNULL:!eNULL:!RC2:!MD5:!DES:!IDEA:!EXP:!SEED@STRENGTH;
         ssl_prefer_server_ciphers   on;
 
         location / {
             root   html;
             index  index.html index.htm;
         }
         location ~ \.php$ {
             root           html;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $documentroot$fastcgi_script_name;
             include        fastcgi_params;
         }
     }
 
 }

***バーチャルホスト [#r17984cc]
httpディレクティブ内に並べていくだけ( ´ω`)簡単

     server {
 
         listen       80;
         ポートベースならlistenを変更。
 
         server_name  www.example.com;
         ネームベースならserver_nameを変更。
         複合なら両方変更。
 
         access_log  logs/com.access.log combined;
         ホスト別のアクセスログ取得。
          
         location / {
             root   html;
             index  index.html index.htm;
         }
 
         location ~ \.php$ {
             root           html;
             #fastcgi_pass   127.0.0.1:9000;
             fastcgi_pass   unix:/usr/local/var/run/php-fpm/php-fpm.sock
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $documentroot$fastcgi_script_name;
             include        fastcgi_params;
         }
     }

***HTTPリバースプロキシ+ファイルキャッシュ [#i93e2f95]
割愛したいくらいメジャー。~
基本だけ。

 http {
     proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
     キャッシュディレクトリの設定。
 
     server {
     フロントエンド(リバースプロキシ)。
         listen 80;
         server_name www.example.jp;
         
         location / {
             proxy_pass             http://xxx.xxx.xxx.xxx;
             バックエンド指定。
 
             proxy_set_header       Host $host;
             proxy_cache            STATIC;
             key_zoneの文字列を指定。
 
             proxy_cache_valid      200  1d;
             proxy_cache_use_stale  error timeout invalid_header updating
                                    http_500 http_502 http_503 http_504;
         }
     }
 }

***FastCGIキャッシュ [#lf0aa6e1]
1台のホストにフロントエンドとバックエンドのHTTPサーバを立てて~
上記リバプロ+キャッシュにしている構成しか見かけないが~
それはどうなんだろうという立ち位置( ´ω`)

 よくわかんないんだけど
 同じホストの上で2つサーバ立ててもCPU負荷が分散されるわけでもないし
 肝はリバプロというよりキャッシュなんだろうから
 ┌─ホスト────────────┐
 │Front(80)---Back(8080)---FastCGI│
 │   │                           │
 │  Cache                         │
 └────────────────┘
 という構成をとるぐらいなら
 ┌─ホスト────────────┐
 │Front(80)---FastCGI             │
 │   │                           │
 │  Cache                         │
 └────────────────┘
 でよくね?

ということで、例えば
 http {
     include       mime.types;
     default_type  application/octet-stream;
 
     sendfile        on;
     keepalive_timeout  1;
 
     gzip on;
     gzip_types text/css text/javascript text/plain application/xhtml+xml application/xml;
 
     fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=CACHE:10m;
     fastcgi_cache_key  "$scheme$proxy_host$request_uri";
 
     server {
         listen       80;
         server_name www.example.jp;
 
         index index.php index.html;
         root html;
 
         location ~ \.php$ {
             fastcgi_pass        unix:/usr/local/var/run/php-fpm.sock;
             fastcgi_index       index.php;
             fastcgi_param       SCRIPT_FILENAME $document_root$request_filename;
             include             fastcgi_params;
             fastcgi_cache       CACHE;
             fastcgi_cache_valid 200 302 1h;
             fastcgi_cache_valid 301 1d;
             fastcgi_cache_valid any 1m;
             fastcgi_cache_min_uses 1;
             fastcgi_cache_use_stale error timeout invalid_header http_500;
         }
     }
  }
など。~
上記設定にもう少し手を入れてWordPressで試したら~
参照系90ms前後→4ms前後程度に高速化。~
 
***HTTPロードバランサー [#a4486b24]
L4orL7+SSLオフロード+リバプロ的な(;´ω`)~

 http {
     include       mime.types;
     default_type  application/octet-stream;
 
     log_format ssl '[$time_local] $remote_addr $ssl_protocol $ssl_cipher "$request" $body_bytes_sent';
 
     sendfile        on;
 
     keepalive_timeout    5;
     keepalive_requests 100;
 
     gzip on;
     gzip_types text/css text/javascript text/plain application/xhtml+xml application/xml;
 
     proxy_set_header Accept-Encoding "";
     proxy_set_header Host $http_host;
     proxy_set_header X-Forwarded-For $remote_addr;
     バックエンドにREMOTE_ADDRを渡す用。
 
     upstream backend {
     バックエンドサーバプール。
 
         hash $sticky;
         #ip_hash;
         #sticky;
         パーシステンス。
         hash    : URLパースによるstickiness。Cookieでもおk。
         ip_hash : REMOTE_ADDRによるstickiness。Proxy経由のアクセスでやや難あり。
         sticky  : Cookieによるstickiness。Cookie使えない携帯電話はダメ。
         
         ip_hashは標準で使用可能。
         hashは  http://wiki.nginx.org/HttpUpstreamRequestHashModule で
         stickyは http://code.google.com/p/nginx-sticky-module/ で。
         
         server xxx.xxx.xxx.xxx:80;
         server yyy.yyy.yyy.yyy:80;
         SSL処理はフロントで引き受けるので後ろはHTTPだけでよい。
     }
 
     upstream staticbackend {
         パーシステンス不要な静的ファイルはラウンドロビンにしてみる。
         server aaa.aaa.aaa.aaa:80;
         server bbb.bbb.bbb.bbb:80;
     }
 
     server {
         listen      80;
         server_name www.example.jp;
         access_log  logs/host.access.log combined;
  
         location ~ \.ico$ {
         例えばfavicon.icoは静的ファイル用バックエンドに渡す。
             proxy_pass http://staticbackend;
         }
  
         location / {
             if ($args ~ "hoge=(.+?)&{0,1}"){
                 set $sticky $1;
             }
             例えば(よくないだろうが)GETクエリに
             セッションID(hoge=xxxxxxxxxxxxx)が入っているとして
             $stickyにセットして、hash $stickyに渡すと
             セッション中は同じサーバへ振り分けられる。
             $argsの代わりに$http_cookieでCookieの値を引っ張れる。
             
             proxy_pass http://backend;
         }
     }
 
     server {
     SSL処理を引き受ける。なんだかんだで重いので注意。
 
         listen      443;
         server_name www.example.jp;
         access_log  logs/host.access.log combined;
         access_log  logs/timing.log timing;
         access_log  logs/uphead.log up_head;
         access_log  logs/ssl_request.log ssl;
 
         ssl                  on;
         ssl_certificate      /usr/local/nginx/conf/server.crt;
         ssl_certificate_key  /usr/local/nginx/conf/server.key;
 
         ssl_session_timeout  5m;
 
         ssl_protocols SSLv3 TLSv1 TLSv1.2;
         ssl_ciphers   ECDHE-RSA-RC4-SHA:RC4-SHA:CAMMELIA256:HIGH:MEDIUM:!aNULL:!MD5;
         ssl_prefer_server_ciphers   on;
 
         location ~ \.ico$ {
             proxy_pass http://staticbackend;
         }
 
         location / {
             if ($args ~ "hoge=(.+?)&{0,1}"){
                 set $sticky $1;
             }
             proxy_pass http://backend;
         }
     }
 }
どのくらい重いかというと
 abを放ってみた。
 バックエンドがネックにならないように
 8kbのファイルを返すだけ。
 -c450がRRでもエラーが返らないギリの同時接続数。
 
 Server Software:        nginx/1.3.4(LB)
 Document Path:          /test.php
 Document Length:        8096 bytes
 Concurrency Level:      450
 
 SSLは
 Server Port:            443
 SSL/TLS Protocol:       TLSv1/SSLv3,RC4-SHA,4096,128
 と
 SSL/TLS Protocol:       TLSv1/SSLv3,DHE-RSA-AES256-SHA,4096,256
 
                           ┌──────┬────┬────┬────┐
                           │    片肺    │   RR   │ RR/RC4 │ RR/AES │
 ┌────────────┼──────┼────┼────┼────┤
 │Time taken for tests (s)│      21.144│  14.383│  72.755│  82.012│
 ├────────────┼──────┼────┼────┼────┤
 │Complete requests       │       10000│   10000│   10000│   10000│
 │Failed   requests       │39(Length)  │       0│       0│       0│
 │Non-2xx  responses      │39(code:499)│       0│       0│       0│
 ├────────────┼──────┼────┼────┼────┤
 │Total transferred (byte)│    82111159│82420000│82420000│82420000│
 │HTML  transferred (byte)│    80650964│80960000│80960000│80960000│
 ├────────────┼──────┼────┼────┼────┤
 │Requests per second (ms)│     472.95 │ 695.29 │ 197.45 │ 121.93 │
 │Time per request    (ms)│     951.479│ 647.214│3273.987│3690.545│
 │Transfer rate   (kb/sec)│    3792.41 │5596.25 │1106.29 │ 981.71 │
 └────────────┴──────┴────┴────┴────┘
 Connection Times (ms,mean)                 CPU(LB)
             ┌──┬──┬───┬───┐ ┌──┬──┬───┬───┐
             │片肺│ RR │RR/RC4│RR/AES│ │片肺│ RR │RR/RC4│RR/AES│
 ┌─────┼──┼──┼───┼───┤ ├──┼──┼───┼───┤
 │Connect   │   3│   3│  1616│  2234│ │  2%│  2%│   98%│   98%│
 │Processing│ 579│ 444│  1630│  1417│ └──┴──┴───┴───┘
 │Waiting   │ 578│ 442│  1613│  1387│
 │Total     │ 582│ 446│  3247│  3651│
 └─────┴──┴──┴───┴───┘
 RC4-SHA128でもこの重さ( ´ω`)

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS