Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器.
Nginx 在开放源码(BSD like) 协议发型, 国内知名厂商都使用其进行作为Web server.
Nginx作为cf的负载均衡器
我们需要一份较为精简的配置文件来顺利启动Nginx. nginx使用upstream创建一个名为负载均衡配置.
然后将所有后端service地址与端口写入进去. 并将其命名为nginx.conf并保存到当前文件夹下.
还需要在配置文件中指定cf容器的name为host, 同时将其link到WebProxy容器中来. 这样nginx才能顺利访问到它.
具体示例如下所示:
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 65535;
}
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" "req_time:[$request_time/Sec]" "upstream_time:[$upstream_response_time/Sec]" ';
access_log /var/log/nginx/access.log main;
#优化文件发送
sendfile on;
#tcp_nopush on;
#关闭Nagle算法
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_vary on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_disable "MSIE [1-6]\.";
upstream myweb {
server webapp1:8080;
server webapp2:8080;
server webapp3:8080;
}
server {
listen 80;
#access_log /var/log/nginx/8080.log main;
location /ws {
proxy_pass http://myweb/ws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
proxy_pass http://myweb;
proxy_http_version 1.1;
proxy_ignore_client_abort on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
nginx配置文件准备好后, 我们开始编写docker-compose的编排文件.
# docker-compose.yaml
# docker-compose.yaml
version: "2"
services:
WebProxy:
image: nginx:latest
ports:
- 80:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
networks:
- local
links:
- WebApp1:webapp1
- WebApp2:webapp2
- WebApp3:webapp3
WebApp1:
image: candymi/cfweb:latest
volumes:
- ./script:/app/script
networks:
- local
WebApp2:
image: candymi/cfweb:latest
volumes:
- ./script:/app/script
networks:
- local
WebApp3:
image: candymi/cfweb:latest
volumes:
- ./script:/app/script
networks:
- local
networks:
local:
driver: bridge
我们将nginx.conf文件直接挂在到nginx的替换nginx镜像的默认配置文件. 这样在启动的时候.nginx将会使用我们编写的配置文件进行启动.
然后links下降WebApp链接到nginx容器内, 这样就实现了nginx与cfweb的host互通.
现在我们使用这份配置文件进行启动:
[candy@MacBookPro:~/Documents/core_framework/docker] $ docker-compose -f docker-compose-with-nginx.yaml up
Starting docker_WebApp2_1 ... done
Starting docker_WebApp1_1 ... done
Starting docker_WebApp3_1 ... done
Starting docker_WebProxy_1 ... done
Attaching to docker_WebApp2_1, docker_WebApp1_1, docker_WebApp3_1, docker_WebProxy_1
上述配置, 我们创建了一个3个cfweb service容器. 使用upstream进行反向代理将不同的请求分发到不同的cfweb容器内部.
测试nginx与cfweb
如果启动没有任何问题后, 我们可以尝试使用curl进行测试.
WebApp1_1 | [2019/04/26 11:46:06] - 192.168.32.1 - 192.168.32.1 - /api/login - GET - 200 - req_time: 0.000249/Sec
WebProxy_1 | 192.168.32.1 - - [26/Apr/2019:11:46:06 +0000] "GET /api/login HTTP/1.1" 200 46 "-" "curl/7.54.0" "req_time:[0.001/Sec]" "upstream_time:[0.000/Sec]"
WebApp2_1 | [2019/04/26 11:46:07] - 192.168.32.1 - 192.168.32.1 - /api/login - GET - 200 - req_time: 0.000230/Sec
WebProxy_1 | 192.168.32.1 - - [26/Apr/2019:11:46:07 +0000] "GET /api/login HTTP/1.1" 200 46 "-" "curl/7.54.0" "req_time:[0.001/Sec]" "upstream_time:[0.000/Sec]"
WebProxy_1 | 192.168.32.1 - - [26/Apr/2019:11:46:08 +0000] "GET /api/login HTTP/1.1" 200 46 "-" "curl/7.54.0" "req_time:[0.002/Sec]" "upstream_time:[0.000/Sec]"
WebApp3_1 | [2019/04/26 11:46:08] - 192.168.32.1 - 192.168.32.1 - /api/login - GET - 200 - req_time: 0.000318/Sec
可以看到nginx与cf容器都打印出了请求日志, 说明我们的容器已经开始正确运行.