nginx反向代理+keepalived

编译时要注意要安装upstream模块,但是默认是安装的,,所以编译可以用web服务的编译来编译。

反向代理

upstream backend {
	#ip_hash;保持会话,这个功能不是很好,这个可以查看本地图片
        server 192.168.222.140:80 max_fails=3 fail_timeout=30s;
	server 192.168.222.141:80 max_fails=3 fail_timeout=30s;
}

server {
    listen       80;
    server_name  www.etiantian.org;

    location / {
        proxy_pass http://backend;
    }
}

此时如果要域名解析,应该使用负载均衡服务器的ip ,域名是www.eitiantian.org,bbs.etiantian.org,blog.etiantian.org。

用户web服务器是多站点,多虚拟主机的情况下:

upstream backend {

    server 192.168.222.140:80 max_fails=3 fail_timeout=30s;
	server 192.168.222.141:80 max_fails=3 fail_timeout=30s;
}

server {
    listen       80;
    server_name  www.etiantian.org;

	location / {
	proxy_pass http://backend;
	proxy_set_header Host $host;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
	}
}
server {
    listen       80;
    server_name  bbs.etiantian.org;

	location / {
	proxy_pass http://backend;
	proxy_set_header Host $host;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
	}
}

一致性哈希算法对数据的存储是不均匀的,但可以最大限度的减少缓存的失效量。在大规模部署Memcached时,容灾和扩容一定要用一致性哈希算法,以确保在出现故障或容量问题时减小对数据库的影响。

当遇到缓存服务器时,,缓存不会存在一个服务器上,所以各个之间是不相互联系的,所以要固定每次访问的缓存服务器,而不是每次访问的服务器都不一样,,,所以ip_hash就体现了效果,所有的缓存服务器加载一起才是个整体。

二、Keepalived+nginx

前提需要关闭防火墙,,因为是内网,关闭没啥大碍,,,不关闭,,keepalived就无法检测对方是否存在

master配置-keepalived:

! Configuration File for keepalived

global_defs {
   notification_email {
   。。。。。。@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 10.0.0.1
   smtp_connect_timeout 30
   router_id LVS_7
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 55
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.222.143/24
    }
}

slave-keepalived配置:

! Configuration File for keepalived

global_defs {
   notification_email {
   。。。。。@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 10.0.0.1
   smtp_connect_timeout 30
   router_id LVS_2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 55
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.222.143/24
    }
}

MASTER 与slave的-nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    error_log logs/error.log error;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    include extra/upstream.conf;
}

master--extra/upstream

upstream backend {
        server 192.168.222.132:80 max_fails=3 fail_timeout=30s;
	server 106.14.124.164:80 max_fails=3 fail_timeout=30s;
}

server {
    listen       80;
    server_name  www.etiantian.org;

    location / {
        proxy_pass http://backend;
    }
}

================================================================================================================

用VIP来访问
master 192.168.222.130
slaver 192.168.222.131
realserver 1 192.168.222.132
realserver 2 192.168.222.133


模拟调试,关闭任意keepalived


PV低于2000万,可以选用NGINX做反向代理

LVS属于转发,,,NGINX是代理


大并发,简单转发,,LVS
大并发,功能要求复杂,根据URI转发,,LVS+NGINX
并发不大,NGINX/haproxy

=================================================================================================================

upstream模块
它支持的代理方式有 proxy_pass,fastcgi_pass,memecached_pass
proxy_pass:反向代理
fastcgi_pass:用于动态程序的交互
memecached_pass:与memcached交互

语法:

upstream backend {
    server backend1.example.com       weight=5;  //端口默认不加就是80,,后面是权重,跟LVS一样,越大,分配的可能性越多
    server backend2.example.com:8080;   //域名加端口
    server unix:/tmp/backend3;       //指定socket文件

    server backup1.example.com:8080   backup;    //备份服务器,等上面指定的服务器都不可以访问的时候会启用,backup的用法和haproxy中用法一样
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

参数:
1、本地图片有参数信息,upstream模块
max_fails=number
nginx尝试连接后端主机失败的次数,这个参数配合下面的三个参数:proxy_next_upstream, fastcgi_next_upstream, memcached_next_upstream
当请求失败后,不去提示用户,而是再去请求别的服务器。
当Nginx接收后端服务器返回这三个参数定义的状态码的时候,会将这个请求转发给正常工作的后端服务器,例如404,502,503
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

==================================================================================================================

upstream算法:
rr轮询(默认)
按客户端请求顺序把客户端的请求逐一分配到不同的后端的服务器,这相当于LVS中
的RR算法。

weight(权重)
在轮询算法的基础上加上权重(默认是rr+weight),权重越大,转发的请求也就越多。

ip_hash
每个请求按访问的IP 的hash结果分配,当新的请求到达时,先将其客户端ip通过哈希算法哈希出一个值,在随后请求客户端,ip的哈希值只要相同,就会被分配至同一台服务器,该调度算法可以解决动态网页session共享问题,但有时会导致请求分配不均,即无法保证1:1的负载均衡。在国内所有的公司都是NAT上网,多个PC对应一个外部ip。
提示:必须是最前端的服务器,后端也必须直接应用服务器多数情况不能和权重参数一起使用。

upstream backend {
    ip_hash;

    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com down;
    server backend4.example.com;
}

注意,当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能是weight和backup

fair
按照后端服务器的响应时间来分配请求,响应时间短的优先分配。

url_hash
按照url的hash结果来分配请求,让每个url定向到同一个后端服务器,后端服务器为缓存服务器时效果显著。。。。提高后端缓存服务器的命中率。

upstream backend {
    
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com down;
    server backend4.example.com;
	hash $request_uri;
	hash_method crc32;
}

least_conn
最少连接数,哪个节气连接数少就少分发。

淘宝的服务器的一致性哈希算法。
http://tengine.taobao.org/document_cn/http_upstream_consistent_hash_cn.html

================================================================================================================

location的正则表达式

=   精确匹配,如果找到匹配=号的内容,立即停止搜索,并立即处理请求(优先级最高)
~   区分大小写
~*  不区分大小写
^~  只匹配字符串,不匹配正则表达式
@   指定一个命名的location,一般只用于内部重定向请求。

Let’s illustrate the above by an example:

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* .(gif|jpg|jpeg)$ {
    [ configuration E ]
}
The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.

http_proxy_module

proxy_pass指令,将请求转发到另一台服务器。

location / {
error_page 404 = @fallback;
}

location @fallback {
proxy_pass http://backend;
}

  

http_proxy模块参数,,,nginx的代理功能是通过http proxy模块来实现的,默认在安装nginx时已经安装了http_proxy模块,因此可以直接使用http proxy模块
本地有图片,,http proxy 模块参数
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

特殊的参数:
proxy_set_header 设置由后端的服务器获取用户的主机名或者真实ip地址,以及代理者的真实ip地址。


proxy_set_header Host $host 当后端WEB服务器上也配置有多个虚拟主机时,需要用该Header来区分反向代理哪个主机名。
上述的可以实现,在负载均衡器上配,,有负载均衡器向后端请求

proxy_set_header X-Forwarded-For $remote_addr 如果后端WEB服务器上的程序需要获取用户ip,从该Header头获取。
rs节点的nginx.conf配置:
log_format commonlog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
要看客户的ip,不知加参数,还要将日志格式修改。

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

upstream static_pools {
         server 10.0.0.6:80  weight=5;
}
upstream dynamic_pools {
         server 10.0.0.8:80  weight=5;
}

server {
       listen       80;
       server_name  blog.etiantian.org;
       location / {
        proxy_pass  http://dynamic_pools;
        include extra/proxy.conf;
       }

       location /static/ { 
        proxy_pass  http://static_pools;
        include extra/proxy.conf;
       }

       location /dynamic/ { 
        proxy_pass  http://dynamic_pools;
        include extra/proxy.conf;
       }
        access_log off;
     }

根据用户请求的user_agent向后转发对应的服务器。,,手机浏览跟pc浏览

server {
       listen       80;
       server_name  blog.etiantian.org;
       location / {
        if ($http_user_agent ~* "android")
		  {
			proxy_pass  http://dynamic_pools;
		  }
        if ($http_user_agent ~* "iphone")
          {
            proxy_pass  http://static_pools;
            }
        proxy_pass  http://dynamic_pools;
        include extra/proxy.conf;
       }

        access_log off;
     }

health_ceck

补充,健康检查

原文地址:https://www.cnblogs.com/bill2014/p/7611925.html