結論
- 特に設定しないとproxy_passで設定するupstreamのmax_failsは1
- proxy_next_upstreamに指定したstatus codeが出ると失敗扱いになってmax_failsに達すると一時的にそのupstreamは使えなくなる
検証
ポート30000, 30001で、常にHTTPステータス503を返すエンドポイントを適当にMac上で動かして
こんなnginx.confを作って
$ cat /tmp/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error_log info; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format ltsv "time:$time_local" "\tstatus:$status" "\treqtime:$request_time" "\tupstream:$upstream_addr" "\tupstream_status:$upstream_status" "\tvhost:$host"; access_log /var/log/nginx/access_log ltsv; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream myapp1 { server docker.for.mac.host.internal:30000; server docker.for.mac.host.internal:30001; } server { listen 80; server_name localhost; location / { # ここが重要 proxy_next_upstream error timeout http_502 http_503; proxy_pass http://myapp1; } } }
こんなコマンド打ってnginxを起動して
$ docker run --name my-custom-nginx-container -v /tmp/nginx.conf:/etc/nginx/nginx.conf:ro -p 80:80 -d nginx
$ curl localhost
こうするとこんなログが出て
2018/12/04 12:32:31 [warn] 6#6: *12 upstream server temporarily disabled while reading response header from upstream, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.65.2:30000/", host: "localhost"
もう一度curlするとこんなログが出て502が返る
2018/12/04 12:33:03 [error] 6#6: *18 no live upstreams while connecting to upstream, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://myapp1/", host: "localhost"
ドキュメント
Module ngx_http_upstream_module の max_fails
あたり。
By default, the number of unsuccessful attempts is set to 1.
What is considered an unsuccessful attempt is defined by the proxy_next_upstream,
なるほどねぇ。
next step
- docker composeでやったほうが再現が楽だしブログも書きやすいしいいことづくめ。次やる時はdocker compose使う