nginx1
Nginx用反向代理做的负载均衡。 Nginx运用算法进行负载均衡。

2台nginx后面2台web服务器。
Nginx负载均衡就是nginx的upstream模块。
负载均衡:一台nginx后面2台tomcat。还没用到keepliaved。upstream就是一组服务器的集合,
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream web_pools { //web_pools是名称
//ip_hash; //hash算法,用ip_hash的时候backup不能用,
server 10.0.0.9:80 weight=5 max_fails=10 fail_timeout=10s least_conns=10; //默认是80,可以是ip也可以是域名,max_fails检测到失败的次数,最小连接数,
server 10.0.0.10:80 weight=5 max_conns=100; //最大100个连接
#server 10.0.0.10:80 weight=5 backup; //web服务器的热备,上面活着就不服务都死了就服务,
}
server { //请求www.etiantian.org的80端口,会加载虚礼主机server,location是默认情况下,只要访问域名www.etiantian.org都会经过这个location,会去找html首页文件,把请求全部抛给了proxy_pass指令,实际上抛给upstream web_pools配置的2台机器了,
listen 80;
server_name www.etiantian.org;
location / {
root html;
index index.html index.htm;
proxy_pass http://web_pools;
//proxy_set_header Host $host; //携带主机名,就是域名。
include proxy.conf;
}
}
}
proxy.conf:
proxy_redirect off;
proxy_set_header Host $host; //把用户请求的主机名带到后面去,把$host给Host传到后面去。
proxy_set_header X-Forwarded-For $remote_addr; //把用户的ip通过代理转给后面
proxy_connect_timeout 90; //代理和后端超时的时间
proxy_send_timeout 90; //后端服务器必须在这个时间内回传数据,否则nginx断开这个连接
proxy_read_timeout 90; //后端等待后端的时间
proxy_buffer_size 4k; //nginx从服务端收到数据后的缓冲区大小,内存缓存
proxy_buffers 4 32k;//缓冲区的大小
proxy_busy_buffers_size 64k; //繁忙时间的buffer
proxy_temp_file_write_size 64k; //buffer不够的时候写磁盘,磁盘文件可以写多大,磁盘缓存
缓冲是只用一次,后面用户不会在从这里面请求到。缓存是用户可以多次使用的。Nginx的缓存也有第三方插件。

Nginx还有诊断功能,节点好了就可以自动加进来。健康监测是根据端口检查的(探测几次死了移除,探测几次活了就加进来,建议2-3次)。
负载均衡和反向代理的区别: Lvs软件的负载均衡是一个转发的功能,其余的就不管了。 Nginx是反向代理,反向代理是你请求我,我转交给别人。会fork出一个请求交给别人,所以叫代理。代理是要帮你做事的,不仅仅要你直接去找别人。 Nginx的负载均衡依赖ngx_http_upstream_module模块,这个模块支持的代理方式有proxy_pass(默认wrr权重轮训算法),fastcgi_pass,memcached_pass。
算法: Rr轮询:一个一个的转发。 Weight:根据权重(机器性能有关) ip_hash:同一个客户端请求代理服务器,第一次发到哪个服务器后面就发到哪个服务器,知道会话超时。 Fair:动态算法,第三方算法,根据节点的响应时间分配请求。探测哪个节点时间快。必须下载upstream_fair模块。 Url_hash:根据地址来hash,hash(url)==1-10就跑到第一个服务器,hash(url)=10-20就跑到第二个服务器上。同一个地址只定向到一台服务器。跟ip_hash很像。
proxy_pass指令:nginx方向代理模块。
用户请求nginx时候,nginx不会全部转发,nginx要重新发起请求,所以有些内容丢失了。
for n in {1..100};do curl www.etiantian.org; sleep 1;done

Nginx实现动静分离:

后面的2个webServer的代码层没有动静分离。根据扩展名和路径实现动静分离。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream web_pools {
server 10.0.0.9:80 weight=5 max_fails=10 fail_timeout=10s;
server 10.0.0.10:80 weight=5;
#server 10.0.0.10:80 weight=5 backup;
}
server {
listen 80;
server_name www.etiantian.org;
location / { //默认走这里
root html;
index index.html index.htm;
proxy_pass http://web_pools;
include proxy.conf;
}
location /static/image/ { //根据路径进行转发,静态资源分离
proxy_pass http://static_pools;
include proxy.conf;
}
location /dynamic/image/ { //根据路径进行转发
proxy_pass http://dynamic_pools;
include proxy.conf;
}
}
}
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_pools {
server 10.0.0.9:80 weight=5 max_fails=10 fail_timeout=10s;
}
upstream dynamic_pools {
server 10.0.0.10:80 weight=5;
}
server {
listen 80;
server_name www.etiantian.org;
location / { //根据user_agent转发,user_agent是客户端的标识
if ($http_user_agent ~* "MSIE")//IE浏览器
{
proxy_pass http://dynamic_pools;
}
if ($http_user_agent ~* "Firefox")//火狐浏览器
{
proxy_pass http://static_pools;
}
proxy_pass http://dynamic_pools; //默认
include proxy.conf;
}
}
}
移动端的应用:
移动服务器和pc服务器是不一样的。

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_pools {
server 10.0.0.9:80 weight=5 max_fails=10 fail_timeout=10s;
}
upstream dynamic_pools {
server 10.0.0.10:80 weight=5;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
if ($http_user_agent ~* "android")
{
proxy_pass http://static_pools;
}
if ($http_user_agent ~* "iphone")
{
proxy_pass http://dynamic_pools;
}
proxy_pass http://dynamic_pools;
include proxy.conf;
}
}
}

浙公网安备 33010602011771号