Nginx实现七层负载
Nginx实现七层负载
Nginx下载地址:http://nginx.org/en/download.html
实验拓扑图:
设备列表:
IP | 主机名 | 角色 | 备注 |
192.168.1.227 | hhxx-01 | Nginx分发代理 |
|
192.168.1.251 | hhxx-03 | Web服务 |
|
192.168.1.252 | hhxx-04 | Web服务 |
|
192.168.1.253 | 实体机 | 客户端 | 实体机 |
使用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] ~]# tar xfnginx-1.12.2.tar.gz -C /usr/local/src/
[[email protected] ~]# cd/usr/local/src/nginx-1.12.2/
#执行安装路径,开启相应模块
[[email protected] nginx-1.12.2]# ./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
参数:
--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文件支持(提供寻求内存使用基于时间的偏移量文件)
#编译安装
[[email protected] nginx-1.12.2]#make -j 4&& make install
#创建运行用户
[[email protected] nginx-1.12.2]#useradd -s /sbin/nologin nginx
#运行Nginx
[[email protected] nginx-1.12.2]#/usr/local/nginx/sbin/nginx
#查看80端口状态
[[email protected] nginx-1.12.2]# netstat-antup|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3681/nginx: master
#写入到开机自动运行
[[email protected] nginx-1.12.2]#echo '/usr/local/nginx/sbin/nginx' >> /etc/rc.local
#打开Web查看
nginx服务日常操作
测试配置文件语法:
[[email protected] nginx-1.12.2]#cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx –t
nginx: the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful
重新加载配置文件:
[[email protected] sbin]# ./nginx-s reload
停止nginx运行:
[[email protected] sbin]# ./nginx -sstop
修改Nginx配置文件
[[email protected] sbin]# cd /usr/local/nginx/conf
#备份一份配置文件
[[email protected] conf]# cpnginx.conf nginx.conf.bak
#编辑配置文件
[[email protected] conf]# vimnginx.conf
改:# user nobody;
为:usernginx nginx;
将原有的nobody 改为上面创建的运行用户Nginx
改:
43 location / {
44 root html;
45 index index.html index.htm;
46 }
#在location/ { 。。。} 中添加以下内容
#定义分发策略
location / {
root html;
index index.html index.htm;
if ($request_uri ~* \.html$){
proxy_passhttp://htmlservers;
}
if ($request_uri ~* \.php$){
proxy_passhttp://phpservers;
}
proxy_passhttp://picservers;
}
#定义负载均衡设备的 Ip
#定义负载均衡设备的 Ip
在配置文件nginx.conf的最后一行 } 前,添加以下内容:
#定义负载均衡服务器组名称
upstream htmlservers {
server 192.168.1.251:80;
server 192.168.1.252:80;
}
upstream phpservers{
server 192.168.1.251:80;
server 192.168.1.252:80;
}
upstream picservers {
server 192.168.1.251:80;
server 192.168.1.252:80;
}
#修改完后,检测配置文件是否正常
[[email protected] conf]#/usr/local/nginx/sbin/nginx -t
nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx:configuration file /usr/local/nginx/conf/nginx.conf test is successful
#重载Nginx
[[email protected] conf]#/usr/local/nginx/sbin/nginx -s reload
配置后端服务器:hhxx-03
#安装Apache 和PHP服务
[[email protected] ~]# yum installhttpd php –y
#写一个静态测试页面
[[email protected] ~]# echo"Hello,I'm No.192.168.1.251" > /var/www/html/index.html
[[email protected] ~]# cat !$
cat/var/www/html/index.html
Hello,I'mNo.192.168.1.251
#写一个php探针页面
[[email protected] ~]# vim/var/www/html/test.php
<?php
phpinfo();
?>
使用rz命令上传一张jpg格式的图片改名为test.jpg。
#启动Apache服务
[[email protected] html]# systemctlstart httpd
打开web单独测试:
配置后端服务器:hhxx-04
#安装Apache 和PHP服务
[[email protected] ~]# yum installhttpd php –y
#写一个静态测试页面
[[email protected] ~]# echo"Hello,I'm No.192.168.1.252" > /var/www/html/index.html
#写一个php探针页面
[[email protected] ~]# vim/var/www/html/test.php
使用rz命令上传一张jpg格式的图片,改名为test.jpg。
#启动Apache服务
[[email protected] html]# systemctlstart httpd
打开web测试:
打开web测试nginx代理
通过访问Nginx代理IP地址,可获取到两台后端WEB服务的网页信息,刷新间隔1s左右,实际生产中192.168.1.251 和 192.168.1.252存放的web页面代码应保持一致,本次实验主要是为了查看效果而采取不同的内容。