nginx实现负载均衡和动静分离

拓扑图:
nginx实现负载均衡和动静分离

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

源码编译安装nginx
一、安装nginx时必须先安装相应的编译工具和相关依赖
[[email protected] ~]#yum -y install gcc gcc-c++ autoconf automake
[[email protected] ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
zlib:nginx提供gzip模块,需要zlib库支持openssl:nginx提供ssl功能pcre:支持地址重写rewrite功能

安装nginx:
[[email protected] ~]# 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
[[email protected] ~]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/nginx-1.8.0/
[[email protected] ~]# ./configure --prefix=/usr/local/nginx --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

查看参数:
[[email protected] 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文件支持(提供寻求内存使用基于时间的偏移量文件)

编译和安装: (查看CPU逻辑数cat /proc/cpuinfo | grep processor | wc -l)
[[email protected] ~]#make -j 6
[[email protected] ~]#make install

生成运行nginx的用户:
[[email protected] nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx
[[email protected] nginx-1.8.0]# id !$
id nginx
uid=8000(nginx) gid=8000(nginx) groups=8000(nginx)
nginx主要目录结构:
[[email protected] /]# ls /server/nginx-1.8.0/
conf html logs sbi
conf #配置文件
html #网站根目录
logs #日志
sbin #nginx启动脚本

主配置文件:
[[email protected] /]# ls /server/nginx-1.8.0/conf/nginx.conf
启动nginx:
[[email protected] /]# /server/nginx-1.8.0/sbin/nginx
[[email protected] /]# netstat -antup | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5281/httpd
[[email protected] /]# netstat -antup | grep :80

开机启动:
[[email protected] nginx-1.8.0]# echo '/server/nginx-1.8.0/sbin/nginx & ’ >> /etc/rc.local
测试:
http://192.168.1.63/

nginx服务日常操作:
测试配置文件语法:
[[email protected] 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

重新加载配置文件
[[email protected] nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -s reload
关闭nginx
[[email protected] /]# /server/nginx-1.8.0/sbin/nginx -s stop
[[email protected] /]# /server/nginx-1.8.0/sbin/nginx -s start #没有start参数
nginx: invalid option: “-s start”

配置nginx成为分发器,实现动静分离
[[email protected] conf]# cd /server/nginx-1.8.0/conf #配置文件目录
[[email protected] conf]# cp nginx.conf nginx.conf.back #备份一下配置文件
[[email protected] conf]# vim nginx.conf
[[email protected] 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;
}
如图:
nginx实现负载均衡和动静分离

把以下内容注释掉,否则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 # }
如图:
nginx实现负载均衡和动静分离
#定义负载均衡设备的 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实现负载均衡和动静分离
保存退出

重新加载nginx服务器配置文件:
[[email protected] 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
[[email protected] conf]# /server/nginx-1.8.0/sbin/nginx -s reload

配置后端服务器: yunzu62

配置web服务器:
[[email protected] html]# yum install httpd  php -y
生成静态测试文件:
[email protected] html]#echo 192.168.1.62 > /var/www/html/index.html
生成动态测试文件:
[[email protected] html]#vim  /var/www/html/test.php   #写如以下内容:
192.168.1.62-php
<?php
phpinfo();
?>
生成图片文件:
上传如下图片,到“yunzu62网站/var/www/html/目录下:
nginx实现负载均衡和动静分离
 
启动apache服务器:
[[email protected] html]# service httpd restart

配置后端服务器: yunzu64

IP: 192.168.1.64
配置web服务器:
[[email protected] 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();
?>
生成图片文件:
上传如下图片,到“yunzu64网站/var/www/html/目录下:
nginx实现负载均衡和动静分离
[[email protected] html]# service httpd restart
到此nginx实现负载均衡结束。

测试转发静态页面:
http://192.168.1.63/
测试转发动态页面:
http://192.168.1.63/test.php
测试转发图片:
http://192.168.1.63/pic.jpg

测试自动剔除坏的节点:
[[email protected] html]# service httpd stop
Stopping httpd: [ OK ]
访问:
http://192.168.1.63/pic.jpg
http://192.168.1.63/pic.jpg
都访问到

测试性能:
扩展: 文件打开数过多
[[email protected] html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #运行正常
[[email protected] 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/

Nginx负载的5种策略设置方法:
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
server 192.168.1.62;
server 192.168.1.64;
}

2、指定权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
server 192.168.1.62 weight=1;
server 192.168.1.64 weight=2;
}

3、IP绑定 ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.1.62:80;
server 192.168.1.64:80;
}

4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
server server1;
server server2;
fair;
}

5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}