docker-compose 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 #作用同上

在部署目录创建相关文件夹:

1
mkdir -p conf/log conf/www conf/ssl

将页面放至 conf/www 文件夹中。


编辑 nginx.conf 配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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/bright.com.pem;
ssl_certificate_key /cert/bright.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
    1
    2
    3
    4
    5
    6
    # 以后台运行方式启动服务
    docker-compose up -d
    # 查看服务状态
    docker-compose ps
    # 查看结果可直接访问部署的网站页面

    查看日志

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 实时查看 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