赞
踩
1. nginx 1.12.2安装
安装nginx依赖包
nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法
yum -y install pcre pcre-devel
nginx的各种模块中需要使用gzip压缩
yum -y install zlib zlib-devel
安全套接字层密码库
yum -y install openssl openssl-devel
下载nginx包并解压
- cd /usr/local/src
- wget http://nginx.org/download/nginx-1.12.2.tar.gz
- tar -zxvf nginx-1.12.2.tar.gz
2. 安装nginx-kafka插件ngx_kafka_module
gitlab地址:https://github.com/brg-liuwei/ngx_kafka_module
安装git
yum install -y git
cd到/usr/local/src目录,将kafka的c客户端源码clone到本地
- git clone https://github.com/edenhill/librdkafka
- #建议直接下载好,解压https://github.com/edenhill/librdkafka
编译安装 librdkafka
- cd librdkafka
- yum install -y gcc gcc-c++ pcre-devel zlib-devel
- ./configure
- make && make install
将ngx_kafka_module编译到nginx(编译nginx,然后将插件同时编译)
- cd /usr/local/src
- git clone https://github.com/brg-liuwei/ngx_kafka_module
- cd /usr/local/src/nginx-1.12.2
- ./configure --add-module=/usr/local/src/ngx_kafka_module/
- make && make install
修改nginx.conf文件
- #nginx的安装目录下
- cd /usr/local/nginx/conf
- vim nginx.conf
- #修改或新增的配置
- http {
-
- # some other configs
-
- kafka;
-
- kafka_broker_list linux128:9092; # host:port ...
-
- server {
-
- # some other configs
- listen 80;
- server_name linux128;
-
- location = /kafka/test {
- # optional directive: kafka_partition [<partition-num> | auto]
- #
- # kafka_partition auto; # default value
- # kafka_partition 0;
- # kafka_partition 1;
-
- #允许cros跨域访问
- add_header 'Access-Control-Allow-Origin' '*';
- kafka_topic test;#kafka topic名称
- }
- }
- }

3.启动zk,kafka集群,创建test 主题
启动zk,kafka集群,略。
创建test 主题
kafka-topics.sh -zookeeper linux128:2181 --create --topic test --partitions 1 --replication-factor 1
4. 启动nginx
- cd /usr/local/nginx/sbin
- nginx
查看nginx进程
ps -ef | grep nginx
5. 测试是否向kafka发送数据成功
- curl localhost/kafka/test -d "message send to kafka topic"
-
- #or you can use -v or --verbose to see http request and response for detail:
-
- curl localhost/kafka/test -d "hello ngx_kafka_module" -v
开启客户端消费者验证
kafka-console-consumer.sh --zookeeper linux128:2181 --topic test --from-beginning
6. 配置过程报错解决
安装了Nginx之后,启动时报错,找不到kafka.so.1这个文件。
error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
解决办法:加载so库
#/etc/ld.so.conf这个是Linux上的文件,里面放的东西是指定Linux在启动时要加载的文件。
echo "/usr/local/lib" >> /etc/ld.so.conf
执行下面的命令使修改生效:
ldconfig
执行curl命令,返回404错误
常见原因,1.检查curl指定的链接是否错误。2.检查nginx.conf文件是否配置正确(修改的nginx.conf文件是安装目录下的,而不是源码目录下的)。
html页面埋点跨域问题
在nginx.conf中编辑:add_header 'Access-Control-Allow-Origin' '*';
- location = /kafka/test {
- #允许跨域访问
- add_header 'Access-Control-Allow-Origin' '*';
- kafka_topic test;#kafka topic名称
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。