@yanglt7
2018-10-21T15:57:41.000000Z
字数 9344
阅读 796
Web集群实战
严格地说,Nginx 仅仅是作为 Nginx Proxy 反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以称为 Nginx 负载均衡。
普通的负载均衡软件,如 LVS,其实现的功能只是对请求数据包的转发(也可能会改写数据包)、传递,其中 DR 模式从负载均衡下面的节点服务器来看,接收到的请求还是来自访问负载均衡器的客户端的真实用户,而反向代理接收访问用户的请求后,会代理用户重新发起请求代理下的节点服务器,最后把数据返回给客户端用户,在节点服务器看来,访问的节点服务器的客户端用户就是反向代理服务器了,而非真实的网站访问用户。
| Nginx http 功能模块 | 模块说明 |
|---|---|
| ngx_http_proxy_module | proxy 代理模块,用于把请求后抛给服务器节点或 upstream 服务器池 |
| ngx_http_upstream_module | 负载均衡模块,可实现网站的负载均衡功能及节点的健康检查 |
| 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 服务器 |
[root@web002 conf]# cat nginx.confworker_processes 1;error_log logs/error.log;events {worker_connections 1024;}http {include 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" "$http_x_forwarded_for"';sendfile on;keepalive_timeout 65;include extra/bbs.conf;include extra/www.conf;}[root@web002 conf]# cat extra/www.confserver {listen 80;server_name www.yangyangyang.org;location / {root html/www;index index.html index.htm;}access_log logs/access_www.log main gzip buffer=32k flush=5s;}[root@web002 conf]# cat extra/bbs.conf#bbs virtualhost by yltserver {listen 80;server_name bbs.yangyangyang.org;location / {root html/bbs;index index.html index.htm;}}
[root@web002 conf]# /application/nginx/sbin/nginx -tnginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful[root@web002 conf]# /application/nginx/sbin/nginx
[root@web001 html]# cat www/index.html192.168.2.145 www[root@web001 html]# cat bbs/index.html192.168.2.145 bbs[root@web002 html]# cat bbs/index.html192.168.2.146 bbs[root@web002 html]# cat www/index.html192.168.2.146 www
[root@lb001 ~]# tail -1 /etc/hosts192.168.2.145 www.yangyangyang.org bbs.yangyangyang.org[root@lb001 ~]# curl www.yangyangyang.org192.168.2.145 www[root@lb001 ~]# curl bbs.yangyangyang.org192.168.2.145 bbs
[root@lb001 ~]# tail -1 /etc/hosts192.168.2.146 www.yangyangyang.org bbs.yangyangyang.org[root@lb001 ~]# curl www.yangyangyang.org192.168.2.146 www[root@lb001 ~]# curl bbs.yangyangyang.org192.168.2.146 bbs
[root@lb001 conf]# cat nginx.confworker_processes 1;error_log logs/error.log;events {worker_connections 1024;}http {include 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" "$http_x_forwarded_for"';sendfile on;keepalive_timeout 65;upstream www_server_pools {server 192.168.2.145:80 weight=1;server 192.168.2.146:80 weight=1;}server {listen 80;server_name www.yangyangyang.org;location / {proxy_pass http://www_server_pools;}}}
[root@web002 conf]# /application/nginx/sbin/nginx -tnginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful[root@web002 conf]# /application/nginx/sbin/nginx
[root@lb001 conf]# tail -1 /etc/hosts192.168.2.147 www.yangyangyang.org[root@lb001 conf]# curl www.yangyangyang.org192.168.2.145 bbs[root@lb001 conf]# curl www.yangyangyang.org192.168.2.146 bbs[root@lb001 conf]# curl www.yangyangyang.org192.168.2.145 bbs[root@lb001 conf]# curl www.yangyangyang.org192.168.2.146 bbs
[root@web001 logs]# tail -2 access_www.log192.168.2.133 - - [10/Oct/2018:22:45:50 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"192.168.2.133 - - [10/Oct/2018:22:45:52 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
proxy_set_header X-Forwarded-For $remote_addr;
worker_processes 1;error_log logs/error.log;events {worker_connections 1024;}http {include 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" "$http_x_forwarded_for"';sendfile on;keepalive_timeout 65;upstream www_server_pools {server 192.168.2.145:80 weight=1;server 192.168.2.146:80 weight=1;}server {listen 80;server_name www.yangyangyang.org;location / {proxy_pass http://www_server_pools;proxy_set_header Host $host;}}}
[root@web002 conf]# /application/nginx/sbin/nginx -s reload[root@lb001 conf]# curl www.yangyangyang.org192.168.2.145 www[root@lb001 conf]# curl www.yangyangyang.org192.168.2.146 www
[root@web001 logs]# tail -2 access_www.log192.168.2.147 - - [10/Oct/2018:22:45:50 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"192.168.2.147 - - [10/Oct/2018:22:45:52 +0800] "GET / HTTP/1.0" 200 36 "-" "curl/7.29.0"
proxy_set_header X-Forwarded-For $remote_addr;
worker_processes 1;error_log logs/error.log;events {worker_connections 1024;}http {include 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" "$http_x_forwarded_for"';sendfile on;keepalive_timeout 65;upstream www_server_pools {server 192.168.2.145:80 weight=1;server 192.168.2.146:80 weight=1;}server {listen 80;server_name www.yangyangyang.org;location / {proxy_pass http://www_server_pools;proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;}}}
[root@lb001 conf]# /application/nginx/sbin/nginx -s reload
default_pools 为默认的服务器池,即动态服务器,有一个服务器,地址为 192.168.2.150,端口为 80。
Nginx 反向代理的实际配置如下:
[root@lb001 conf]# cat nginx.confworker_processes 1;error_log logs/error.log;events {worker_connections 1024;}http {include 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" "$http_x_forwarded_for"';sendfile on;keepalive_timeout 65;upstream static_pools {server 192.168.2.145:80 weight=1;}upstream upload_pools {server 192.168.2.146:80 weight=1;}upstream default_pools {server 192.168.2.148:80 weight=1;}server {listen 80;server_name www.yangyangyang.org;location / {proxy_pass http://default_pools;include proxy.conf;}location /static/ {proxy_pass http://static_pools;include proxy.conf;}location /upload/ {proxy_pass http://upload_pools;include proxy.conf;}}}[root@lb001 conf]# /application/nginx/sbin/nginx[root@lb001 conf]# /application/nginx/sbin/nginx -tnginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful[root@lb001 conf]# /application/nginx/sbin/nginx -s reload
[root@web001 ~]# tail -1 /etc/hosts192.168.2.145 www.yangyangyang.org bbs.yangyangyang.org[root@web001 ~]# /application/nginx/sbin/nginx[root@web001 ~]# /application/nginx/sbin/nginx -tnginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful[root@web001 ~]# /application/nginx/sbin/nginx -s reload[root@web001 ~]# cd /application/nginx/html/www/[root@web001 www]# mkdir -p static[root@web001 www]# echo static_pools >static/index.html[root@web001 www]# cat static/index.htmlstatic_pools[root@web001 www]# curl www.yangyangyang.org/static/index.htmlstatic_pools
[root@web002 ~]# /application/nginx/sbin/nginx[root@web002 ~]# /application/nginx/sbin/nginx -tnginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful[root@web002 ~]# /application/nginx/sbin/nginx -s reload[root@web002 ~]# tail -1 /etc/hosts192.168.2.146 www.yangyangyang.org bbs.yangyangyang.org[root@web002 ~]# cd /application/nginx/html/www/[root@web002 www]# mkdir -p upload[root@web002 www]# echo upload_pools >upload/index.html[root@web002 www]# cat upload/index.htmlupload_pools[root@web002 www]# curl www.yangyangyang.org/upload/index.htmlupload_pools
[root@web003 ~]# /application/nginx/sbin/nginx[root@web003 ~]# nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful[root@web003 ~]# /application/nginx/sbin/nginx -s reload[root@web003 ~]# tail -1 /etc/hosts192.168.2.150 www.yangyangyang.org bbs.yangyangyang.org[root@web003 ~]# cd /application/nginx/html/www/[root@web003 www]# echo default_pools >index.html[root@web003 www]# cat index.htmldefault_pools[root@web003 www]# curl www.yangyangyang.orgdefault_pools


