Nginx 负载均衡 of TCP and HTTP protocol for EMQX

今天因为要配置EMQX负载均衡,涉及到TCP以及HTTP的负载均衡

  1. 外网TCP直连1885端口,转发到内网的192.168.1.101:1884和192.168.1.102:1884端口,比如例子中的tcpA.abc.com
  2. 外网HTTP访问80端口,转发到内网的192.168.1.101:18083和192.168.1.102:18083端口,比如例子中的web.abc.com, passHost1
  3. 外网HTTP访问8090端口,转发到内网的192.168.1.101:8080和192.168.1.102:8080端口,比如例子中的api.abc.com, passHost2
  4. 核心点是TCP的配置要在stream里面,HTTP的配置要在http里面,其它的注意监听端口,以及转发端口

附完整的类似配置文件:

load_module /usr/lib64/nginx/modules/ngx_stream_module.so;
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}
stream {
      upstream tcpA.abc.com {
          server 192.168.1.101:1884 max_fails=3 fail_timeout=30s;
          server 192.168.1.102:1884 max_fails=3 fail_timeout=30s;
      }

      server {
          listen 1885;
          proxy_pass tcpA.abc.com;
      }

}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    upstream passHost2 {
        server 192.168.1.101:8080 max_fails=3 fail_timeout=30s;
        server 192.168.1.102:8080 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 8090;
        server_name api.abc.om;
        location / {
            proxy_pass http://passHost2;
        }
    }


    upstream passHost1 {
        server 192.168.1.101:18083 max_fails=3 fail_timeout=30s;
        server 192.168.1.102:18083 max_fails=3 fail_timeout=30s;
    }
    server {
        listen       80 ;
        server_name  web.abc.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://passHost1;
            index index.html index.php index.htm;
	       }
        location /Nginxstatus {
           stub_status on;
           auth_basic "NginxStatus";
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        location ~ \.php {
            root /usr/share/nginx/html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值