docker-compose 配置 Nginx 部署静态页面并开启 https 请求
docker-compose 配置
version: '2.0'
services:
services:
image: nginx
container_name: nginx-ssl
restart: always
ports:
- 6443:6443
- 7777:80
volumes:
- ./conf/log:/etc/nginx/logs
- ./conf/www:/etc/nginx/html
# ssl 文件夹中放置nginx服务对应的证书文件
- ./conf/ssl:/cert
- /etc/letsencrypt:/etc/letsencrypt
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- /etc/localtime:/etc/localtime:ro #可与宿主机共享时区
- /etc/timezone:/etc/timezone:ro #作用同上
在部署目录创建相关文件夹:
mkdir -p conf/log conf/www conf/ssl
将页面放至 conf/www 文件夹中。
编辑 nginx.conf 配置文件内容
#user nobody;
worker_processes 8;
worker_rlimit_nofile 12000;
events {
use epoll;
worker_connections 12000;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
# https 监听端口,https端口不可使用docker端口转发,宿主机与容器内端口需配置一致,否则无法访问
listen 6443 ssl;
server_name localhost;
root /etc/nginx/html;
# 以下为证书文件
ssl_certificate /cert/4008915_drpeckerviewer.jianpeicn.com.pem;
ssl_certificate_key /cert/4008915_drpeckerviewer.jianpeicn.com.key;
ssl_password_file /cert/ssl.pass;
ssl_session_timeout 5m;
access_log logs/access.log;
#开启索引功能
autoindex on;
# 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_exact_size off;
autoindex_localtime on; # 显示本机时间而非 GMT 时间
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 解决跨域问题
add_header Access-Control-Allow-Origin '*';
}
}
启动服务
- 启动docker-compose
# 以后台运行方式启动服务
docker-compose up -d
# 查看服务状态
docker-compose ps
# 查看结果可直接访问部署的网站页面
查看日志
# 实时查看 nginx 日志
tail -f conf/log/access.log
# 查看 nginx 日志最近的200行
tail -n200 conf/log/access.log
# docker-compose 实时查看日志
docker-compose logs -f
# 查看docker-compose 近200行日志
docker-compose logs --tail=200
# 查看docker-compose 近200行日志后实时显示日志
docker-compose logs -f --tail=200
# 查看docker服务所占资源情况
docker stats