著者:髙田 知典
「NGINX」(エンジンエックス)は、人気の高いWebサーバーソフトウエ
アです。「NGINX Plus」は、オープンソース版のNGINXにさまざまな
機能拡張を施した商用版です。追加された拡張機能を利用すること
で、システムの可用性と堅牢性の向上や、運用の簡素化を実現できま
す。本特集では、NGINX Plusの機能や試用方法などを紹介します。
シェルスクリプトマガジン Vol.65は以下のリンク先でご購入できます。![]()
![]()
図2 アクティブヘルスチェックの設定例
upstream my_upstream {
zone my_upstream 64k;
server server1.example.com slow_start=30s;
server server2.example.com slow_start=30s;
}
server {
location / {
proxy_pass http://my_upstream;
health_check interval=5s uri=/test.php match=statusok;
}
}
match statusok {
status 200;
header Content-Type = text/html;
body ~ "Server[0-9]+ is alive";
}
図3 Sticky cookie の設定例
upstream my_upstream {
server server1.example.com;
server server2.example.com;
sticky cookie srv_id expires=1h;
}
図4 Sticky route の設定例
map $cookie_jsessionid $route_cookie {
~.+\.(?P<route>\w+)$ $route;
}
map $request_uri $route_uri {
~jsessionid=.+\.(?P<route>\w+)$ $route;
}
upstream backend {
server backend1.example.com route=a;
server backend2.example.com route=b;
sticky route $route_cookie $route_uri;
}
図5 Sticky learnの設定例
upstream backend {
server backend1.example.com;
server backend2.example.com;
sticky learn
create=$upstream_cookie_examplecookie
lookup=$cookie_examplecookie
zone=client_sessions:1m
timeout=1h;
}
図6 DNS名をAレコード情報を使って解決する設定例
resolver 10.0.0.2 valid=10s;
upstream backends {
zone backends 64k;
server backends.example.com:8080 resolve;
}
server {
location / {
proxy_pass http://backends;
}
}
図7 DNS名をSRVレコード情報を使って解決する設定例
resolver 10.0.0.2 valid=10s;
upstream backends {
zone backends 64k;
server backends.example.com service=_http._tcp resolve;
}
server {
location / {
proxy_pass http://backends;
}
}
図9 コンテンツキャッシュのパージ設定例
http {
(略)
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m purger=on;
map $request_method $purge_method {
PURGE 1;
default 0;
}
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass https://localhost:8002;
proxy_cache mycache;
proxy_cache_purge $purge_method;
}
}
geo $purge_allowed {
default 0;
10.0.0.1 1;
192.168.0.0/24 1;
}
map $request_method $purge_method {
PURGE $purge_allowed;
default 0;
}
}
図15 nginx-sync.confの設定例
NODES="node2.example.com node3.example.com node4.example.com"
CONFPATHS="/etc/nginx/nginx.conf /etc/nginx/conf.d"
EXCLUDE="default.conf"
図16 NGINX Plus APIの設定例
http {
(略)
# Configuration of the server group
upstream appservers {
# 共有メモリーにサーバーグループ構成を保存するようにゾーンを設定
zone appservers 64k;
server appserv1.example.com weight=5;
server appserv2.example.com:8080 fail_timeout=5s;
server reserve1.example.com:8080 backup;
server reserve2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://appservers;
health_check;
}
location /api {
# GET以外のメソッドをBasic認証で制限
limit_except GET {
auth_basic "NGINX Plus API";
auth_basic_user_file /etc/nginx/.htpasswd;
}
# APIを書き込みモードで動作させる
api write=on;
# アクセス元をローカルホストのみに制限
allow 127.0.0.1;
deny all;
}
}
}
図18 stateファイルの設定を追加した例
http {
(略)
# Configuration of the server group
upstream appservers {
zone appservers 64k;
state /var/lib/nginx/state/appservers.conf;
# server appserv1.example.com weight=5;
# server appserv2.example.com:8080 fail_timeout=5s;
# server reserve1.example.com:8080 backup;
# server reserve2.example.com:8080 backup;
}
}
図19 キーバリューストアの設定例
http {
# キーバリューストアの定義
keyval_zone zone=blacklist:1M state=/tmp/blacklist.json;
keyval $remote_addr $black_list zone=blacklist;
server {
listen 80;
location / {
root /usr/share/nginx/html;
if ($black_list = 0) {
return 403;
}
}
location /api {
# GET以外のメソッドをベーシック認証で制限
limit_except GET {
auth_basic "NGINX Plus API";
auth_basic_user_file /etc/nginx/.htpasswd;
}
# APIを書き込みモードで動作させる
api write=on;
#アクセス元をローカルホストのみに制限
allow 127.0.0.1;
deny all;
}
}
}