Nginx
- 1. Nginx
[en-juhn-eks]
まだリバースプロキシにApache使ってるんですか?
- 4. C10K Problem
• クライアント数があまりにも多くなると
サーバがパンクする問題
• 簡単な例は、プロセス数。Apacheは、1リ
クエスト=1プロセス。大量リクエストで
生成可能なプロセスが枯渇する。
• サーバ性能が上がっててもプロセス数の
最大は変わらない(30000程度)
• サーバがどんなに高性能になってもこの
問題はなくならない。
- 5. イベント駆動アーキテクチャ
• ブロッキングI/O
– データ処理中は他の処理を受け付けない
– 処理を待たせるため、大量のWorkerプロセスが必要
となる
– Apache
• ノンブロッキングI/O
– データ処理中でも他の処理を受け付ける
– 複数の処理を1度に受け付けて、待ち時間に他の処理
を実行できるので、Workerプロセスを効率的に使え
る。
– Nginx、Node.js
- 6. Nginxの構成例
コンテンツ
サーバ
(Apache,
Varnish Cache,
etc...)
Appサーバ
Nginx (Apache,Node,
Starman,etc...)
DB
Appサーバ
(Apache,Node,
Starman,etc...)
- 8. 設定
user nginx; error_page 500 502 503 504 /50x.html;
worker_processes 2; location = /50x.html {
root /usr/share/nginx/html;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; }
events { location / {
event epoll; proxy_redirect off;
worker_connections 1024;
proxy_connect_timeout 15;
}
proxy_send_timeout 15;
http { proxy_read_timeout 15;
include /etc/nginx/mime.types;
default_type application/octet-stream; proxy_set_header X-FORWARDED_PROTO http;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' proxy_set_header Host _:80;
'$status $body_bytes_sent "$http_referer" ' proxy_set_header X-Real-IP $remote_addr;
'"$http_user_agent" "$http_x_forwarded_for"'; proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
access_log /var/log/nginx/access.log main; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
sendfile on;
keepalive_timeout 65; proxy_pass http://app-server/;
}
upstream app-server { }
server localhost:8080; }
}
server {
listen 80;
server_name hostname.localhost.localdomain;
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 404;
たったこれだけの設定で
}
error_page 404 /404.html;
Apache以上の性能がたた
location = /404.html {
}
root /usr/share/nginx/html; きだせる
- 9. Nginxの特徴
• 下位サーバの戻りを気にせずに他の処理も受
け付けられるので、リバースプロキシに適し
ている
• 静的コンテンツが得意
(不足はあるがコンテンツキャッシュ機能も
存在する)
• 動的コンテンツは苦手
(動的コンテンツは、ApacheやNode.js,
Starmanに任せたほうが幸せ)