Nginx动静分离

nginx实现动静分离负载均衡集群

LB 负载均衡集群分两类:

LVS (四层)和 nginx 或 haproxy (七层)

客户端通过访问分发器的 VIP 来访问网站

现在应用更复杂,比如现在网站页面有: .php .html .png .jpeg .jsp 等,
有动态页面有静

态页面。静态页面一般是不变的。

LVS 是四层的。基于 IP 的。现在需要在应用层基于丌同的应用迚行分发。

|

七层 LB , Nginx / Haproxy 都可以支持 7 层 LB

现在实现以下功能,拓扑图:

注:使用 Nginx 或 Haproxy 时,这里访问处理都需要经过分发器,没有像 LVS/DR 方式。

工作中,希望这样:

静态文件处理:可以使用 nginx 或 apache

动文件处理: apache ,tomcat

图片文件处理: squid

使用 nginx 实现动静分离的负载均衡集群

  1. Nginx 负载均衡基础知识

nginx 的 upstream 目前最常用 3 种方式的分配

1)、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。

2)、weight

挃定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。

3)、ip_hash

每个请求挄访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的

问题。

4)、fair(第三方)

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

5)、url_hash(第三方) url 哈希

实例 1:使用 nginx 实现负载均衡和动静分离

源码编译安装 nginx

一、安装 nginx 时必须先安装相应的编译工具

[root@xuegod63 ~]#yum -y install gcc
gcc-c++ autoconf automake

[root@xuegod63 ~]#yum -y install zlib
zlib-devel openssl openssl-devel pcre pcre-devel

zlib:nginx 提供 gzip 模块,需要 zlib 库支持

openssl:nginx 提供 ssl 功能

pcre:支持地址重写 rewrite 功能

安装 nginx:

[root@xuegod63 ~]# ll nginx-1.8.0.tar.gz -h
#整个 nginx 文件不到 813K,很小

-rw-r–r-- 1 root root 813K Jul 14 20:17
nginx-1.8.0.tar.gz

[root@xuegod63 ~]# tar -zxvf
nginx-1.8.0.tar.gz -C /usr/local/src/

[root@xuegod63 ~]# cd /usr/local/src/nginx-1.8.0/

[root@xuegod63 ~]# ./configure
–prefix=/server/nginx-1.8.0 --with-http_dav_module

–with-http_stub_status_module
 --with-http_addition_module  --with-http_sub_module

–with-http_flv_module --with-http_mp4_module

查看参数:

[root@xuegod63 nginx-1.8.0]# ./configure
–help | grep mp4

参数:

–with-http_dav_module 吭用 ngx_http_dav_module 支持(增加 PUT,DELETE,MKCOL:创建集

合,COPY 和 MOVE 方法)默认情况下为关闭,需编译开启

–with-http_stub_status_module 吭用 ngx_http_stub_status_module 支持(获取 nginx 自上次启

动以来的工作状态)

–with-http_addition_module 启用 ngx_http_addition_module 支持(作为一个输出过滤器,支持

不完全缓冲,分部分响应请求)

–with-http_sub_module 启用 ngx_http_sub_module 支持(允许用一些其他文本替换 nginx 响应中

的一些文本)

–with-http_flv_module 启用 ngx_http_flv_module 支持(提供寻求内存使用基于时间的偏移量文件)

–with-http_mp4_module 启用对 mp4 文件支持(提供寻求内存使用基于时间的偏移量文件)

编译和安装:

[root@xuegod63 ~]#make -j 4

[root@xuegod63 ~]#make install

生成运行 nginx 的用户:

[root@xuegod63 nginx-1.8.0]# useradd -u
8000 -s /sbin/nologin nginx

[root@xuegod63 nginx-1.8.0]# id !$

id nginx

uid=8000(nginx) gid=8000(nginx)
groups=8000(nginx)

nginx 主要目录结构:

[root@xuegod63 /]# ls /server/nginx-1.8.0/

conf html logs sbin

conf #配置文件

html #网站根目录

logs #日志

sbin #nginx 启动脚本

主配置文件:

[root@xuegod63 /]# ls /server/nginx-1.8.0/conf/nginx.conf

启动 nginx:

[root@xuegod63 /]#
/server/nginx-1.8.0/sbin/nginx

[root@xuegod63 /]# netstat -antup | grep
:80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN

5281/httpd

[root@xuegod63 /]# netstat -antup | grep
:80

开机启动:

[root@xuegod63 nginx-1.8.0]# echo
'/server/nginx-1.8.0/sbin/nginx & ’ >> /etc/rc.local

测试:

http://192.168.1.63/

nginx 服务日常操作:

测试配置文件语法:

[root@xuegod63 nginx-1.8.0]#
/server/nginx-1.8.0/sbin/nginx -t

nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf
syntax is ok

nginx: configuration file
/server/nginx-1.8.0/conf/nginx.conf test is successful

重新加载配置文件

[root@xuegod63 nginx-1.8.0]#
/server/nginx-1.8.0/sbin/nginx -s reload

关闭 nginx

[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx
-s stop

[root@xuegod63 /]#
/server/nginx-1.8.0/sbin/nginx -s start #没有 start 参数

nginx: invalid option: “-s start”

配置 nginx 成为分发器,实现动静分离

[root@xuegod63 conf]# cd
/server/nginx-1.8.0/conf #配置文件目录

[root@xuegod63 conf]# cp nginx.conf
nginx.conf.back #备份一下配置文件

[root@xuegod63 conf]# vim nginx.conf

[root@xuegod63 nginx-1.8.0]# vim
/server/nginx-1.8.0/conf/nginx.conf #指定启动 nginx 用户

改:# user nobody;

为:user nginx nginx;

改:

43 location / {

44 root html;

45 index index.html index.htm; #在 location / { 。。。} 中添加以下内容

#定义分发策略

location / {

root html;

index index.html index.htm;

if (KaTeX parse error: Can't use function '\.' in math mode at position 16: request_uri ~* \̲.̲html){

proxy_pass http://htmlservers;

}

if (KaTeX parse error: Can't use function '\.' in math mode at position 16: request_uri ~* \̲.̲php){

proxy_pass http://phpservers;

}

proxy_pass http://picservers;

}

如图:

把以一内容注释掉,否则 php 文件直接在 nginx 服务器上解析了,不再解析给后端服务器:

location ~ .php$ {

73 # root html;

74 # fastcgi_pass 127.0.0.1:9000;

75 # fastcgi_index index.php;

76  #  fastcgi_param
 SCRIPT_FILENAME

/server/nginx-1.8.0/html$fastcgi_script_name;

77 # include fastcgi_params;

78 # }

如图:

#定义负载均衡设备的 Ip

#定义负载均衡设备的 Ip

在配置文件 nginx.conf 的最后一行}前,添加以下内容:

upstream htmlservers { #定义负载均衡服务器组名称

server 192.168.1.62:80;

server 192.168.1.64:80;

}

upstream phpservers{

server 192.168.1.62:80;

server 192.168.1.64:80;

}

upstream picservers {

server 192.168.1.62:80;

server 192.168.1.64:80;

}

#后期工作中,根据工作中的需要,配置成具体业务的 IP 地址

如图:

保存退出。

重新加载 nginx 服务器配置文件:

[root@xuegod63 conf]#
/server/nginx-1.8.0/sbin/nginx -t

nginx: the configuration file
/server/nginx-1.8.0/conf/nginx.conf syntax is ok

nginx: configuration file
/server/nginx-1.8.0/conf/nginx.conf test is successful

[root@xuegod63 conf]#
/server/nginx-1.8.0/sbin/nginx -s reload

配置后端服务器: xuegod62

配置 web 服务器:

[root@xuegod62 html]# yum install httpd php
-y

生成静态测试文件:

root@xuegod62 html]#echo 192.168.1.62 > /var/www/html/index.html

生成动态测试文件:

[root@xuegod62 html]#vim
/var/www/html/test.php #写如以下内容:

192.168.1.62-php

<?php phpinfo(); ?>

生成图片文件:

上传如下图片,到“xuegod62 网站/var/www/html/目录下:

启动 apache 服务器:

[root@xuegod62 html]# service httpd restart

配置后端服务器:
xuegod64

IP: 192.168.1.64

配置 web 服务器:

[root@xuegod64 html]# yum install httpd php
-y

生成静态测试文件:

echo 192.168.1.64 >
/var/www/html/index.html

生成动态测试文件:

vim /var/www/html/test.php #写如以下内容:

192.168.1.64-php

<?php phpinfo(); ?>

生成图片文件:

上传如下图片,到“xuegod64 网站/var/www/html/目录下:

[root@xuegod64 html]# service httpd restart

到此 nginx 实现负载均衡结束。

测试转发静态页面:

http://192.168.1.63/index.html

测试转发劢态页面:

http://192.168.1.63/test.php

测试转发图片:

http://192.168.1.63/pic.jpg

测试性能:

扩展: 文件打开数过多

[root@xuegod64 html]# ab -n 1000 -c 1000
http://192.168.1.62/index.html #运行正常

[root@xuegod64 html]# ab -n 2000 -c 2000
http://192.168.1.62/index.html #报错

This is ApacheBench, Version 2.3
<$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology
Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation,
http://www.apache.org/

Benchmarking 192.168.1.62 (be patient)

socket: Too many open files (24) # 测试时,一次打开的 socket 文件太多。

#ulimit -a #查看

#ulimit -n

1024

系统默认一个迚程最多同时允许打开 1024 的文件

解决:

#ulimit -n 10240 #报错的解决方法

总结,扩展:

如有 tomcat ,apache,squid 配置为如下:

[root@xuegod63 conf]# vim nginx.conf # 在最后添加以下内容。 定义服务器组

upstream tomcat_servers {

server 192.168.1.2:8080;

server 192.168.1.1:8080;

server 192.168.1.11:8080;

}

upstream apache_servers {

server 192.168.1.5:80;

server 192.168.1.177:80;

server 192.168.1.15:80;

}

upstream squid_servers {

server 192.168.1.26:3128;

server 192.168.1.55:3128;

server 192.168.1.18:3128;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值