51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

nginx 代理 websocker 4层转发

需求, 后端有websokcer 通信,

目标: 实现通过nginx 监听 8193 转发到后端。

主要参考 stream 模块,它跟http 是同一级别的。

user  nginx;
worker_processes  auto;

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


events {
    worker_connections  1024;
}


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;
    
    client_header_buffer_size 16k;
    large_client_header_buffers 4 32k;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/conf.d/uat/*.conf;
    include /etc/nginx/conf.d/uat2/*.conf;
    include /etc/nginx/conf.d/uat/one/*.conf;
 



}

stream {

    log_format basic '$remote_addr [$time_local] '
                     '$protocol $status $bytes_sent $bytes_received '
                     '$session_time';

    upstream tcp_proxy {
        hash $remote_addr consistent; #远程地址做个hash
        server 192.168.1.209:31005;
    }
    server {
        listen 8193;

        proxy_connect_timeout 1s; #后端链接空闲超时断开
        proxy_timeout 10s; #后端连接超时时间
        proxy_pass tcp_proxy;


   access_log  /var/log/nginx/uat-cloudservice2.kattgatt.com.log basic;
   #error_log  /var/log/nginx/uat-cloudservice2.kattgatt.com.error.log;
 }
}
赞(7)
未经允许不得转载:工具盒子 » nginx 代理 websocker 4层转发