メインコンテンツへ移動

nginx HTTPS 逆方向プロキシ

無料2017-04-23#Node#Solution#免费HTTPS证书推荐#nginx添加https模块#nginx反向代理https#nginx node https#nodejs https

無料の緑色の鍵を獲得しませんか?

一.環境

Centos:

$ cat /etc/redhat-releas
CentOS Linux release 7.3.1611 (Core)

必須ではありません。設定方法は同じで、nginx のコンパイルインストールプロセスも同じです。慣れているものを使えばよいです

nginx:

$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module

configure argumentswith-http_ssl_module必須です。./configure 時にこのオプションを付けて SSL モジュールを有効にする必要があります。そうしないと面倒なことになります(phpize のようなものがないため、再コンパイルして必要に応じて上書きするしかありません)

無料 SSL 証明書:

https://letsencrypt.org/getting-started/

Let's Encrypt は比較的信頼性が高く、非営利組織で HTTPS 化を推進することを目的としています。そのため、世界平和に貢献しましょう(主に無料だからですが〜)

P.S. 証明書の有効期限は 3 ヶ月で、期限が切れたら更新する必要があります。ツールで自動的に更新でき、それほど面倒ではありません

node:

$ nvm ls
        v6.10.0
->       v7.9.0
default -> stable (-> v7.9.0)
node -> stable (-> v7.9.0) (default)
stable -> 7.9 (-> v7.9.0) (default)

必須ではありません。nginx の設定後に逆方向プロキシが正常に動作するかを確認するために使用します。HTTP サーバーを起動できる他の方法でも何でも構いません

二.無料 SSL 証明書の申請

https://letsencrypt.org/getting-started/ にアクセスし、手順に従ってください。With Shell Access 方式を推奨します。certbot というコマンドラインツールを提供しており、対話式の設定で、直感的な操作が可能です

また、コマンドラインツールを使用する利点は、証明書の期限が切れた場合に自動的に更新でき、1 つのコマンドを実行するだけで済むことです。これを cron タスクに追加すれば、干渉されずに済みます。さらに毎日自動的に実行することもでき、公式もこれを推奨しています。これはハートビートとして機能し、生存を示し、スクリーニングによる取り消しを回避できます:

Note:

if you're setting up a cron or systemd job, we recommend running it twice per day (it won't do anything until your certificates are due for renewal or revoked, but running it regularly would give your site a chance of staying online in case a Let's Encrypt-initiated revocation happened for some reason). Please select a random minute within the hour for your renewal tasks.

certbot の手順に従ってください。例えばこの記事の環境での手順 は以下の通りです:

# yum リポジトリを追加
yum -y install yum-utils
yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
# certbot をインストール
sudo yum install certbot
# 証明書のみ取得、自動設定はしない
certbot certonly

すべて正常であれば、以下のようなメッセージが表示されます:

Generating key (2048 bits): /etc/letsencrypt/keys/0000_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0000_csr-certbot.pem

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/<mydomain>/fullchain.pem. Your cert will
   expire on 2017-07-17. To obtain a new or tweaked version of this
   certificate in the future, simply run certbot again. To
   non-interactively renew *all* of your certificates, run "certbot
   renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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

証明書と秘密鍵は /etc/letsencrypt/live/<mydomain>/ に保存されます:

$ ls /etc/letsencrypt/live/<mydomain>/
cert.pem  chain.pem  fullchain.pem  privkey.pem  README

cert.pem証明書(サーバー証明書)、privkey.pem秘密鍵です。この 2 つがあれば十分です。また、chain.pem はルート証明書と中間証明書など、サーバー証明書以外の認証プロセスに必要な証明書チェーンです。fullchain.pem は完全な証明書チェーンで、cert.pemchain.pem の内容を含みます

最後に自動更新を有効にします:

# 更新テスト
certbot renew --dry-run

すべて正常であれば、cron タスクを追加します:

crontab -e
# 毎日 1:11 に証明書を更新
1 1 * * * certbot renew >> ~/cron/cert.log --renew-hook "/usr/local/nginx/sbin/nginx -s reload"
# すべてのタスクを表示
crontab -l

注意: 証明書を更新した後、nginx を自動的に再起動するために --renew-hook を必ず追加してください。そうしないと期限切れの証明書を使い続けます。証明書は更新されますが、反映されません

三.nginx HTTPS 逆方向プロキシの設定

SSL モジュールを確認します:

$ /usr/local/nginx/sbin/nginx -V

configure argumentswith-http_ssl_module があれば、SSL モジュールが既に存在することを示します。そうでない場合は、パラメータを追加して再コンパイルし、既存の nginx 実行ファイルを上書きする必要があります。具体的な手順については、nginx 再コンパイルで SSL モジュールを追加 を参照してください

設定ファイルを修正します:

# バックアップ
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_
# 編集
vi /usr/local/nginx/conf/nginx.conf
# HTTPS server 以下の内容を変更
server {
    listen       443 ssl;
    server_name  localhost;

    ssl_certificate      /etc/letsencrypt/live/<mydomain>/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/<mydomain>/privkey.pem;

    ssl_session_timeout  5m;

    location / {
        proxy_pass http://localhost:7777;
    }
}
# 設定構文をテスト
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# nginx を再起動
/usr/local/nginx/sbin/nginx -s reload

すべて正常であれば、これでサイト全体の HTTPS リクエストが 7777 ポートに転送されるように設定されました

P.S. 特定のパスの転送を設定する必要がある場合は、location のパスマッチングルールを修正すればよいです。詳細は Nginx 設定の概要 を参照してください

四.HTTP サーバーを起動してプロキシが機能することを確認

HTTPS 接続は nginx が管理するため、背後の HTTP サービスはほとんど変更を加える必要がありません。HTTP リクエストを受け取るためですが、nginx によって転送されたものです(ただし、cookie などは転送後に問題が発生する可能性があり、proxy_cookie_path を設定する必要があります。後日詳しく説明します)

const http = require('http');

const PORT = 7777;

http.createServer((req, res) => {
    res.end('hoho, ' + PORT);
}).listen(PORT);
console.log('server is ready, listening ' + PORT);

https:<mydomain>/ にアクセスして hoho, 7777 が表示されれば、プロキシが機能していることがわかります

アドレスバーに美しい緑色の鍵が表示されます。また、通常は DNS 解決ルールを設定し、HTTP リクエストを HTTPS に強制リダイレクトするか、より適切な HSTS 方式などを設定する必要がありますが、それは後の話です

コメント

コメントはまだありません

コメントを書く