$30 off During Our Annual Pro Sale. View Details »

Webサーバが死んだので503だけを返す(Lighttpd/Apache httpd)

Webサーバが死んだので503だけを返す(Lighttpd/Apache httpd)

Kenichiro MATOHARA

July 09, 2022
Tweet

More Decks by Kenichiro MATOHARA

Other Decks in Technology

Transcript

  1. Webサーバが死ん
    Webサーバが死ん
    だので503だけを
    だので503だけを
    返す
    返す
    (Lighttpd/Apache
    (Lighttpd/Apache
    httpd)
    httpd)
    1 / 27

    View Slide

  2. 南隅から参加(鹿児島の右下)
    好きなLinuxディストリビューションはDebian
    お仕事募集 mailto:work@matohara.org
    Kenichiro Matohara(matoken)
    Kenichiro Matohara(matoken)
    https://matoken.org
    https://matoken.org
    2 / 27

    View Slide

  3. 最近
    最近
    自宅サーバも死ぬ
    *
    Facebook復旧
    https://www.facebookwkhpilnemxj7asaniu7vnjjbiltxjqh
    3 / 27

    View Slide

  4. 今月壊れたもの
    今月壊れたもの
    HP ProLiant MicroServer(初代)
    NASや自宅サーバとして利用していた
    Unboundも立てていたので名前解決ができなくなったりして困
    った
    たぶん以前からS.M.A.R.T.から警告が出ていた / のdiskが死ん

    ディスプレイとディスクが必要
    4 / 27

    View Slide

  5. 外部向けWebサーバ
    外部向けWebサーバ
    外向けにもいくつかのサービスが動いていた
    復旧目処がたっていない
    SBCのRaspberry Piでhttpdを起動してhttpステータスコード
    503を返したい.
    5 / 27

    View Slide

  6. HTTPステータスコード
    HTTPステータスコード
    通常は
    200 OK
    だけど目にすることはない.
    404 Not Found
    など
    はよく見かけるのでは?
    今回は
    503 Service Unavailable
    にしてみた.
    503 といっしょに
    Retry-After
    で復旧予定時間も出すといい
    のだけど未定なので無しで

    HTTP レスポンスステータスコード - HTTP | MDN
    503 Service Unavailable - HTTP | MDN
    RFC 7231 - Hypertext Transfer Protocol
    (HTTP/1.1): Semantics and Content
    6 / 27

    View Slide

  7. Raspberry Piの状況
    Raspberry Piの状況
    Raspberry Pi 3 model B
    Raspberry Pi OS bullseye armhf導入
    micro httpd起動中
    7 / 27

    View Slide

  8. httpdを変更
    httpdを変更
    micro httpdでの方法がよくわからない(VirtualDomain/すべて
    503等)のでmicro httpdは止めて,Lighttpdに変更
    $ sudo systemctl stop micro-httpd

    $ sudo systemctl disable micro-httpd

    $ sudo apt install lighttpd lighttpd-mod-openssl
    8 / 27

    View Slide

  9. SSL証明書の入手
    SSL証明書の入手
    認証局には以前も利用していたLet’s Encryptを利用,専用の証明書
    管理ツールのcertbotを導入して利用
    $ sudo apt install certbot

    $ sudo certbot certonly -d sub1.example.org

    $ sudo certbot certonly -d sub2.example.org

    $ sudo certbot certonly -d sub3.example.org

    :
    9 / 27

    View Slide

  10. Lighttpdのhttps設定
    Lighttpdのhttps設定
    lighttpd-enable-mod コマンドでLighttpdのsslモジュールと
    rewriteモジュールを有効に
    $ sudo lighttpd-enable-mod rewrite ssl
    10 / 27

    View Slide

  11. Lighttpdでのhttpsの設定
    Lighttpdでのhttpsの設定
    lighttpd/conf-enabled/10-ssl.conf を編集して証明書ファイル
    指定
    2つ目以降ののドメインを設定
    Lighttpdを再読込して動作を確認
    ssl.pemfile = "/etc/letsencrypt/live/sub1.example.org/fullchain.pem"

    ssl.privkey = "/etc/letsencrypt/live/sub1.example.org/privkey.pem"
    $HTTP["host"] =~ "sub2.example.org"{

    ssl.pemfile = "/etc/letsencrypt/live/sub2.example.org/fullchain.pem"

    ssl.privkey = "/etc/letsencrypt/live/sub2.example.org/privkey.pem"

    }

    $HTTP["host"] =~ "sub3.example.org"{

    ssl.pemfile = "/etc/letsencrypt/live/sub3.example.org/fullchain.pem"

    ssl.privkey = "/etc/letsencrypt/live/sub3.example.org/privkey.pem"

    }

    :
    $ sudo systemctl lighttpd restart

    $ w3m https://sub1.example.org/

    $ w3m https://sub2.example.org/
    11 / 27

    View Slide

  12. HTTPステータスコード503を返
    HTTPステータスコード503を返


    HTTPステータスコード503を返すだけのcgi scriptを作成していつも
    それを返すように
    12 / 27

    View Slide

  13. cgiを有効に
    cgiを有効に
    lighttpd-enable-mod コマンドでLighttpdのcgiモジュールを有効

    /etc/lighttpd/conf-enabled/10-cgi.conf を以下のように修正
    Lighttpdを再読込して動作を確認
    $ sudo lighttpd-enable-mod cgi
    $ grep -v ^# lighttpd/conf-enabled/10-cgi.conf | uniq

    server.modules += ( "mod_cgi" )

    cgi.assign = (

    ".cgi" => "/usr/bin/perl",

    )
    $ sudo systemctl lighttpd restart

    $ echo "#!/usr/bin/perl

    print 'Content-Type: text/plain\n\nhello world.'" > /var/www/html/hello.cgi

    $ sudo chown www-data.www-data /var/www/html/hello.cgi

    $ sudo chmod 700 /var/www/html/hello.cgi

    $ w3m https://sub1.example.org/hello.cgi

    $ sudo rm /var/www/html/hello.cgi
    13 / 27

    View Slide

  14. 503を返すcgiを用意
    503を返すcgiを用意
    HTTPステータスコード503を返す /var/www/html/503.cgi を用

    #!/usr/bin/perl

    use strict;

    use warnings;

    print "Status: 503 Service Unavailable\n";

    print "Content-Type: text/html\n\n";

    print <








    home.matoken.org





    Server is down.





    END_OF_HTML
    14 / 27

    View Slide

  15. cgi scriptに権限を設定して動作確
    cgi scriptに権限を設定して動作確


    $ sudo chown www-data.www-data /var/www/html/503.cgi

    $ sudo chmod 700 /var/www/html/503.cgi

    $ w3m -dump_head https://sub1.example.org/503.cgi | grep ^HTTP

    HTTP/1.0 503 Service Unavailable

    $ w3m https://sub1.example.org/503.cgi
    15 / 27

    View Slide

  16. Lighttpdですべてのアクセスに
    Lighttpdですべてのアクセスに
    503を返すようにする
    503を返すようにする
    /etc/lighttpd/lighttpd.conf ファイルに以下を追記
    Lighttpdを再読込して動作を確認
    $HTTP["url"] != "/503.cgi$" {

    url.rewrite = ( "" => "/503.cgi" )

    }
    $ sudo systemctl lighttpd restart

    $ w3m https://sub1.example.org/hoge

    $ w3m https://sub2.example.org/fuga

    $ w3m http://sub1.example.org/hoge

    $ w3m http://sub2.example.org/fuga
    16 / 27

    View Slide

  17. ログ確認
    ログ確認
    /var/log/lighttpd/
    を見ると
    error.log
    はあるけど
    access.log
    がない?
    lighttpd-enable-mod コマンドでLighttpdのaccesslogモジュー
    ルを有効に
    $ sudo lighttpd-enable-mod accesslog

    $ sudo systemctl lighttpd restart
    17 / 27

    View Slide

  18. とりあえずok
    とりあえずok
    案外アクセスある(9000/day)
    ほぼクロウラー
    503を返してるのでリトライしてる?
    復旧見込み時間を長めに設定して返すようにすると減るかも?
    18 / 27

    View Slide

  19. 自宅回線が遅く
    自宅回線が遅く
    もしかしてアクセスが多いせい?
    DNSを書き換えてアクセスの多いドメインを外のサーバに回してみる
    いつもはDown 8Mbps/Up 0.8Mbps程
    $ speedtest-cli --simple

    Ping: 639.861 ms

    Download: 0.44 Mbit/s

    Upload: 0.77 Mbit/s
    19 / 27

    View Slide

  20. 外のサーバ
    外のサーバ
    Debian bullseye amd64
    Apache httpd 2.4.38
    20 / 27

    View Slide

  21. 対象ドメインのSSL証明書取得
    対象ドメインのSSL証明書取得

    cpしてきてもいいと思う
    $ sudo certbot certonly -d sub1.example.org
    21 / 27

    View Slide

  22. 503用のhtmlを用意
    503用のhtmlを用意
    $ sudo -u www-data mkdir /var/www/sub1.example.org

    $ sudo -u www-data vi /var/www/sub1.example.org/503.html
    22 / 27

    View Slide

  23. 対象ドメインの設定を作成
    対象ドメインの設定を作成
    `/etc/apache2/sites-enabled/090-
    sub1.example.org.conf
    apache httpd再起動
    1
    すべてを503として
    /503.html
    にリダイレクト


    ServerName sub1.example.org

    Redirect permanent / https://sub1.example.org/







    ServerName sub1.example.org

    ServerAdmin [email protected]

    DocumentRoot /var/www/sub1.example.org/

    RedirectMatch 503 ^/(?!503\.html)

    ErrorDocument 503 /503.html

    ErrorLog ${APACHE_LOG_DIR}/error_sub1.example.org.log

    CustomLog ${APACHE_LOG_DIR}/access_sub1.example.org.log combined

    SSLCertificateFile /etc/letsencrypt/live/sub1.example.org/fullchain.pem

    SSLCertificateKeyFile /etc/letsencrypt/live/sub1.example.org/privkey.pem




    1
    $ sudo service apache2 restart
    23 / 27

    View Slide

  24. DNS書き換えを行い動作確認
    DNS書き換えを行い動作確認
    DNS書き換え前にhosts書き換えでテスト
    $ echo "192.0.2.5 sub1.example.org" | sudo tee -a /etc/hosts

    $ w3m -dump_head head http://sub1.example.org/piyo | grep ^HTTP/

    HTTP/1.1 503 Service Unavailable

    $ w3m -dump_extra head http://sub1.example.org/piyo | lv
    24 / 27

    View Slide

  25. 余録)cowsay
    余録)cowsay
    Lighttpdはcgiなのでなんとなくcowsayに喋ってもらうようにした
    (偶にecho-sd).しかしcgiから呼ぶと横幅がうまく取れなくなるよう
    (未解決)
    25 / 27

    View Slide

  26. まとめ
    まとめ
    自宅サーバが起動しなくなったのでRaspberry Piで503を返すよ
    うにした.
    Lighttpdはrewrite+cgi
    Apache httpdはrewrite+html
    自宅回線が遅くなったのは?
    復旧のためにアルバイト探し中……
    26 / 27

    View Slide

  27. 奥付
    奥付
    発表
    未発表
    発表者
    利用ソフトウェア
    ライセンス
    小江戸らぐ 7月のオフな集まり(第240回)
    Kenichiro Matohara(matoken)
    Asciidoctor Reveal.js
    CC BY 4.0
    27 / 27

    View Slide