4
2

More than 3 years have passed since last update.

ローカル環境のDocker、nginxプロキシでSSLサーバ証明書を使う(mkcertでオレオレ証明書)

Posted at

ローカル環境でSSLを使ったテストをどうすれば良いか悩み、あれこれやって何とか形になったので、記事に残しておきます。

インストール

brew install mkcert
brew install nss # if yo use Firefox

ブラウザの証明書に登録する

mkcert -install

サーバ証明書と秘密鍵を発行する

※リバースプロキシで複数のサブドメインを使うため、ワイルドカードを使う

mkcert -cert-file localhost.crt -key-file localhost.key "*.dev.localhost" dev.localhost localhost 127.0.0.1

/etc/hosts にドメインを追加する

 ・
 ・
 ・
127.0.0.1 dev.localhost

nginxのプロキシサーバーを作る(docker)

※./certsフォルダに作った証明書と秘密鍵を入れておく
※dockerのnetworkを作っておく

version: '3'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: 'nginx-proxy'
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./certs:/etc/nginx/certs

networks:
  default:
    external:
      name: proxy-network

web1サーバーを作る

web1 HTMLファイル

※publicフォルダに入れておく

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>web1</title>
  </head>
  <body>
      <h1>
        web1
      </h1>
  </body>
</html>

web1 dockerサーバーを作る

version: '3'
services:
  web1:
    container_name: 'web1'
    image: nginx
    volumes:
      - ./public:/usr/share/nginx/html
    environment:
      - VIRTUAL_HOST=web1.dev.localhost
      - VIRTUAL_PORT=80

networks:
  default:
    external:
      name: proxy-network

web2サーバーを作る

web2 HTMLファイル

※publicフォルダに入れておく

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>web2</title>
  </head>
  <body>
      <h1>
        web2
      </h1>
  </body>
</html>

web2 dockerサーバーを作る

version: '3'
services:
  web2:
    container_name: 'web2'
    image: nginx
    volumes:
      - ./public:/usr/share/nginx/html
    environment:
      - VIRTUAL_HOST=web2.dev.localhost
      - VIRTUAL_PORT=80

networks:
  default:
    external:
      name: proxy-network

その他のdockerサーバーをプロキシ配下に入れる

VIRTUAL_HOST、VIRTUAL_PORT、networksを追加すれば入れることができる

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2