只要持之以恒,知识丰富了,终能发现其奥秘。——杨振宁

安装nginx

1
2
3
4
5
6
7
8
docker pull nginx
# 选择 docker.io/library/nginx:latest
# 创建nginx配置文件存放目录
mkdir -p /server/nginx
# 创建配置文件
touch /server/nginx/nginx.conf
# 编辑配置文件
vim /server/nginx/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
events{
worker_connections 1024;
}

http{
upstream ruben{
server xxx.xxx.xxx.xxx:8080 weight=1;
server xxx:xxx:xxx:xxx:8081 weight=1;
}

server{
listen 80;
server_name localhost;
location / {
proxy_pass http://ruben;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128;
}
location ~ /upload/img/ {
root /;
}
}
}

这里注意第一个location是代理到上面负载均衡的其他容器

第二个location是映射当前nginx容器内的静态资源

启动

1
2
3
4
5
6
docker run \
--name nginx80 \
-d -p 80:80 \
-v /server/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /upload/img/:/upload/img/ \
nginx

第一个-v是挂载配置文件

第二个-v是挂载静态资源