Nginx部署

EMQX 客服发表于:2022年03月16日 14:59:10更新于:2023年01月09日 16:50:05

一、前提准备

  1. 说明

    该文档以centos7系统进行安装nginx,安装需要使用root账号。

  2. openssl版本

    为了安全扫描漏洞,将openssl 升级到最新稳定版本 参考链接

    wget https://www.openssl.org/source/openssl-1.1.1c.tar.gz
  3. Nginx安装依赖

    可能操作系统缺少pcre-devel,需要使用命令:yum install pcre-devel 进行安装依赖。

  4. 安装包准备

    官网下载最新稳定版本:https://nginx.org/en/download.html

  5. 操作系统调优

    支持百万连接 参考链接;同时一个IP最多创建6W左右的长连接,nginx部署的服务器需要配置多个IP或者多台服务器安装nginx。

二、安装

  1. 解压nginx

    tar -xvf nginx-1.20.2.tar.gz
  2. 编译配置

    # cd nginx-1.20.2/
    # vi auto/lib/openssl/conf
    CORE_INCS="$CORE_INCS $OPENSSL/include"            
    CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"            
    CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"            
    CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"

    修改前:

    image.png

    修改后:

    image.png


  3. 执行命令

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx 
--conf-path=/usr/local/nginx/conf/nginx.conf 
--error-log-path=/usr/local/nginx/logs/error.log 
--http-log-path=/usr/local/nginx/logs/access.log 
--pid-path=/usr/local/nginx/logs/nginx.pid 
--lock-path=/usr/local/nginx/logs/nginx.lock 
--http-client-body-temp-path=/usr/local/nginx/temp/client_temp 
--http-proxy-temp-path=/usr/local/nginx/temp/proxy_temp 
--http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi_temp 
--http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi_temp 
--http-scgi-temp-path=/usr/local/nginx/temp/scgi_temp 
--with-threads --with-file-aio --with-http_gunzip_module 
--with-http_gzip_static_module --with-http_ssl_module 
--with-http_v2_module --with-http_realip_module 
--with-http_addition_module --with-http_sub_module 
--with-http_dav_module --with-http_flv_module 
--with-http_mp4_module --with-http_random_index_module 
--with-http_secure_link_module 
--with-http_stub_status_module 
--with-http_auth_request_module 
--with-http_slice_module 
--with-http_sub_module 
--with-stream 
--with-stream_ssl_module 
--with-openssl=/usr/local/openssl 
--with-ld-opt="-Wl,-E"

4.编译安装

image.png

三、配置

  1. 负载均衡策略

  2. 后端集群需要负载均衡时,可以有以下4种策略可以配置:

(1)轮询(默认):

    

        每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

        示例:

        upstream myserver {

        server 123.56.241.132:1883;

                server 123.56.241.139:1883;

        }

(2)weight:

    

        weight代表权重,默认为1,权重越高被分配的客户端越多。

        示例:

        upstream myserver {

             server 123.56.241.132:1883 down;

                server 123.56.241.139:1883 weight=3 max_fails=2 fail_timeout=2s backup;

        }

        down:表示单前的server暂时不参与负载。

        weight:默认为1,weight越大,负载的权重就越大。

        max_fails:允许请求失败的次数;默认为1。

        fail_timeout:失败超时时间,默认10s,max_fails次失败后,暂停的时间。

        backup:其它所有的非backup机器down或者忙的时候,请求backup机器

(3)ip_hash:

        每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

        示例:

        upstream myserver {

                ip_hash;

        server 123.56.241.132:1883;

                server 123.56.241.139:1883;

        }

(4)least_coon:

        最少连接数,哪个后端服务器连接数少就分发到哪个后端服务器。

        示例:

        upstream myserver {

                least_conn;

        server 123.56.241.132:1883;

                server 123.56.241.139:1883;

        }

3.nginx.conf基础调整   

        (1)worker_processes: 工作进程数。

        (2)worker_connections:单个工作进程可以允许同时建立外部连接的数量,数字越大能同时处理的连接数越多。

           image.png

 3.tcp配置

在nginx.conf中,与http同层中加入stream模块stream {    
                       log_format proxy '$remote_addr [$time_local] '                     
                                        '$protocol $status $bytes_sent $bytes_received '                     
                                        '$session_time "$upstream_addr" '                     
                                        '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';    
                                        access_log /usr/local/nginx/logs/tcp-access.log proxy;    
                                        upstream mqtt_tcp_server {        
                                            server 10.211.55.2:1883 max_fails=2 fail_timeout=10s weight=1;      #高可用均衡配置        
                                            #server 10.2.12.165:1883 max_fails=2 fail_timeout=10s weight=1;        
                                            #server 10.2.12.165:1883 max_fails=2 fail_timeout=10s weight=1;    
                                            }     #支持多IP    
                                            split_clients "$remote_addr$remote_port" $multi_ip {        
                                                    20% 10.211.55.5;        
                                                    20% 10.211.55.20;        
                                                    20% 10.211.55.21;        
                                                    20% 10.211.55.22;        
                                                    * 10.211.55.23;    }    
                                            server {        
                                                    listen 1883;        
                                                    proxy_bind $multi_ip;        
                                                    proxy_connect_timeout 10s;        
                                                    proxy_timeout 1800s; #默认心跳时间为10min;        
                                                    proxy_buffer_size 3M;        
                                                    tcp_nodelay on;        
                                                    proxy_pass mqtt_tcp_server;        
                                                    proxy_protocol on; #是否开启反向代理协议    
                                                    } 
                                                  }

 image.png

备注:如果proxy_protocol 设置为on,代表支持反向代理协议v1,emqx 对应的tcp监听器需要开启代理协议,emqx设置代理协议:

image.png

image.png

4.ssl配置

单向认证:

在tcp配置基础上添加证书相关参数

image.png

或者

image.png

备注:放在server 监听端口外,是代表所有的server只要是进行证书认证,都使用该ssl相关的配置。

双向认证:

在ssl单项认证基础上添加CA证书及开启验证客户端证书参数

ssl_client_certificate /usr/local/nginx/certs/ca.pem;

ssl_verify_client on;

ssl_verify_depth 1;

image.png


备注:nginx 需要同时支持单向认证和双向认证的监听端口,将ssl 相关的参数配置到对应监听端口模块下。

5.ws配置

在http 模块中添加    
    #支持多IP    
    split_clients "$remote_addr$remote_port" $multi_ip {        
            20% 10.211.55.5;        
            20% 10.211.55.20;        
            20% 10.211.55.21;        
            20% 10.211.55.22;        
            * 10.211.55.23;    
    }     
    upstream mqtt_websocket {        
            server 10.211.55.2:8083 max_fails=2 fail_timeout=10s weight=1;    
    }    
    server {        
            listen 8083;        
            location /mqtt {            
                    proxy_bind $multi_ip;            
                    proxy_redirect off;            
                    proxy_pass http://mqtt_websocket;            
                    proxy_connect_timeout 10s;            
                    # websocket连接有效时间,在该时间内没有数据交互的话websocket连接会自动断开,默认为60s            
                    proxy_send_timeout 3600s;            
                    proxy_read_timeout 3600s;            
                    proxy_http_version 1.1;            
                    # websocket连接的Upgrade必须设置为WebSocket,表示在取得服务器响应之后,使用HTTP升级将HTTP协议转换(升级)为WebSocket协议            
                    proxy_set_header Upgrade $http_upgrade;            
                    # websocket 的Connection必须设置为Upgrade,表示客户端希望连接升级            
                    proxy_set_header Connection "Upgrade";            
                    #反向代理真实IP            
                    proxy_set_header Host $host;            
                    proxy_set_header X-Real-IP $remote_addr;            
                    proxy_set_header REMOTE-HOST $remote_addr;            
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              
                    #禁用缓存             
                    proxy_buffering off;
                    }
                   }

image.png

备注:如果不需要反向代理真实IP端口,将proxy_set_header 4个配置删除。

6.wss配置

单向认证:

单项认证中需要在ws配置中加入服务端证书及ssl监听端口

image.png

或者

image.png

备注:放在server 监听端口外,是代表所有的server只要是进行证书认证,都使用该ssl相关的配置。

双向认证:

在wss 单项认证 配置中加入CA根证书及开启客户端验证参数。

image.png

备注:nginx 需要同时支持单向认证和双向认证的监听端口,将ssl 相关的参数配置到对应监听端口模块下。

7.监控模块(可选)

(1)检测with-http_stub_status_module模块是否安装

         image.png

(2)配置

location /ngx_satus {            
        stub_status on;            
        access_log off;            
        #allow 127.0.0.1;允许哪个ip可以访问       
      }

image.png

(3)检测是否生效

         通过浏览器访问http://{ip}/ngx_status

image.png


a) Active connections: 8 表示Nginx正在处理的活动连接数8个。

b) server  12 表示Nginx启动到现在共处理了12个连接

c) accepts 12 表示Nginx启动到现在共成功创建12次握手

d) handled requests 116 表示总共处理了 116 次请求

e) Reading:Nginx 读取到客户端的 Header 信息数

f) Writing:Nginx 返回给客户端 Header 信息数

g) Waiting:Nginx 已经处理完正在等候下一次请求指令的驻留链接(开启keep-alive的情况下,这个值等于Active-(Reading+Writing))


8.日志模块(可选)

定义nginx日志输出格式,格式可以自定义:

log_format proxy '$remote_addr [$time_local] '

                     '$protocol $status $bytes_sent $bytes_received '

                     '$session_time "$upstream_addr" '

                     '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

image.png

输出方式:

image.png

四、启停

验证配置文件时报错如下图:

是因为在nginx编译时,指定部分临时文件是放在temp下;需要创建temp目录。

image.png 

在/usr/local/nginx 下操作

1. 启动:./sbin/nginx

2. 重启:./sbin/nginx -s reload

3. 停止:./sbin/nginx -s stop

4. 验证配置文件是否正确:./sbin/nginx -t


    您需要登录后才可以回复