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才能顺利访问到它.

具体示例如下所示:

  1. worker_processes 1;
  2.  
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5.  
  6.  
  7. events {
  8. worker_connections 65535;
  9. }
  10.  
  11.  
  12. http {
  13. include /etc/nginx/mime.types;
  14. default_type application/octet-stream;
  15.  
  16. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  17. '$status $body_bytes_sent "$http_referer" '
  18. '"$http_user_agent" "req_time:[$request_time/Sec]" "upstream_time:[$upstream_response_time/Sec]" ';
  19.  
  20. access_log /var/log/nginx/access.log main;
  21.  
  22. #优化文件发送
  23. sendfile on;
  24. #tcp_nopush on;
  25. #关闭Nagle算法
  26. tcp_nodelay on;
  27.  
  28. keepalive_timeout 65;
  29.  
  30. gzip on;
  31. gzip_vary on;
  32. gzip_min_length 1k;
  33. gzip_comp_level 9;
  34. 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;
  35. gzip_disable "MSIE [1-6]\.";
  36.  
  37.  
  38. upstream myweb {
  39. server webapp1:8080;
  40. server webapp2:8080;
  41. server webapp3:8080;
  42. }
  43.  
  44. server {
  45. listen 80;
  46. #access_log /var/log/nginx/8080.log main;
  47.  
  48. location /ws {
  49. proxy_pass http://myweb/ws;
  50. proxy_http_version 1.1;
  51. proxy_set_header Upgrade $http_upgrade;
  52. proxy_set_header Connection "upgrade";
  53. }
  54.  
  55. location / {
  56. proxy_pass http://myweb;
  57. proxy_http_version 1.1;
  58. proxy_ignore_client_abort on;
  59. proxy_set_header Host $http_host;
  60. proxy_set_header X-Real-IP $remote_addr;
  61. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  62. }
  63.  
  64. }
  65.  
  66. }

nginx配置文件准备好后, 我们开始编写docker-compose的编排文件.

  1. # docker-compose.yaml
  2. # docker-compose.yaml
  3. version: "2"
  4. services:
  5. WebProxy:
  6. image: nginx:latest
  7. ports:
  8. - 80:80
  9. volumes:
  10. - ./nginx.conf:/etc/nginx/nginx.conf
  11. networks:
  12. - local
  13. links:
  14. - WebApp1:webapp1
  15. - WebApp2:webapp2
  16. - WebApp3:webapp3
  17. WebApp1:
  18. image: candymi/cfweb:latest
  19. volumes:
  20. - ./script:/app/script
  21. networks:
  22. - local
  23. WebApp2:
  24. image: candymi/cfweb:latest
  25. volumes:
  26. - ./script:/app/script
  27. networks:
  28. - local
  29. WebApp3:
  30. image: candymi/cfweb:latest
  31. volumes:
  32. - ./script:/app/script
  33. networks:
  34. - local
  35.  
  36. networks:
  37. local:
  38. driver: bridge

我们将nginx.conf文件直接挂在到nginx的替换nginx镜像的默认配置文件. 这样在启动的时候.nginx将会使用我们编写的配置文件进行启动.

然后links下降WebApp链接到nginx容器内, 这样就实现了nginx与cfweb的host互通.

现在我们使用这份配置文件进行启动:

  1. [candy@MacBookPro:~/Documents/core_framework/docker] $ docker-compose -f docker-compose-with-nginx.yaml up
  2. Starting docker_WebApp2_1 ... done
  3. Starting docker_WebApp1_1 ... done
  4. Starting docker_WebApp3_1 ... done
  5. Starting docker_WebProxy_1 ... done
  6. Attaching to docker_WebApp2_1, docker_WebApp1_1, docker_WebApp3_1, docker_WebProxy_1

上述配置, 我们创建了一个3个cfweb service容器. 使用upstream进行反向代理将不同的请求分发到不同的cfweb容器内部.

测试nginx与cfweb

如果启动没有任何问题后, 我们可以尝试使用curl进行测试.

  1. 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
  2. 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]"
  3. 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
  4. 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]"
  5. 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]"
  6. 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都打印出了请求日志, 说明我们的容器已经开始正确运行.