当前位置:   article > 正文

nginx反向代理及负载均衡

nginx反向代理及负载均衡
node1192.168.136.55Nginx主负载均衡器
node3192.168.136.57Web01服务器
node4192.168.136.58Web02服务器
node5192.168.135.131客户端(测试)

 nginx反向代理

1. 安装nginx

三台机器都安装nginx

yum install nginx -y

2. 配置用于测试的Web服务(以下操作在两台web服务器)。 配置虚拟主机

  1. [root@node3 conf.d]# mkdir -p /usr/share/nginx/html/{www,bbs}/logs
  2. [root@node3 ~]# cd /etc/nginx/conf.d/
  3. [root@node3 conf.d]# vim vhost.conf
  4. server {
  5. listen 80;
  6. server_name bbs.yunjisuan.com;
  7. location / {
  8. root /usr/share/nginx/html/bbs;
  9. index index.html index.htm;
  10. }
  11. access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
  12. }
  13. server {
  14. listen 80;
  15. server_name www.yunjisuan.com;
  16. location / {
  17. root /usr/share/nginx/html/www;
  18. index index.html index.htm;
  19. }
  20. access_log /usr/share/nginx/html/www/logs/access_www.log main;
  21. }
  22. # nginx -t 测语法
  23. # systemctl start nginx

node4 一样配置

3. 配置用于测试的Web服务 在两台web服务器上操作。 准备测试页面

  1. echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html
  2. echo "`hostname -I `bbs" > /usr/share/nginx/html/bbs/index.html

 客户端测试:

  1. # vi + /etc/hosts
  2. 192.168.136.57 bbs.yunjisuan.com www.yunjisuan.com
  3. 192.168.136.58 bbs.yunjisuan.com www.yunjisuan.com
  4. [root@node5 ~]# curl www.yunjisuan.com
  5. 192.168.136.57 www
  6. [root@node5 ~]# curl bbs.yunjisuan.com
  7. 192.168.136.57 bbs
  8. #或者
  9. [root@node5 ~]# curl -H host:bbs.yunjisuan.com 192.168.136.57
  10. 192.168.136.57 bbs
  11. [root@node5 ~]# curl -H host:www.yunjisuan.com 192.168.136.57
  12. 192.168.136.57 www

 nginx负载均衡

实现 Nginx 负载均衡的组件说明

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

配置简单的负载均衡 以下操作在node1

实现:经过反向代理后的节点服务器记录用户IP

          反向代理多虚拟主机节点服务器

  1. # cd /etc/nginx/conf.d/
  2. # vim vhost.conf
  3. upstream wwwPools {
  4. server 192.168.136.57;
  5. server 192.168.136.58;
  6. }
  7. server {
  8. listen 80;
  9. server_name www.yunjisuan.com;
  10. location / {
  11. proxy_pass http://wwwPools;
  12. proxy_set_header Host $host; # 依据host字段信息,识别代理的是哪个虚拟主机
  13. proxy_set_header X-Forwarded-For $remote_addr; # 显示客户端的IP地址
  14. }
  15. }
  16. server {
  17. listen 80;
  18. server_name bbs.yunjisuan.com;
  19. location / {
  20. proxy_pass http://wwwPools;
  21. proxy_set_header Host $host;
  22. proxy_set_header X-Forwarded-For $remote_addr;
  23. }
  24. }

客户端:  (改成反向代理的地址)

  1. # vim /etc/hosts
  2. 192.168.136.55 bbs.yunjisuan.com www.yunjisuan.com
  1. [root@node5 ~]# for ((i=1;i<=4;i++)); do curl http://bbs.yunjisuan.com;done
  2. 192.168.136.58 bbs
  3. 192.168.136.57 bbs
  4. 192.168.136.58 bbs
  5. 192.168.136.57 bbs

如果web服务器是apache

  1. # vim vhost.conf
  2. <VirtualHost *:80>
  3. ServerName bbs.yunjisuan.com
  4. DocumentRoot "/var/www/html/bbs/"
  5. CustomLog "/var/www/html/bbs/logs/access_log" combined
  6. </VirtualHost>
  7. <VirtualHost *:80>
  8. ServerName www.yunjisuan.com
  9. DocumentRoot "/var/www/html/www/"
  10. CustomLog "/var/www/html/www/logs/access_log" combined
  11. </VirtualHost>
  1. # cp /usr/share/nginx/html/www/index.html /var/www/html/www
  2. # cp /usr/share/nginx/html/bbs/index.html /var/www/html/bbs

修改配置文件 vim /etc/httpd/conf/httpd.conf

LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

如果是tomcat

改成 

pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i" resolveHosts="false"

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

vim /etc/nginx/conf.d/vhost.conf

  1. upstream static_pools {
  2. server 192.168.136.57;
  3. }
  4. upstream upload_pools {
  5. server 192.168.136.58;
  6. }
  7. upstream default_pools {
  8. server 192.168.136.59;
  9. }
  10. server {
  11. listen 80;
  12. server_name www.yunjisuan.com;
  13. location /static/ {
  14. proxy_set_header X-Forwarded-For $remote_addr;
  15. proxy_pass http://static_pools;
  16. }
  17. location /upload/ {
  18. proxy_set_header X-Forwarded-For $remote_addr;
  19. proxy_pass http://upload_pools;
  20. }
  21. location / {
  22. proxy_set_header X-Forwarded-For $remote_addr;
  23. proxy_pass http://default_pools;
  24. }
  25. }

在node3上创建页面 

[root@node3 static]# echo "static web page test." > /usr/share/nginx/html/static/index.html 

在node4上

  1. [root@openEuler-node4 ~]# mkdir /var/www/html/upload
  2. [root@openEuler-node4 ~]# echo "upload test page" > /var/www/html/upload/index.html

在192.168.136.59上

# echo "default test page" > /usr/share/nginx/html/index.html
  1. [root@node5 conf.d]# curl http://www.yunjisuan.com/upload/
  2. upload test page
  3. [root@node5 conf.d]# curl http://www.yunjisuan.com/static/
  4. static web page test.
  5. [root@node5 conf.d]# curl http://www.yunjisuan.com
  6. default test page

根据客户端的设备(user_agent)转发 

  1. [root@node1 ~]# vim /etc/nginx/conf.d/vhost.conf
  2. upstream static_pools {
  3. server 192.168.136.57;
  4. }
  5. upstream upload_pools {
  6. server 192.168.136.58;
  7. }
  8. upstream default_pools {
  9. server 192.168.136.59;
  10. }
  11. server {
  12. listen 80;
  13. server_name www.yunjisuan.com;
  14. location / {
  15. if ($http_user_agent ~* "MSIE")
  16. { proxy_pass http://static_pools;
  17. }
  18. if ($http_user_agent ~* "Chrome")
  19. {
  20. proxy_pass http://upload_pools;
  21. }
  22. proxy_pass http://default_pools;
  23. }
  24. }
  1. [root@node3 html]# echo "static page" > /usr/share/nginx/html/index.html
  2. [root@node4 html]# echo "upload test page " > /usr/share/nginx/html/index.html
  1. [root@node5 conf.d]# curl -A chrome -H host:www.yunjisuan.com 192.168.136.55
  2. upload test page
  3. [root@node5 conf.d]# curl -A MSIE -H host:www.yunjisuan.com 192.168.136.55
  4. static page
  5. [root@node5 conf.d]# curl -A XXX -H host:www.yunjisuan.com 192.168.136.55
  6. default test page

 

upstream其他使用

修改为IP HASH算法

始终只由一台服务器提高 

添加server备份  

当前面都down了 启动backup

注意:backup不能和ip_hash同时开启  

想完全不启用其中的一台server

1、backup所有RS都不能提高服务时才起作用

2、backup 不能和ip_hash算法一起使用

3、down 通常用于RS维护时,不参与调度

4、ip_hash 会话保持

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/486557
推荐阅读
相关标签
  

闽ICP备14008679号