LoginSignup
10
9

More than 5 years have passed since last update.

ウェブキャッシュサーバnuster入門-1(基本)

Last updated at Posted at 2018-02-08

nusterとは

NusterはHAProxyを元に開発したキャッシュサーバで、HAProxyと互換性がある。
そしてHAProxyのACLを利用して細かいキャッシュルールを定義できる。

仕様は変更されるかもしれないので、最新のものはhttps://github.com/jiangwenyuan/nuster/blob/master/README.mdを参照してください。

ホームページ

https://github.com/jiangwenyuan/nuster

特徴

  • HAProxyのすべての機能や特徴がサポート、100%互換性
  • 動的キャッシング機能
    • HTTP method, URI, path, query, header, cookiesなどでキャッシング
    • HTTP request or response contentsなどでキャッシング
    • environment variables, server stateなどでキャッシング
    • SSL version, SNI などでキャッシング
    • connection rate, number, byte などでキャッシング
  • スーパー速い
  • キャッシュ purging
  • フロントエンドとバックエンド両方HTTPS
  • HTTP圧縮
  • HTTPリライトやリダイレクト
  • などなど

仕組み

TCP/HTTPロードバランサーとキャッシュサーバで使える

client - nuster(http/https load balancer, cache server) - App [- nuster(tcp load balancer)] - DB

セットアップ

インストール

make TARGET=linux2628 USE_LUA=1 LUA_INC=/usr/include/lua5.3 USE_OPENSSL=1 USE_PCRE=1 USE_ZLIB=1 
make install PREFIX=/usr/local/nuster/bin

lua, ssl, zlibいらない場合はパラメータから外す
TARGET: linux2628, linux26, linux24, linux24e, linux22, solaris, freebsd, openbsd, cygwin, custom, generic

docker

docker pull nuster/nuster
docker run -d -v /path/to/nuster.cfg:/etc/nuster/nuster.cfg:ro -p 8080:8080 nuster/nuster

コンフィグ

最小限ファイル

global
    nuster cache on 
    daemon
    nbproc 2
defaults
    retries 3
    option redispatch
    option dontlognull
    timeout client  300s
    timeout connect 300s
    timeout server  300s
frontend web1
    bind *:8080
    mode http
    default_backend app1
backend app1
    balance roundrobin
    mode http
    nuster cache
    nuster-rule all ttl 0
    server s1 10.0.0.101:8080
    server s2 10.0.0.102:8080
    server s3 10.0.0.103:8080

セクション

  • global: 全体に関連するパラメータを設定する
  • defaults: 各プロキシにおけるパラメータのデフォルト値を設定する
  • frontend: クライアントからリクエストを受ける側についてを設定する
  • backend: 受けたリクエストを振り分ける先のサーバについてを設定する
  • listen: frontendとbackendを合わせて1つで設定する

詳細はHAProxy configuration

cacheとして基本設定

  • globalにnuster cache onを設定する
  • backendにnuster cache onnuster-ruleを設定する

詳細はhttps://github.com/jiangwenyuan/nuster/blob/master/README.md#directives

起動

/usr/local/nuster/bin/haproxy -f nuster.conf

コンフィグ抜粋で

tcp load balancerとして

frontend mysql-lb
   bind *:3306
   mode tcp
   default_backend mysql-cluster
backend mysql-cluster
   balance roundrobin
   mode tcp
   server s1 10.0.0.101:3306
   server s2 10.0.0.102:3306
   server s3 10.0.0.103:3306

http load balancerとして

frontend web-lb
   bind *:80
   mode http
   default_backend apps
backend apps
   balance roundrobin
   mode http
   server s1 10.0.0.101:8080
   server s2 10.0.0.102:8080
   server s3 10.0.0.103:8080

https

frontend web
    bind *:8080 ssl crt XXX.pem                # HTTPSリクエストを受ける
    # bind *:8080
    mode http
    default_backend app
backend app
    balance roundrobin
    nuster cache off
    nuster-rule all
    mode http
    #server a1 10.0.0.101:8002                 # HTTPでbackendと通信
    server a2 10.0.0.101:8003 ssl verify none  # HTTPSでbackendと通信

クライアント、バックエンドサーバ両方HTTPSできる

user == https ==> nuster == https ==> backend
user == https ==> nuster == http  ==> backend
user == http  ==> nuster == https ==> backend
user == http  ==> nuster == http  ==> backend

POSTをキャッシング

frontend web1
    bind *:8080
    mode http
    use_backend app1a if { path /search }
    default_backend app1b
backend app1a
    balance roundrobin
    mode http
    option http-buffer-request
    nuster cache
    # /searchの結果を60秒間キャッシングする
    nuster-rule rpost ttl 60 if METH_POST
backend app1b
    #***

キャッシング例

cache /a.jpg, expire しない

nuster-rule r1 ttl 0 if { path /a.jpg }

staticファイルをcache

acl static path_beg -i /static/ /images/ /css/
nuster-rule static_files ttl 0 if static

ユーザー毎に /mypageをcache: keyにsessionIdを入れる

acl pathB path /mypage
nuster-rule r2 key method.scheme.host.path.delimiter.query.cookie_sessionId ttl 60 if pathB

リクエストが/a.html且つリスポンスにcache headerがあれば /a.htmlをcache

http-request set-var(txn.pathC) path
acl pathC var(txn.pathC) -m str /a.html
acl resHdrCache1 res.hdr(cache) yes
nuster-rule r3 if pathC resHdrCache1

connectionが100を超えると /heavy を 100 秒cache

acl heavypage path /heavy
acl tooFast be_conn ge 100
nuster-rule heavy ttl 100 if heavypage tooFast 

cache前にresponseにheaderを入れる

http-response set-header X-ASDF fdsa

cache前に圧縮する

backend app
  filter compression
  compression algo gzip
  compression type text/html text/plain text/css text/javascript
  nuster cache
  nuster-rule all

キャッシュ削除

ウェブキャッシュサーバnuster入門-2(Purge) で紹介

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9