Nginx版:使用mkcert在本地环境和Docker中轻松实现SSL
背景
使用mkcert可以轻松地在本地环境和Docker中设置SSL,原先是针对Apache的,现在转为针对Nginx。
安装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文件的描述
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 方式显示则算成功。
