编译安装
tar -zxvf openresty-1.13.6.1.tar.gz
./configure
make
make install
Openresty配置
按注释调整配置
nginx.conf
#user nobody;
## 1.工作进程数,与CPU逻辑核心数保持一致(***必须配置***)
worker_processes 8;
## 2. 提高TCP连接数(***必须配置***)
worker_rlimit_nofile 1000000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
## 3.采用epoll事件模型(***必须配置***)
use epoll;
## 4.提高TCP连接数(***必须配置***)
worker_connections 1000000;
}
## 6.最好删除该部分监听端口,保留该部分为了查看nginx是否正常运行
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 89;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
}
## 4.导入EMQX的集群配置文件
include stream.conf;
stream.conf
stream {
## 1. 访问日志,排查问题使用
log_format basic '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time $upstream_addr';
access_log logs/stream-access.log basic;
upstream emqx {
## 3.集群节点,可以配置多个,不用粘性会话,MQTT集群内是互通的,路由任意节#点都可以收到数据包
#server 192.168.3.24:1883 max_fails=3 fail_timeout=30s;
server 192.168.3.23:1883 max_fails=3 fail_timeout=30s;
}
server {
## 2.监听端口, so_keepalive必须配置,tcp保活时间控制2min,检测数据包间隔5s,重试3次,(MQTT的心跳包是1m发一个,保活时间间隔不得低于1m)
listen 90 so_keepalive=2m:2s:3;
proxy_connect_timeout 1s;
proxy_pass emqx;
}
}
Linux系统参数调整
需要设置nginx-->EMQX的端口连接上限,理论上nginx和EQM集群节点最多保持65535个连接,排除已被占用端口,预估可达到6W。
sysctl -w net.ipv4.ip_local_port_range="1024 65535"
# 所有进程最大打开文件数(2M)
sysctl -w fs.file-max=2097152
# 单进程最大可分配文件数(2M)
sysctl -w fs.nr_open=2097152
在/etc/security/limits.conf 添加,关闭当前会话,重新打开,否则不生效
* soft nofile 100000
* hard nofile 100000
端口的TCP监听队列长度,可能用于处理大量TCP连接时,队列溢出
sysctl -w net.core.somaxconn=65535
压测准备
Client(1台)--->Nginx(1台)--->EMQX(1台)网络环境,理论最大支持6W客户端在线
如果希望达到百万级别的环境:
准备Client(16台)--->Nginx(16台)---->EMQX(1台)
或者Client(16台)--->EMQX(1台)