天天看点

Linux高级运维(三)-搭建Nginx调度器

NGINX  反向代理

代理服务器: 功能 1调度器(合理调用服务器,负载均衡) -轮询

     2健康检查

使用Nginx实现Web反向代理功能,实现如下功能:

后端Web服务器两台,可以使用httpd实现

Nginx采用轮询的方式调用后端Web服务器

两台Web服务器的权重要求设置为不同的值

最大失败次数为1,失败超时时间为30秒

一.Web调度器(http协议)

1部署web1

装包(httpd)-配置-启动服务

Web服务器可以简单使用yum方式安装httpd实现Web服务,为了可以看出后端服务器的不同,可以将两台后端服务器的首页文档内容设置为不同的内容 ~]#

1)部署后端web1 服务器

1 yum -y install httpd

2.[[email protected] ~]# echo “192.168.2.100” > /var/www/html/index.html

3.[[email protected] ~]# systemctl restart httpd

4.[[email protected] ~]# firewall-cmd --set-default-zone=trusted

5.[[email protected] ~]# setenforce 0

2部署web2

1.[[email protected] ~]# yum -y install httpd

2.[[email protected] ~]# echo “192.168.2.200” > /var/www/html/index.html

3.[[email protected] ~]# systemctl restart httpd

4.[[email protected] ~]# firewall-cmd --set-default-zone=trusted

5.[[email protected] ~]# setenforce 0

3 配置Nginx服务器,添加服务器池,实现反向代理功能

修改/usr/local/nginx/conf/nginx.conf配置文件

Vim /usr/local/nginx/conf/nginx.conf

http{

Upstream webserver {   Upstream (定义集群,添加服务器池)

Server 192.168.2.100 ;

Server 192.168.2.200};

Server{

Location / {

Proxy_pass http://webserver;

#通过proxy_pass 将用户的请求转发给webserer 集群

}

}

4重启服务

-s reload

-t 检测是否写错

5客户端使用浏览器访问代理服务器测试轮询效果

测试 curl http://192.168.4.5

二配置集群属性

 1 设置失败时间 超时时间.权重

Weight weight可以设置后台服务器的权重,max_fails可以设置后台服务器的失败次数,fail_timeout可以设置后台服务器的失败超时时间。

Max_fails

Fail_timeout

Down

Ip_hasn(取消轮询)

Upstream (定义集群,添加服务器池)

1 [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

… …

http {

… …

upstream webserver {

server 192.168.2.100 weight=1 max_fails=1 fail_timeout=30;

server 192.168.2.200 weight=2 max_fails=2 fail_timeout=30;

server 192.168.2.101 down;

}

#weight设置服务器权重值,默认值为1

#max_fails设置最大失败次数

#fail_timeout设置失败超时时间,单位为秒

#down标记服务器已关机,不参与集群调度

… …

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://webserver

2)重新加载配置

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

3)关闭一台后端服务器(如web1)

[[email protected] ~]# systemctl stop httpd

4)客户端使用浏览器访问代理服务器测试轮询效果

[[email protected] ~]# curl http://192.168.4.5 //使用该命令多次访问查看效果

5)再次启动后端服务器的httpd(如web1)

[[email protected] ~]# systemctl start httpd

PS 欲罢不能 :

  1诱人的目标加上不断积极正向的反馈

  2 毫不费力的入门加逐渐上升的挑战

  3 社交性

  4 未完成的紧张感

上瘾的原因

二 nginx 的TCP\UDP调度器

使用Nginx实现TCP/UDP调度器功能,实现如下功能:

后端SSH服务器两台

Nginx编译安装时需要使用–with-stream,开启ngx_stream_core_module模块

Nginx采用轮询的方式调用后端SSH服务器

一部署支持4层TCP/UDP代理的Nginx服务器

1)部署nginx服务器

编译安装必须要使用–with-stream参数开启4层代理模块。

[ro[email protected] ~]# yum -y install gcc pcre-devel openssl-devel //安装依赖包

[[email protected] ~]# tar -xf nginx-1.12.2.tar.gz

[[email protected] ~]# cd nginx-1.12.2

[[email protected] nginx-1.12.2]# ./configure \

–with-http_ssl_module //开启SSL加密功能

–with-stream //开启4层反向代理功能

[[email protected] nginx-1.12.2]# make && make install //编译并安装

二:配置Nginx服务器,添加服务器池,实现TCP/UDP反向代理功能

1)修改/usr/local/nginx/conf/nginx.conf配置文件

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

stream {

upstream backend {

server 192.168.2.100:22; //后端SSH服务器的IP和端口

server 192.168.2.200:22;

}

server {

listen 12345; //Nginx监听的端口

proxy_connect_timeout 1s; //连接的超时时间,可选配置

proxy_timeout 3s;

proxy_pass backend;

}

2 重新加载配置

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

3)客户端使用访问代理服务器测试轮询效果

[[email protected] ~]# ssh 192.168.4.5 -p 12345 //使用该命令多次访问查看效果

*****************************

要求对Nginx服务器进行适当优化,解决如下问题,以提升服务器的处理性能:

如何自定义返回给客户端的404错误页面

如何查看服务器状态信息

如果客户端访问服务器提示“Too many open files”如何解决

如何解决客户端访问头部信息过长的问题

如何让客户端浏览器缓存数据

日志切割

开启gzip压缩功能,提高数据传输效率

开启文件缓存功能

然后客户机访问此Web服务器验证效果:

使用ab压力测试软件测试并发量

编写测试脚本生成长头部信息的访问请求

客户端访问不存在的页面,测试404错误页面是否重定向

一:自定义报错页面

1)优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到

[[email protected] ~]# firefox http://192.168.4.5/xxxxx //访问一个不存在的页面

2)修改Nginx配置文件,自定义报错页面

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

… …

charset utf-8; //仅需要中文时需要改选项,可选项

error_page 404 /404.html; //自定义错误页面

… …

[[email protected] ~]# vim /usr/local/nginx/html/404.html //生成错误页面

Oops,No NO no page …

[[email protected] ~]# nginx -s reload

3)优化后,客户端使用浏览器访问不存在的页面,会提示自己定义的40x.html页面

[[email protected] ~]# firefox http://192.168.4.5/xxxxx //访问一个不存在的页面

4)常见http状态码

二:如何查看服务器状态信息(非常重要的功能)

1)编译安装时使用–with-http_stub_status_module开启状态页面模块

[[email protected] ~]# tar -zxvf nginx-1.12.2.tar.gz

[[email protected] ~]# cd nginx-1.12.2

[[email protected] nginx-1.12.2]# ./configure \

–with-http_ssl_module //开启SSL加密功能

–with-stream //开启TCP/UDP代理模块

–with-http_stub_status_module //开启status状态页面

[[email protected] nginx-1.12.2]# make && make install //编译并安装

2)启用Nginx服务并查看监听端口状态

ss命令可以查看系统中启动的端口信息,该命令常用选项如下:

[[email protected] ~]# /usr/local/nginx/sbin/nginx

[[email protected] ~]# netstat -anptu | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx

[[email protected] ~]# ss -anptu | grep nginx

3)修改Nginx配置文件,定义状态页面

[[email protected] ~]# cat /usr/local/nginx/conf/nginx.conf

… …

location /status {

stub_status on;

#allow IP地址;

#deny IP地址;

}

… …

[[email protected] ~]# nginx

4)优化后,查看状态页面信息

[[email protected] ~]# curl http://192.168.4.5/status

Active connections: 1

server accepts handled requests

10 10 3

Reading: 0 Writing: 1 Waiting: 0

Active connections:当前活动的连接数量。

Accepts:已经接受客户端的连接总数量。

Handled:已经处理客户端的连接总数量。

(一般与accepts一致,除非服务器限制了连接数量)。

Requests:客户端发送的请求数量。

Reading:当前服务器正在读取客户端请求头的数量。

Writing:当前服务器正在写响应信息的数量。

Waiting:当前多少客户端在等待服务器的响应。

继续阅读