Ubuntu の Nginx に対して Let's Encrypt の certbot を設定するには

昔は SSL 証明書は購入することが必要でしたが、最近は Let’s Encrypt が普及したことで無料で https 対応できるようになりました。 ただこの証明書は有効期間が短いため、適切に設定しておかないとすぐに期限切れになってしまいます。 これは、 certbot を使って Ubuntu の Nginx の指定ドメインに対してセットアップするときの忘備録です。 certbot のリポジトリ追加とインストール $ sudo add-apt-repository ppa:certbot/certbot $ sudo apt-get update $ sudo apt-get install python-certbot-nginx 指定したドメインに certbot を設定する $ sudo certbot --nginx -d yourdomain.com 実行すると、下記のとおり出力されます。 途中訊かれるのは http のリクエストを https にリダイレクトするかどうかです。 Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Obtaining a new certificate Performing the following challenges: http-01 challenge for yourdomain.com Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/yourdomain.com Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/yourdomain.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://yourdomain.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=yourdomain.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/yourdomain.com/privkey.pem Your cert will expire on 2019-07-07. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le 外部からの SSL 設定の確認 上記の You should test your configuration at: にある通り、 ブラウザで https://www.ssllabs.com/ssltest/analyze.html?d=_yourdomain.com_ を開いて適切かどうか確認しましょう。 ...

2019年4月8日 · Toshimitsu Takahashi

Nginx で IE 8.0 以下には jQuery のバージョン 1 を返すようにする

jQuery バージョン 2 から IE 9 未満のサポートが切り捨てられて、実装的にもファイルサイズ的にも軽くなっています。 ただそうは言っても IE 8 以前のアクセスもあるため、サーバ側でブラウザに合わせて、jQuery を切り替えて返してしまおうという設定をしました。 server { (略) modern_browser unlisted; ancient_browser "MSIE 6.0" "MSIE 7.0" "MSIE 8.0"; location ~ ^/statics { root /var/www/html/statics; if ($ancient_browser) { set $oldie_jq O; } if ($request_filename ~* jquery\\.min\\.js) { set $oldie\_jq "${oldie\_jq}K"; } if ($oldie_jq = OK) { rewrite ^(.*)\\/jquery\\.min\\.js$ $1/jquery-1.min.js break; } break; } (略) } modern_browser と ancient_browser はそのままで Nginx としてモダンなブラウザか古いブラウザをセットすることができます。これによって古いブラウザに該当するかどうかを $ancient_browser を評価するだけで行えます。 続いてリクエストファイルが jquery.min.js かどうかを判定しています。if で AND が書けないためトリッキーな変数評価をしています。 ...

2014年9月22日 · Toshimitsu Takahashi

HTTPサーバのログ解析からアタックを検出する簡易ツールを作ってみた

Webサイトを運用しているとアタックや如何わしいアクセスを調べたいときがあります。簡単にHTTPサーバのアクセスログから短い間隔で大量にアクセスしているホストを、諸々のコマンドで操作するのは面倒なので、簡単に検出できるツールがあると便利です。 探してみたところ簡単に扱えそうなものがなかったので、Rubyベースで作ってみました。 Detect HTTP Attack https://github.com/tilfin/detect-http-attack Ruby 1.9 系で動きます。依存するライブラリはないので、直ぐにコマンドラインで実行できます。標準入出力で処理しているのシェルのパイプラインに組み合わせることができます。 準備 $ git clone https://github.com/tilfin/detect-http-attack.git $ cd detect-http-attack $ ./detect_http_attack.rb --help Usage: detect_http_attack [options] -ltsv Log type is LTSV -n notify when detecting attack -s COUNT Specify minimum sequential count -i SECONDS Specify maximum interval seconds -f CONFFILE Specify configuration file 対応するログフォーマット Apache, Nginx Combined Log (Common Logもそのまま無いフィールドが空になる) LTSV format (引数に -ltsv を付加) http://ltsv.org/ 使い方 引数で検出する最大のアクセス間隔(秒)と、その間隔で連続した回数を引数で渡すと、それに適合するアクセスを標準出力します。 例えば、2秒以内の間隔で5回以上連続でアクセスがあった場合を検出するときは次のようにします。(デフォルトは3秒以内の間隔で8回以上連続で検出) ./detect-http-attack.rb -s 2 -i 5 < /var/log/apache/access_log 10.128.192.255 7 Mozilla/3.0 (windows) 2012-12-20T08:25:28+09:00 200 /admin/phpmyadmin/scripts/setup.php - 2012-12-20T08:25:28+09:00 200 /wp-content/plugins/wp-phpmyadmin/phpmyadmin/scripts/setup.php - 2012-12-20T08:25:28+09:00 200 /mysql/scripts/setup.php - 2012-12-20T08:25:28+09:00 200 /phpmyadmin2/scripts/setup.php - 2012-12-20T08:25:28+09:00 200 /pma/scripts/setup.php - 2012-12-20T08:25:28+09:00 200 /phpmyadmin/scripts/setup.php - 2012-12-20T08:25:28+09:00 200 /myadmin/scripts/setup.php - 2012-12-20T08:25:28+09:00 200 /phpMyAdmin/scripts/setup.php - 通常は、ホストごとにブロックで出力しますが、-n フラグを付けると標準エラーに検出したアクセス毎に出力します。ターミナルで下記のように実行すれば、検出したアタックを適宜表示しつつ、まとめた結果をファイルに書き出すことができます。 ...

2013年2月12日 · Toshimitsu Takahashi

リバースプロキシの Nginx でバックエンドの X-Powered-By ヘッダを消すには

バックエンドサービスが何で動いてるのかどうしてわかるのか不思議に思っていたら、X-Powered-By という拡張 HTTP ヘッダにフレームワークの情報が入ってることに気付いた。 ということでセキュリティホールがあったときによろしくないので、消すことにした。 Nginx で消すにはこんな感じに定義する。 server { proxy_hide_header X-Powered-By; } 全体にかける。 server { location / { proxy_hide_header X-Powered-By; } } ロケーション別にかける。

2012年8月23日 · Toshimitsu Takahashi

Apache SSL だったものを Nginx をフロントにおいてリバースプロキシ化したときの設定

さくらの VPS (Ubuntu) で Apache に 80 (http), 443 (https) で Listen させていました。バーチャルホストでいくつかのドメインをこれで処理していましたが、フロントに Nginx を置いて必要な処理だけ Apache にすることにしました。 そこで SSL を Nginx で動かすにあたって、リバースプロキシ下でも Redmine など https 設定で動くように設定したときのメモです。 SSL証明書、秘密鍵の設定 Apache の /etc/apache2/ssl.crt/server.crt と ca-bundle.crt を Nginx の /etc/nginx/ssl/cert.pem に移す $ cd /etc/apache2/ssl.crt # cat server.crt ca-bundle.crt > /etc/nginx/ssl/cert.pem ※CA証明書を別にできないのでサーバ証明書に追記する。 Apache の /etc/apache2/ssl.key/server.key を Nginx の /etc/nginx/ssl/server.key にコピー $ cp /etc/apache2/ssl.key/server.key /etc/nginx/ssl/cert.key Nginx の設定 Apache は 8080 (http) ポートで動かす。Nginx の 443 (https) をそこにリバースプロキシする。 # HTTPS server # server { listen 443; server_name www.example.com; access_log off; ssl on; ssl_certificate ssl/cert.pem; ssl\_certificate\_key ssl/cert.key; ssl\_session\_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl\_prefer\_server_ciphers on; location / { proxy_redirect off; proxy\_set\_header Host $host; proxy\_set\_header X-Real-IP $remote_addr; proxy\_set\_header X-Forwarded-Proto https; proxy\_set\_header X-Forwarded-Host $host; proxy\_set\_header X-Forwarded-Server $host; proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for; proxy_pass http://127.0.0.1:8080/; } } proxy_set_header でいくつかのヘッダを Apache に渡すことで https 設定の Redmine などが動くようになる。

2011年12月31日 · Toshimitsu Takahashi