[关闭]
@yanglt7 2018-10-21T15:57:41.000000Z 字数 9344 阅读 703

【Web 集群实战】19_Nginx 反向代理与负载均衡

Web集群实战


1. 集群简介

1.1 集群特点

1.2 集群的常见分类

2. Nginx 负载均衡集群

2.1 反向代理与负载均衡概念简介

2.2 实现 Nginx 负载均衡的组件

Nginx http 功能模块 模块说明
ngx_http_proxy_module proxy 代理模块,用于把请求后抛给服务器节点或 upstream 服务器池
ngx_http_upstream_module 负载均衡模块,可实现网站的负载均衡功能及节点的健康检查

2.3 快速实践 Nginx 负载均衡准备

HOSTNAME IP 说明
lb001 192.168.2.147 Nginx 主负载均衡器
lb002 192.168.2.148 Nginx 辅负载均衡器
web001 192.168.2.145 web001 服务器
web002 192.168.2.146 web002 服务器

【Web 集群实战】06_Nginx 安装与虚拟主机配置

  1. [root@web002 conf]# cat nginx.conf
  2. worker_processes 1;
  3. error_log logs/error.log;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  11. '$status $body_bytes_sent "$http_referer" '
  12. '"$http_user_agent" "$http_x_forwarded_for"';
  13. sendfile on;
  14. keepalive_timeout 65;
  15. include extra/bbs.conf;
  16. include extra/www.conf;
  17. }
  18. [root@web002 conf]# cat extra/www.conf
  19. server {
  20. listen 80;
  21. server_name www.yangyangyang.org;
  22. location / {
  23. root html/www;
  24. index index.html index.htm;
  25. }
  26. access_log logs/access_www.log main gzip buffer=32k flush=5s;
  27. }
  28. [root@web002 conf]# cat extra/bbs.conf
  29. #bbs virtualhost by ylt
  30. server {
  31. listen 80;
  32. server_name bbs.yangyangyang.org;
  33. location / {
  34. root html/bbs;
  35. index index.html index.htm;
  36. }
  37. }
  1. [root@web002 conf]# /application/nginx/sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
  4. [root@web002 conf]# /application/nginx/sbin/nginx
  1. [root@web001 html]# cat www/index.html
  2. 192.168.2.145 www
  3. [root@web001 html]# cat bbs/index.html
  4. 192.168.2.145 bbs
  5. [root@web002 html]# cat bbs/index.html
  6. 192.168.2.146 bbs
  7. [root@web002 html]# cat www/index.html
  8. 192.168.2.146 www
  1. [root@lb001 ~]# tail -1 /etc/hosts
  2. 192.168.2.145 www.yangyangyang.org bbs.yangyangyang.org
  3. [root@lb001 ~]# curl www.yangyangyang.org
  4. 192.168.2.145 www
  5. [root@lb001 ~]# curl bbs.yangyangyang.org
  6. 192.168.2.145 bbs
  1. [root@lb001 ~]# tail -1 /etc/hosts
  2. 192.168.2.146 www.yangyangyang.org bbs.yangyangyang.org
  3. [root@lb001 ~]# curl www.yangyangyang.org
  4. 192.168.2.146 www
  5. [root@lb001 ~]# curl bbs.yangyangyang.org
  6. 192.168.2.146 bbs

2.4 实现一个简单的负载均衡

  1. [root@lb001 conf]# cat nginx.conf
  2. worker_processes 1;
  3. error_log logs/error.log;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  11. '$status $body_bytes_sent "$http_referer" '
  12. '"$http_user_agent" "$http_x_forwarded_for"';
  13. sendfile on;
  14. keepalive_timeout 65;
  15. upstream www_server_pools {
  16. server 192.168.2.145:80 weight=1;
  17. server 192.168.2.146:80 weight=1;
  18. }
  19. server {
  20. listen 80;
  21. server_name www.yangyangyang.org;
  22. location / {
  23. proxy_pass http://www_server_pools;
  24. }
  25. }
  26. }
  1. [root@web002 conf]# /application/nginx/sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
  4. [root@web002 conf]# /application/nginx/sbin/nginx
  1. [root@lb001 conf]# tail -1 /etc/hosts
  2. 192.168.2.147 www.yangyangyang.org
  3. [root@lb001 conf]# curl www.yangyangyang.org
  4. 192.168.2.145 bbs
  5. [root@lb001 conf]# curl www.yangyangyang.org
  6. 192.168.2.146 bbs
  7. [root@lb001 conf]# curl www.yangyangyang.org
  8. 192.168.2.145 bbs
  9. [root@lb001 conf]# curl www.yangyangyang.org
  10. 192.168.2.146 bbs

2.5 反向代理多虚拟主机节点

  1. [root@web001 logs]# tail -2 access_www.log
  2. 192.168.2.133 - - [10/Oct/2018:22:45:50 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
  3. 192.168.2.133 - - [10/Oct/2018:22:45:52 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
  1. proxy_set_header X-Forwarded-For $remote_addr;
  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  10. '$status $body_bytes_sent "$http_referer" '
  11. '"$http_user_agent" "$http_x_forwarded_for"';
  12. sendfile on;
  13. keepalive_timeout 65;
  14. upstream www_server_pools {
  15. server 192.168.2.145:80 weight=1;
  16. server 192.168.2.146:80 weight=1;
  17. }
  18. server {
  19. listen 80;
  20. server_name www.yangyangyang.org;
  21. location / {
  22. proxy_pass http://www_server_pools;
  23. proxy_set_header Host $host;
  24. }
  25. }
  26. }
  1. [root@web002 conf]# /application/nginx/sbin/nginx -s reload
  2. [root@lb001 conf]# curl www.yangyangyang.org
  3. 192.168.2.145 www
  4. [root@lb001 conf]# curl www.yangyangyang.org
  5. 192.168.2.146 www

2.6 反向代理后的节点服务器记录用户 IP

  1. [root@web001 logs]# tail -2 access_www.log
  2. 192.168.2.147 - - [10/Oct/2018:22:45:50 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
  3. 192.168.2.147 - - [10/Oct/2018:22:45:52 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
  1. proxy_set_header X-Forwarded-For $remote_addr;
  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  10. '$status $body_bytes_sent "$http_referer" '
  11. '"$http_user_agent" "$http_x_forwarded_for"';
  12. sendfile on;
  13. keepalive_timeout 65;
  14. upstream www_server_pools {
  15. server 192.168.2.145:80 weight=1;
  16. server 192.168.2.146:80 weight=1;
  17. }
  18. server {
  19. listen 80;
  20. server_name www.yangyangyang.org;
  21. location / {
  22. proxy_pass http://www_server_pools;
  23. proxy_set_header Host $host;
  24. proxy_set_header X-Forwarded-For $remote_addr;
  25. }
  26. }
  27. }
  1. [root@lb001 conf]# /application/nginx/sbin/nginx -s reload

2.7 根据 URL 中的目录地址实现代理转发

背景

需求分析

配置

  1. [root@lb001 conf]# cat nginx.conf
  2. worker_processes 1;
  3. error_log logs/error.log;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  11. '$status $body_bytes_sent "$http_referer" '
  12. '"$http_user_agent" "$http_x_forwarded_for"';
  13. sendfile on;
  14. keepalive_timeout 65;
  15. upstream static_pools {
  16. server 192.168.2.145:80 weight=1;
  17. }
  18. upstream upload_pools {
  19. server 192.168.2.146:80 weight=1;
  20. }
  21. upstream default_pools {
  22. server 192.168.2.148:80 weight=1;
  23. }
  24. server {
  25. listen 80;
  26. server_name www.yangyangyang.org;
  27. location / {
  28. proxy_pass http://default_pools;
  29. include proxy.conf;
  30. }
  31. location /static/ {
  32. proxy_pass http://static_pools;
  33. include proxy.conf;
  34. }
  35. location /upload/ {
  36. proxy_pass http://upload_pools;
  37. include proxy.conf;
  38. }
  39. }
  40. }
  41. [root@lb001 conf]# /application/nginx/sbin/nginx
  42. [root@lb001 conf]# /application/nginx/sbin/nginx -t
  43. nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
  44. nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
  45. [root@lb001 conf]# /application/nginx/sbin/nginx -s reload
  1. [root@web001 ~]# tail -1 /etc/hosts
  2. 192.168.2.145 www.yangyangyang.org bbs.yangyangyang.org
  3. [root@web001 ~]# /application/nginx/sbin/nginx
  4. [root@web001 ~]# /application/nginx/sbin/nginx -t
  5. nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
  6. nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
  7. [root@web001 ~]# /application/nginx/sbin/nginx -s reload
  8. [root@web001 ~]# cd /application/nginx/html/www/
  9. [root@web001 www]# mkdir -p static
  10. [root@web001 www]# echo static_pools >static/index.html
  11. [root@web001 www]# cat static/index.html
  12. static_pools
  13. [root@web001 www]# curl www.yangyangyang.org/static/index.html
  14. static_pools
  1. [root@web002 ~]# /application/nginx/sbin/nginx
  2. [root@web002 ~]# /application/nginx/sbin/nginx -t
  3. nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
  4. nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
  5. [root@web002 ~]# /application/nginx/sbin/nginx -s reload
  6. [root@web002 ~]# tail -1 /etc/hosts
  7. 192.168.2.146 www.yangyangyang.org bbs.yangyangyang.org
  8. [root@web002 ~]# cd /application/nginx/html/www/
  9. [root@web002 www]# mkdir -p upload
  10. [root@web002 www]# echo upload_pools >upload/index.html
  11. [root@web002 www]# cat upload/index.html
  12. upload_pools
  13. [root@web002 www]# curl www.yangyangyang.org/upload/index.html
  14. upload_pools
  1. [root@web003 ~]# /application/nginx/sbin/nginx
  2. [root@web003 ~]# nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
  4. [root@web003 ~]# /application/nginx/sbin/nginx -s reload
  5. [root@web003 ~]# tail -1 /etc/hosts
  6. 192.168.2.150 www.yangyangyang.org bbs.yangyangyang.org
  7. [root@web003 ~]# cd /application/nginx/html/www/
  8. [root@web003 www]# echo default_pools >index.html
  9. [root@web003 www]# cat index.html
  10. default_pools
  11. [root@web003 www]# curl www.yangyangyang.org
  12. default_pools

static

upload

default

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注