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;
  }
}

反向代理和负载均衡配置实例:

基本环境:

hostnameIPservice
nginx192.168.177.132nginx
RS01192.168.177.148apache
RS02192.168.177.149apache

部署步骤

  • 基本网站
//需要关闭防火墙和selinux
[root@RS01 ~]# yum -y install httpd
[root@RS01 ~]# echo 'test 01' > /var/www/html/index.html
[root@RS01 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS02 ~]# yum -y install httpd
[root@RS02 ~]# echo 'test 02' > /var/www/html/index.html
[root@RS02 ~]# 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.177.148 weight=3;
        server 192.168.177.149 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.177.132 ;done
test 01
test 02
test 01
test 02
test 01
test 02
test 01
test 02

动静分离实例:

基本环境:

hostnameIPservice
nginx192.168.177.132nginx
RS01192.168.177.148httpd
RS03192.168.177.150lnmp

部署步骤:

[root@nginx conf]# vi nginx.conf
upstream static {
        server 192.168.177.149 weight=1;
    }
    upstream danamic {
        server 192.168.177.150 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状况监控

基本环境:

hostnameIPservice
nginx192.168.177.148nginx、zabbix_agent
zabbix192.168.177.149lamp 、zabbx_server、 zabbix_agent

部署步骤:

  • 开启状态界面

[root@nginx conf]# pwd
[root@nginx conf]# vi nginx.conf
/usr/local/nginx/conf
location /status {
            stub_status on;
            allow 192.168.177.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.177.148/status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 
[root@nginx conf]# curl http://192.168.177.148/status
Active connections: 1 
server accepts handled requests
 2 2 2 
Reading: 0 Writing: 1 Waiting: 0 
[root@nginx conf]# curl http://192.168.177.148/status
Active connections: 1 
server accepts handled requests
 3 3 3 
Reading: 0 Writing: 1 Waiting: 0 
[root@nginx conf]# curl http://192.168.177.148/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.177.148/status |awk 'NR==3{print $3}')
    echo $status
}

function Reading()
{
    status=$(curl -s http://192.168.177.148/status |awk 'NR==4{print $2}')
    echo $status
}

function Writing()
{
    status=$(curl -s http://192.168.177.148/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.177.148 -k check_nginx[handled]
28
[root@zabbix ~]# zabbix_get -s 192.168.177.148 -k check_nginx[Reading]
0
[root@zabbix ~]# zabbix_get -s 192.168.177.148 -k check_nginx[Writing]
1
  • web界面配置
    在这里插入图片描述

    • 添加主机组
      [图片]

    • 创建主机
      [图片]

    • 添加监控项
      [图片]

    • 查看最新数据
      [图片]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值