Docker Compose安装Nginx

使用Docker Compose快速搭建Nginx环境。

安装Docker Compose

确保安装Docker Compose,参考安装地址https://docs.docker.com/engine/install/

安装Nginx准备

创建安装目录

例如部署为/opt/nginx,如果不存在则创建

创建配置文件

创建配置文件夹/opt/nginx/conf

创建配置文件nginx.conf

对应文件目录:/opt/nginx/conf/nginx.confworker_processes修改工作线程数量

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
user  nginx;
worker_processes 8;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

server_tokens off; # 找到server_tokens并设置为off

keepalive_timeout 65;

gzip on;

gzip_buffers 4 16k;

gzip_comp_level 5; #推荐先设置为中间的值,比如4或者5

gzip_min_length 1024; #默认为20,建议设置位1KB,如果设置为0则表示压缩全部数据包


gzip_types text/plain application/x-javascript text/css application/xml; #一般情况下如此设置

gzip_vary on; #选择支持vary header;改选项可以让前端的缓存服务器缓存经过gzip压缩的页面; 这个可以不写,表示在传送数据时,给客户端说明我使用了gzip压缩

# proxy_cache_path /opt/nginx/proxy_cache levels=1:2:1 keys_zone=pcache:5m max_size=2g; ##定义缓存存放路径

include /etc/nginx/servers/*;
}

创建配置文件proxy.conf

对应文件目录:/opt/nginx/conf/proxy.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
#关闭代理重定向
proxy_redirect off;

#重新设定Header信息
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header Host $proxy_host;
proxy_set_header Host $host;
#proxy_set_header Accept-Encoding 'none';

#隐藏Header信息
#proxy_hide_header X-Cache;
#proxy_hide_header X-Powered-By;
#proxy_hide_header Last-Modified;
#proxy_hide_header Date;
#proxy_hide_header Content-Length;
#proxy_hide_header Content-Language;
#proxy_hide_header Cache-Control;

#通过Header信息
#proxy_pass_header Server;

#允许客户上传的最大数据
client_max_body_size 8m;

#代理连接超时(1分钟)
proxy_connect_timeout 1m;

#代理发送超时
proxy_send_timeout 3m;

#代理接收超时
proxy_read_timeout 3m;

#设定缓存文件夹大小
proxy_temp_file_write_size 1024m;

#缓冲区大小
proxy_buffer_size 32k;

#缓冲区设置
proxy_buffers 4 32k;

#高负荷下缓冲大小
proxy_busy_buffers_size 64k;

#不允许代理端主动关闭连接
proxy_ignore_client_abort on;

proxy_next_upstream error timeout invalid_header http_503;

创建静态资源目录html

对应文件目录:/opt/nginx/html

创建端口映射目录servers

对应文件目录:/opt/nginx/servers,所有的端口映射都在这个文件中

参考配置,文件名称为gitlab.demo.wueasy.com,文件名称建议使用域名命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {  
listen 80;
server_name gitlab.demo.wueasy.com;
charset utf-8;

#设置主访问日志
#access_log logs/access.log main;
access_log /dev/null;

error_page 404 /404.html;

#全部请求转发到后端服务器的设置
location /
{
proxy_pass http://172.16.85.194:8929;
client_max_body_size 200M;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
}

创建docker-compose.yml文件

在部署目录中创建文件/opt/nginx/docker-compose.yml,输入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: '3'
services:
nginx:
image: nginx
container_name: nginx
restart: always
environment:
- TZ=Asia/Shanghai
ports:
- 80:80
- 443:443
volumes:
- /etc/localtime:/etc/localtime:ro
- ./conf/nginx.conf:/etc/nginx/nginx.conf:ro
- ./conf/proxy.conf:/etc/nginx/proxy.conf:ro
- ./servers:/etc/nginx/servers
- ./log:/var/log/nginx
- ./html:/opt/html:ro

配置说明:

  • html目录:存储静态资源
  • servers目录:存储映射配置

启动服务

启动nginx容器并在后台运行

1
docker compose up -d

可以通过docker logs nginx查看日志。

停止服务

1
docker stop nginx

重启

1
docker restart nginx

停止并卸载服务

1
docker compose down