Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. 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
  2. Raspberry Piの状況 Raspberry Piの状況 Raspberry Pi 3 model B Raspberry

    Pi OS bullseye armhf導入 micro httpd起動中 7 / 27
  3. 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
  4. 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
  5. 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
  6. 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 <<END_OF_HTML; <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> <head> <meta charset="utf-8" /> <title>home.matoken.org</title> </head> <body> <p>Server is down.</p> </body> </html> END_OF_HTML 14 / 27
  7. 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
  8. 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
  9. 対象ドメインの設定を作成 対象ドメインの設定を作成 `/etc/apache2/sites-enabled/090- sub1.example.org.conf apache httpd再起動 1 すべてを503として /503.html にリダイレクト

    <VirtualHost *:80> ServerName sub1.example.org Redirect permanent / https://sub1.example.org/ </VirtualHost> <IfModule mod_ssl.c> <VirtualHost *:443> 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 </VirtualHost> </IfModule> 1 $ sudo service apache2 restart 23 / 27
  10. 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