21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nginx版:mkcertを使ってローカル環境でもDockerでも楽々SSL

Last updated at Posted at 2019-07-29

背景

mkcertを使ってローカル環境でもDockerでも楽々SSLはApacheだったので、Nginx版です。

mkcertのインストール

mkcertのインストールはmkcertを使ってローカル環境でもDockerでも楽々SSLと一緒です。

brew install mkcert
mkcert -install
mkcert localhost 127.0.0.1

localhost+1.pem localhost+1-key.pemが生成される。

./cert-key/localhost.pem
./cert-key/localhost-key.pem
に置いておきましょう。
名称は何でもいいですが、
ここではlocalhost+1.pemからlocalhost.pem
localhost+1-key.pemからlocalhost-key.pem
に名称を変えています。

docker-compose.ymlの記述

docker-compose.yml
version: "3"
services: 
  nginx:
    build: ./
    ports:
      - 80:80
      - 443:443

Dockerfileの記述

FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf
ADD ./cert-key/localhost.pem /etc/certs/localhost.pem
ADD ./cert-key/localhost-key.pem /etc/certs/localhost-key.pem

nginx.confの記述

events{
}
http{
    server {
        listen 80 default;
        server_name localhost; 
    }
    server {
        listen 443 ssl;
        ssl_certificate /etc/certs/localhost.pem;
        ssl_certificate_key /etc/certs/localhost-key.pem; 
    }
}

eventsがないと
no "events" section in configurationとエラーがでます。

docker-compose の起動

docker-compose build
docker-compose up -d

でdocker-composeを起動する。

localhostにアクセスしてhttpsでも表示できれば成功。

スクリーンショット 2019-07-29 13.55.34.png
21
19
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
21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?