您的位置:首页 > 运维架构 > 反向代理

Linux安装配置反向代理Nginx

2018-01-09 15:12 981 查看

应用场景

对于一个大型网站来说,负载均衡是永恒的话题,顾名思义,负载均衡即是将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够快,给用户很好的体验。随着硬件技术的迅猛发展,越来越多的负载均衡硬件设备涌现出来,如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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: