nginx/設定
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
**設定 [#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_prot...
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/xm...
image/x-icon image/bmp appli...
text/javascript application/...
application/pdf application/...
application/rtf application/...
application/msword applicati...
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-f...
同ホストならソケットが好きです( ´ω`)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_ro...
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:!...
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 $documentroo...
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-f...
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $documentroo...
include fastcgi_params;
}
}
***HTTPリバースプロキシ+ファイルキャッシュ [#i93e2f95]
割愛したいくらいメジャー。~
基本だけ。
http {
proxy_cache_path /usr/local/nginx/cache levels=1:2 k...
キャッシュディレクトリの設定。
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...
http_500 http_502 htt...
}
}
}
***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 appli...
fastcgi_cache_path /usr/local/nginx/fastcgi_cache le...
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/...
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $documen...
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 invali...
}
}
}
など。~
上記設定にもう少し手を入れてWordPressで試したら~
参照系90ms前後→4ms前後程度に高速化。~
***HTTPロードバランサー [#a4486b24]
L4orL7+SSLオフロード+リバプロ的な(;´ω`)~
http {
include mime.types;
default_type application/octet-stream;
log_format ssl '[$time_local] $remote_addr $ssl_prot...
sendfile on;
keepalive_timeout 5;
keepalive_requests 100;
gzip on;
gzip_types text/css text/javascript text/plain appli...
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でも...
ip_hash : REMOTE_ADDRによるstickiness。Proxy経由...
sticky : Cookieによるstickiness。Cookie使えない...
ip_hashは標準で使用可能。
hashは http://wiki.nginx.org/HttpUpstreamRequ...
stickyは http://code.google.com/p/nginx-sticky-...
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/serve...
ssl_certificate_key /usr/local/nginx/conf/serve...
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.2;
ssl_ciphers ECDHE-RSA-RC4-SHA:RC4-SHA:CAMMELIA...
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,4...
┌──────┬────┬────┬────┐
│ 片肺 │ RR │ RR/RC4...
┌────────────┼──────┼────┼────┼────┤
│Time taken for tests (s)│ 21.144│ 14.383│ 72.755...
├────────────┼──────┼────┼────┼────┤
│Complete requests │ 10000│ 10000│ 10000...
│Failed requests │39(Length) │ 0│ 0...
│Non-2xx responses │39(code:499)│ 0│ 0...
├────────────┼──────┼────┼────┼────┤
│Total transferred (byte)│ 82111159│82420000│82420000...
│HTML transferred (byte)│ 80650964│80960000│80960000...
├────────────┼──────┼────┼────┼────┤
│Requests per second (ms)│ 472.95 │ 695.29 │ 197.45 ...
│Time per request (ms)│ 951.479│ 647.214│3273.987...
│Transfer rate (kb/sec)│ 3792.41 │5596.25 │1106.29 ...
└────────────┴──────┴────┴────┴────┘
Connection Times (ms,mean) CPU(LB)
┌──┬──┬───┬───┐ ┌──┬──┬───┬───┐
│片肺│ RR │RR/RC4│RR/AES│ │片肺│ RR │RR/RC4│...
┌─────┼──┼──┼───┼───┤ ├──┼──┼───┼───┤
│Connect │ 3│ 3│ 1616│ 2234│ │ 2%│ 2%│ 98%│ ...
│Processing│ 579│ 444│ 1630│ 1417│ └──┴──┴───┴───┘
│Waiting │ 578│ 442│ 1613│ 1387│
│Total │ 582│ 446│ 3247│ 3651│
└─────┴──┴──┴───┴───┘
RC4-SHA128でもこの重さ( ´ω`)
終了行:
**設定 [#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_prot...
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/xm...
image/x-icon image/bmp appli...
text/javascript application/...
application/pdf application/...
application/rtf application/...
application/msword applicati...
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-f...
同ホストならソケットが好きです( ´ω`)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_ro...
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:!...
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 $documentroo...
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-f...
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $documentroo...
include fastcgi_params;
}
}
***HTTPリバースプロキシ+ファイルキャッシュ [#i93e2f95]
割愛したいくらいメジャー。~
基本だけ。
http {
proxy_cache_path /usr/local/nginx/cache levels=1:2 k...
キャッシュディレクトリの設定。
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...
http_500 http_502 htt...
}
}
}
***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 appli...
fastcgi_cache_path /usr/local/nginx/fastcgi_cache le...
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/...
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $documen...
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 invali...
}
}
}
など。~
上記設定にもう少し手を入れてWordPressで試したら~
参照系90ms前後→4ms前後程度に高速化。~
***HTTPロードバランサー [#a4486b24]
L4orL7+SSLオフロード+リバプロ的な(;´ω`)~
http {
include mime.types;
default_type application/octet-stream;
log_format ssl '[$time_local] $remote_addr $ssl_prot...
sendfile on;
keepalive_timeout 5;
keepalive_requests 100;
gzip on;
gzip_types text/css text/javascript text/plain appli...
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でも...
ip_hash : REMOTE_ADDRによるstickiness。Proxy経由...
sticky : Cookieによるstickiness。Cookie使えない...
ip_hashは標準で使用可能。
hashは http://wiki.nginx.org/HttpUpstreamRequ...
stickyは http://code.google.com/p/nginx-sticky-...
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/serve...
ssl_certificate_key /usr/local/nginx/conf/serve...
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.2;
ssl_ciphers ECDHE-RSA-RC4-SHA:RC4-SHA:CAMMELIA...
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,4...
┌──────┬────┬────┬────┐
│ 片肺 │ RR │ RR/RC4...
┌────────────┼──────┼────┼────┼────┤
│Time taken for tests (s)│ 21.144│ 14.383│ 72.755...
├────────────┼──────┼────┼────┼────┤
│Complete requests │ 10000│ 10000│ 10000...
│Failed requests │39(Length) │ 0│ 0...
│Non-2xx responses │39(code:499)│ 0│ 0...
├────────────┼──────┼────┼────┼────┤
│Total transferred (byte)│ 82111159│82420000│82420000...
│HTML transferred (byte)│ 80650964│80960000│80960000...
├────────────┼──────┼────┼────┼────┤
│Requests per second (ms)│ 472.95 │ 695.29 │ 197.45 ...
│Time per request (ms)│ 951.479│ 647.214│3273.987...
│Transfer rate (kb/sec)│ 3792.41 │5596.25 │1106.29 ...
└────────────┴──────┴────┴────┴────┘
Connection Times (ms,mean) CPU(LB)
┌──┬──┬───┬───┐ ┌──┬──┬───┬───┐
│片肺│ RR │RR/RC4│RR/AES│ │片肺│ RR │RR/RC4│...
┌─────┼──┼──┼───┼───┤ ├──┼──┼───┼───┤
│Connect │ 3│ 3│ 1616│ 2234│ │ 2%│ 2%│ 98%│ ...
│Processing│ 579│ 444│ 1630│ 1417│ └──┴──┴───┴───┘
│Waiting │ 578│ 442│ 1613│ 1387│
│Total │ 582│ 446│ 3247│ 3651│
└─────┴──┴──┴───┴───┘
RC4-SHA128でもこの重さ( ´ω`)
ページ名: