概述
Nginx
一. 安装部署nginx(单机版)
1.上传nginx安装包
2.解压nginx
[ ] tar -zxvf nginx-1.12.2.tar.gz -C /usr/local/src/
3.进入到nginx的源码目录
[ ] cd /usr/local/src/nginx-1.12.2/
4.预编译
[ ] ./configure
5.安装gcc编译器
[ ] yum -y install gcc pcre-devel openssl openssl-devel
6.然后再执行
[ ] ./configure
7.编译安装nginx(默认安装在/usr/local/nginx位置)
[ ] make && make install
8.启动nginx
[ ] cd /usr/local/nginx
启动: sbin/nginx
帮助: sbin/nginx -h
重新加载: sbin/nginx -s reload
9.查看nginx进程
[ ] ps -ef | grep nginx
[ ] netstat -anpt | grep nginx
10.web访问192.168.1.191查看是否启动成功
将springboot程序部署在多台服务器上,然后启动springboot
[ ] java -jar niubike-0.0.1-SNAPSHOT.war >> ./logs 2>&1 &
11.修改nginx的配置文件,让nginx实现负载均衡功能
[ ] vi /usr/local/nginx/conf/nginx.conf
nginx.conf中需要修改的部分:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
#响应数据的来源
upstream tomcats {
server node6:8888 weight=1;
server node7:8888 weight=1;
server node8:8888 weight=1;
}
server {
listen 80;
server_name node-5.xiaoniu.com;
location ~ .* {
proxy_pass http://tomcats;
}
}
}
二.keepalived实现高可靠(HA)
HA(High Available), 高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节点及备用节点。
高可靠软件keepalived
keepalive是一款可以实现高可靠的软件,通常部署在2台服务器上,分为一主一备。Keepalived可以对本机上的进程进行检测,一旦Master检测出某个进程出现问题,将自己切换成Backup状态,然后通知另外一个节点切换成Master状态。
keepalived安装
#下载:keepalived官网
#将keepalived解压到/usr/local/src目录下
[ ] tar -zxvf keepalived-1.3.6.tar.gz -C /usr/local/src
#进入到/usr/local/src/keepalived-1.3.6目录
[ ] cd /usr/local/src/keepalived-1.3.6
#开始configure
[ ] ./configure
#编译并安装
[ ] make && make install
–将keepalived添加到系统服务中
#拷贝执行文件
[ ] cp /usr/local/sbin/keepalived /usr/sbin/
#将init.d文件拷贝到etc下,加入开机启动项
[ ] cp /usr/local/src/keepalived-1.3.6/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
#将keepalived文件拷贝到etc下
[ ] cp /usr/local/src/keepalived-1.3.6/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#创建keepalived文件夹
[ ] mkdir -p /etc/keepalived
#将keepalived配置文件拷贝到etc下
[ ] cp /usr/local/src/keepalived-1.3.6/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#添加可执行权限
[ ] chmod +x /etc/init.d/keepalived
#添加keepalived到开机启动
[ ] chkconfig --add keepalived
[ ] chkconfig keepalived on
–配置keepalived虚拟IP
#MASTER节点
global_defs {
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.10/24
}
}
#BACKUP节点
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.10/24
}
}
#分别启动两台机器上的keepalived
[ ] service keepalived start
–配置keepalived心跳检查
#MASTER节点
global_defs {
}
vrrp_script chk_health {
script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
interval 1
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress {
10.0.0.10/24
}
notify_master "/usr/local/keepalived/sbin/notify.sh master"
notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}
#添加切换通知脚本
vi /usr/local/keepalived/sbin/notify.sh
#!/bin/bash
case "$1" in
master)
/usr/local/nginx/sbin/nginx
exit 0
;;
backup)
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
exit 0
;;
fault)
/usr/local/nginx/sbin/nginx -s stop
exit 0
;;
*)
echo 'Usage: notify.sh {master|backup|fault}'
exit 1
;;
esac
#添加执行权限
chmod +x /usr/local/keepalived/sbin/notify.sh
global_defs {
}
vrrp_script chk_health {
script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress {
10.0.0.10/24
}
notify_master "/usr/local/keepalived/sbin/notify.sh master"
notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}
#在第二台机器上添加notify.sh脚本
#分别在两台机器上启动keepalived
[ ] service keepalived start
[ ] chkconfig keepalived on
三. nginx整合kafka
0.1.上传nginx安装包
0.2.解压nginx
[ ] tar -zxvf nginx-1.12.2.tar.gz -C /usr/local/src/
1.安装git
[ ] yum install -y git
2.切换到/usr/local/src目录,然后将kafka的c客户端源码clone到本地(可直接访问连接下载librdkafka-master.zip)
[ ] cd /usr/local/src
[ ] git clone https://github.com/edenhill/librdkafka
3.进入到librdkafka,然后进行编译
[ ] cd librdkafka
[ ] yum install -y gcc gcc-c++ pcre-devel zlib-devel
[ ] ./configure
[ ] make && make install
4.安装nginx整合kafka的插件,进入到/usr/local/src,clone nginx整合kafka的源码(可直接访问连接下载ngx_kafka_module.zip)
[ ] cd /usr/local/src
[ ] git clone https://github.com/brg-liuwei/ngx_kafka_module
5.进入到nginx的源码包目录下 (编译nginx,然后将将插件同时编译)
[ ] cd /usr/local/src/nginx-1.12.2
[ ] ./configure --add-module=/usr/local/src/ngx_kafka_module/
[ ] make
[ ] make install
6.修改nginx的配置文件,详情请查看当前目录的nginx.conf
[ ] vi /usr/local/nginx/conf/nginx.conf
重要部分:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
kafka;
kafka_broker_list node6:9092 node7:9092 node8:9092;
server {
listen 80;
server_name node-6.xiaoniu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
备注:向/kafka/track该路径下写入信息,nginx会将写入的信息写到kafka的track topic下
location = /kafka/track {
kafka_topic track;
}
location = /kafka/user {
kafka_topic user;
}
location = /kafka/record{
kafka_topic record;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html; }}}
7.启动zk和kafka集群(创建topic)
[ ] /root/hadoop/zookeeper-3.4.6/bin/zkServer.sh start
[ ] /root/hadoop/kafka_2.11-0.10.2.1/bin/kafka-server-start.sh -daemon /root/hadoop/kafka_2.11-0.10.2.1/config/server.properties
8.启动nginx,报错,找不到kafka.so.1的文件
[ ] error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
9.加载so库
[ ] echo “/usr/local/lib” >> /etc/ld.so.conf
[ ] ldconfig
10.测试,向nginx中写入数据,然后观察kafka的消费者能不能消费到数据
[ ] curl localhost/kafka/track -d “message send to kafka topic”
[ ] curl localhost/kafka/track -d “老赵666”
11.出现的问题:
启动报错,80端口被占用
[ ] nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解决:
[ ] killall -9 nginx
[ ] /usr/local/nginx/sbin/nginx
最后
以上就是精明外套为你收集整理的Nginx环境搭建的全部内容,希望文章能够帮你解决Nginx环境搭建所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复