Linux安装配置反向代理Nginx

简介: 应用场景 对于一个大型网站来说,负载均衡是永恒的话题,顾名思义,负载均衡即是将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够快,给用户很好的体验。

应用场景

对于一个大型网站来说,负载均衡是永恒的话题,顾名思义,负载均衡即是将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够快,给用户很好的体验。随着硬件技术的迅猛发展,越来越多的负载均衡硬件设备涌现出来,如F5 BIG-IP、Citrix NetScaler、Radware等等,虽然可以解决问题,但其高昂的价格却往往令人望而却步,因此负载均衡软件仍然是大部分公司的不二之选。Nginx作为webserver的后起之秀,其优秀的反向代理功能和灵活的负载均衡策略受到了业界广泛的关注。

Nginx进程基于Master + Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能。在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性。

Keepalived是Linux下面实现VRRP 备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接。二者结合,可以构架出比较稳定的软件负载均衡方案。

操作步骤

1. Nginx安装配置

1.1 系统基础配置

关闭selinux
 # vi /etc/selinux/config
……
SELINUX=disabled
……

关闭防火墙,在Centos6.5中
 # service iptables stop
 # chkconfig iptables off

关闭防火墙,在Centos7中
 # systemctl stop firewalld
 # systemctl disable firewalld

修改系统文件打开限制数量,增加在配置文件最后
 # vi /etc/security/limits.conf
* soft noproc 65535
* hard noproc 65535
* soft nofile 409600
* hard nofile 409600

重启机器
 # reboot

1.2 设置时间同步

安装ntpdate , ntpd
 # yum install -y ntpdate ntp pcre-devel pcre

复制时区至本地时区
 # cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

时间同步,ip请改成可用的时间服务器的ip地址,并写入时间戳
 # ntpdate ip  
 # hwclock -w

开启ntpd服务,在Centos6中
 # service start ntpd
 # service enable ntpd

开启ntpd服务,在Centos7中
 # systemctl start ntpd
 # systemctl enable ntpd

1.3 安装软件包

安装包下载地址
http://pan.baidu.com/s/1hrUHlOw
安装依赖组件
 # yum install zlib zlib-devel
 # yum install pcre-devel

将安装包中的文件拷贝到服务器,并解压缩安装包
 # tar -zxvf nginx-1.8.1.tar.gz

然后进入目录进行安装
 # cd nginx-1.8.1
 #./configure --prefix=/opt/nginx1.8.1
 # make
 # make install
 # ln  -sf  /opt/nginx1.8.1  /usr/local/nginx
 # echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile && source /etc/profile

1.4 配置服务

创建启动脚本
将启动脚本文件夹中的nginx文件发送到服务器中并
 # cp nginx  /etc/init.d/
 # chmod +x /etc/init.d/nginx

修改用户权限
 # useradd -r -M nginx
 # mkdir  -p  /var/log/nginx
 # chown nginx  -R /var/log/nginx

注册启动服务
 # chmod a+x /etc/init.d/nginx
 # chkconfig --add nginx
 # chkconfig nginx on

1.5 启动服务

 # service nginx start

2. 配置高可用

注:在主备两台Nginx服务器上都要安装Keepalived。

2.1 安装Keepalived

安装依赖包,将keepalived6(Centos6环境中) 或 keepalived7(Centos7中)传送到服务器
 # tar zxvf keepalived6.tar.gz     或tar zxvf keepalived7.tar.gz
 # yum localinstall keepalived/*.rpm -y

将软件源中的keepalived-1.2.16.tar.gz传送到服务器并解压
 # tar zxvf keepalived-1.2.16.tar.gz

编译安装
 # cd keepalived-1.2.16
 # ./configure
 # make
 # make install

配置服务
 # cp /usr/local/sbin/keepalived /usr/sbin/
 # cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
 # cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
 # chmod +x /etc/init.d/keepalived
 # chkconfig --add keepalived
 # chkconfig keepalived on
 # mkdir /etc/keepalived

2.2 配置主服务器

创建配置文件
 # vi /etc/keepalived/keepalived.conf

修改配置文件并保存
! Configuration File for keepalived
global_defs {
   notification_email {
     #abc@example.com
   }
   #notification_email_from admin@example.com
   #smtp_server smtp.example.com
   #smtp_connect_timeout 30
   router_id nginx_master
}
vrrp_script chk_http_port {
    script "</dev/tcp/127.0.0.1/80" #监控本地Nginx端口
    interval 1
    weight -10
}

vrrp_instance VI_1 {
    state MASTER #主服务器
    interface eth0 #通信网卡,根据实际配置
    virtual_router_id 51 #路由标识,同网段内不可冲突且需与备用服务器一致
    priority 100 #优先级,0-254
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass qwe123!@#
    }

    virtual_ipaddress {
        192.168.202.200 #虚拟IP,可定义多个
    }

    track_script {
        chk_http_port
    }
}

启动服务
 # service keepalived start

2.3 配置备用服务器

创建配置文件
 # vi /etc/keepalived/keepalived.conf

修改配置文件并保存
! Configuration File for keepalived
global_defs {
   notification_email {
     #abc@example.com
   }
   #notification_email_from admin@example.com
   #smtp_server smtp.example.com
   #smtp_connect_timeout 30
   router_id nginx_backup
}

vrrp_script chk_http_port {
    script "</dev/tcp/127.0.0.1/80" #监控本地Nginx端口
    interval 1 #执行间隔
    weight -10 #执行失败,服务优先级-10
}

vrrp_instance VI_1 {
    state BACKUP #备用服务器
    interface eth0 #通信网卡,根据实际配置
    virtual_router_id 51 #路由标识,需与主服务器一致,同网段内不可冲突
    priority 99 #优先级,比主服务器要低,0-254
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass qwe123!@#
    }

    virtual_ipaddress {
        192.168.202.200 #虚拟IP,可定义多个
    }

    track_script {
        chk_http_port
    }
}

启动服务
 # service keepalived start

2.4 安装验证

浏览器访问http://ip出现以下提示说明nginx正常运行

这里写图片描述

查看keepalived绑定虚拟ip的情况
 # ip a
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
10天前
|
安全 Ubuntu Java
Linux配置使用篇
Linux配置使用篇
|
17天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
23 0
|
22天前
|
存储 负载均衡 索引
linux7安装elasticsearch-7.4.0集群配置
linux7安装elasticsearch-7.4.0集群配置
109 0
|
27天前
|
监控 Linux Shell
【Shell 命令集合 网络通讯 】Linux 配置和管理网络流量的形状 shapecfg命令 使用指南
【Shell 命令集合 网络通讯 】Linux 配置和管理网络流量的形状 shapecfg命令 使用指南
36 0
|
27天前
|
网络协议 Shell Linux
【Shell 命令集合 网络通讯 】Linux 设置和配置PPP pppsetup命令 使用教程
【Shell 命令集合 网络通讯 】Linux 设置和配置PPP pppsetup命令 使用教程
34 0
|
27天前
|
缓存 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 配置DNS dnsconf 命令 使用教程
【Shell 命令集合 网络通讯 】Linux 配置DNS dnsconf 命令 使用教程
38 0
|
28天前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
38 0
|
27天前
|
域名解析 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
49 1
|
27天前
|
存储 Shell Linux
【Shell 命令集合 系统管理 】Linux 修改用户的属性和配置 usermod命令 使用指南
【Shell 命令集合 系统管理 】Linux 修改用户的属性和配置 usermod命令 使用指南
30 1
|
16天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
78 0