docker部署nginx并配置https

1.准备SSL证书:

生成私钥:运行以下命令生成一个私钥文件。

生成证书请求(CSR):运行以下命令生成证书请求文件。

生成自签名证书:使用以下命令生成自签名证书。

复制代码
openssl genrsa -out example.com.key 2048
openssl req -new -key example.com.key -out example.com.csr
openssl x509 -req -days 365 -in example.com.csr -signkey example.com.key -out example.com.crt

2.创建Nginx配置文件

创建一个名为nginx.conf的Nginx配置文件。

复制代码
  server {
      listen 80;
      server_name zsh.com;
      return 301 https://$server_name$request_uri;
  }

  server {
      listen 443 ssl;
      server_name zsh.com;

      ssl_certificate /etc/nginx/certs/example.com.crt;
      ssl_certificate_key /etc/nginx/certs/example.com.key;

      location / {
        root /var/www/html; 
        index index.html;     
      }
  }

3.创建一个Dockerfile:

创建一个Dockerfile来构建你的Nginx容器。

复制代码
FROM nginx:latest

# 复制SSL证书和私钥到容器中
COPY example.com.crt /etc/nginx/certs/
COPY example.com.key /etc/nginx/certs/

# 复制Nginx配置文件到容器中
COPY nginx.conf /etc/nginx/nginx.conf

# 暴露80和443端口
EXPOSE 80
EXPOSE 443

# 启动Nginx服务
CMD ["nginx", "-g", "daemon off;"]

4.构建Docker镜像:

在Dockerfile所在的目录中执行以下命令构建镜像。

复制代码
docker build -t nginx-https .

5.运行容器:

运行你的Nginx容器,并将端口映射到主机上。

复制代码
docker run -d -p 80:80 -p 443:443 nginx-https
相关推荐
Johny_Zhao6 小时前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
FungLeo11 小时前
FengCMS·PHP+MySQL 升级实录 ·本地容器环境设计:只读挂载、LOCAL_UID、数据卷权限
开发语言·mysql·docker·php·fengcms
Csvn15 小时前
Nginx 可观测性与工程化:指标、告警与发布回滚演练
后端·nginx
cpolar技术支持1 天前
用一份 YAML 画出家里的网络拓扑:哪个口通公网、哪几个容器藏在内网,一张图摊开,再用 cpolar 在外网随时翻这份图
docker·网络拓扑·内网穿透·cpolar·yaml
Csvn2 天前
Nginx 高可用网关:VIP 切换、proxy_protocol 与 SNI 分流实战
后端·nginx
wno7042 天前
Nginx代理MySQL或PG连接
运维·mysql·nginx
FriendshipT2 天前
YOLOE Docker 部署实战:三模式推理、掩码输出与跨图视觉的完整实现
运维·人工智能·python·深度学习·docker·容器
the局外人2 天前
Docker 部署:从镜像构建到容器上线
后端·docker·容器
方便面不加香菜2 天前
Docker Compose 文件 -- docker-compose.yml
spring cloud·docker·容器
Csvn2 天前
Nginx 性能调优与压测:连接账本、找拐点与调优前后对比
后端·nginx