nginx 反向代理与负载均衡、动静分离、状况监控
反向代理与负载均衡
nginx
通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。
nginx
实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx
发布的路径去读取,而不需要从后台服务器获取了。
但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync
做服务端自动同步或者使用NFS
、MFS
分布式共享存储。
Http Proxy
模块,功能很多,最常用的是proxy_pass
和proxy_cache
如果要使用proxy_cache
,需要集成第三方的ngx_cache_purge
模块,用来清除指定的URL缓存。这个集成需要在安装nginx
的时候去做,如:
./configure --add-module=../ngx_cache_purge-1.0 ......
nginx
通过upstream
模块来实现简单的负载均衡,upstream
需要定义在http
段内
在upstream
段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:
upstream idfsoft.com {
ip_hash;
server 127.0.0.1:9080 weight=5;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。
定义好upstream
后,需要在server
段内添加如下内容:
server {
location / {
proxy_pass http://idfsoft.com;
}
}
反向代理和负载均衡配置实例:
环境:
安装服务 | 主机名 | IP |
---|---|---|
nginx | nginx | 192.168.200.5 |
httpd | RS1 | 192.168.200.6 |
httpd | RS2 | 192.168.200.7 |
//在RS上配置apache服务 需要关闭防火墙和selinux这里不演示了
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo 'this is rs1' > /var/www/html/index.html
[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo 'this is rs2' > /var/www/html/index.html
[root@RS2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
在nginx上编辑配置文件
[root@nginx conf]# vi nginx.conf
upstream test.com { //添加在http里面
server 192.168.200.6 weight=3;
server 192.168.200.7 weight=1;
}
#gzip on;
server { //添加在server里面
location / {
proxy_pass http://test.com;
}
}
[root@nginx conf]# 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
[root@nginx conf]# nginx -s reload
[root@nginx conf]# for i in $(seq 10);do curl 192.168.200.5 ;done
this is rs1
this is rs1
this is rs2
this is rs1
this is rs1
this is rs1
this is rs2
this is rs1
this is rs1
this is rs1
动静分离实例:
环境:
安装服务 | 主机名 | IP |
---|---|---|
nginx | nginx | 192.168.200.5 |
httpd | RS1 | 192.168.200.7 |
lnmp | RS2 | 192.168.200.8 |
[root@nginx conf]# vi nginx.conf
upstream static {
server 192.168.200.7 weight=1;
}
upstream danamic {
server 192.168.200.8 weight=2;
}
#gzip on;
server {
location / {
proxy_pass http://static;
}
location ~ \.php$ {
proxy_pass http://danamic;
}
}
[root@nginx conf]# 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
[root@nginx conf]# nginx -s reload
nginx状况监控
环境:
安装服务 | 主机名 | IP |
---|---|---|
nginx、zabbix_agent | nginx | 192.168.200.6 |
lamp 、zabbx_server、 zabbix_agent | zabbix | 192.168.200.5 |
//开启状态界面
[root@nginx conf]# pwd
[root@nginx conf]# vi nginx.conf
/usr/local/nginx/conf
location /status {
stub_status on;
allow 192.168.200.0/24;
deny all;
}
[root@nginx conf]# 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
[root@nginx conf]# nginx -s reload
[root@nginx conf]# curl http://192.168.200.6/status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
[root@nginx conf]# curl http://192.168.200.6/status
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
[root@nginx conf]# curl http://192.168.200.6/status
Active connections: 1
server accepts handled requests
3 3 3
Reading: 0 Writing: 1 Waiting: 0
[root@nginx conf]# curl http://192.168.200.6/status
Active connections: 1
server accepts handled requests
4 4 4
配置监控脚本
[root@nginx ~]# mkdir /scripts
[root@nginx ~]# chown -R zabbix.zabbix /scripts/
[root@nginx ~]# ll -d /scripts/
drwxr-xr-x. 2 zabbix zabbix 6 Sep 5 12:49 /scripts/
[root@nginx ~]# cd /scripts/
[root@nginx scripts]# vi check_nginx.sh
#!/bin/bash
function handled()
{
status=$(curl -s http://192.168.200.6/status |awk 'NR==3{print $3}')
echo $status
}
function Reading()
{
status=$(curl -s http://192.168.200.6/status |awk 'NR==4{print $2}')
echo $status
}
function Writing()
{
status=$(curl -s http://192.168.200.6/status |awk 'NR==4{print $4}')
echo $status
}
case $1 in
Writing)
Writing
;;
handled)
handled
;;
Reading)
Reading
;;
*)
echo "$0 handled|Writing|Reading"
;;
esac
[root@nginx scripts]# chmod +x check_nginx.sh
[root@nginx ~]# cd /usr/local/etc/
[root@nginx etc]# vi zabbix_agentd.conf
UserParameter=check_nginx[*],/bin/bash /scripts/check_nginx.sh $1
[root@nginx etc]# pkill zabbix
[root@nginx etc]# zabbix_agentd
//测试
[root@zabbix ~]# zabbix_get -s 192.168.200.6 -k check_nginx[handled]
28
[root@zabbix ~]# zabbix_get -s 192.168.200.6 -k check_nginx[Reading]
0
[root@zabbix ~]# zabbix_get -s 192.168.200.6 -k check_nginx[Writing]
1
web界面配置
添加主机组
创建主机
添加监控项
查看最新数据