得到任务要完善公司的ngix服务。加入自定义的错误页面。然后就在自己的机器的docker里装上了2个ngix的容器。一个扮演proxy,一个扮演backend app server.
第一步 为2台服务器准备2个内部的ip。然后写个简单的 docker-composer.yaml让2个ngix都能自己跑起来。里面有个小插曲,就是当nginx配置错误,或导致docker启动错误,就自己关闭了。在最后加上一个tty:true可以让进程不挂掉。
本地的host文件加入
192.168.88.18 xxxxxxx.vm
192.168.88.19 app.xxxxxxx.vm
proxy的docker-composer 这样写
version: "3.5"
services:
# nginx server
app:
container_name: xxxxxxx-master
restart: "no"
image: nginx:1.17
volumes:
- /home/hchen/private/xxxxxxx/nginx:/var/www/html:delegated
ports:
- "8080:80"
expose:
- "80"
networks:
dh_net:
ipv4_address: 192.168.88.18
extra_hosts:
- "echobot.vm:192.168.88.18"
tty: true
networks:
dh_net:
name: dh_network
ipam:
driver: default
config:
- subnet: 192.168.88.0/24
然后app的类似,这样写
version: "3.5"
services:
# nginx server
app:
container_name: xxxxxx-app
restart: "no"
image: nginx:1.17
volumes:
- /home/hchen/private/xxxxxx/apps:/var/www/html:delegated
ports:
- "8081:80"
networks:
dh_net:
ipv4_address: 192.168.88.19
extra_hosts:
- "xxxxxxx.vm:192.168.88.18"
- "app.xxxxxxx.vm:192.168.88.19"
networks:
dh_net:
name: dh_network
ipam:
driver: default
config:
- subnet: 192.168.88.0/24
写完以后,跑起来,分别可以打开这2个站点。
第二步,把proxy改为反向代理。修改default.conf
upstream app {
server 192.168.88.19;
}
server {
listen 80;
server_name xxxxxxx.vm;
location / {
proxy_pass http://app;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
主要就是i加上upstream 名称 ,去掉本地 /的解析,改为proxy_pass http://名称。然后reload.
这样一台简单的反向代理就搭建好了。