• Nginx 4层反向代理


    L112

    是基于TCP

    POST_ACCEPT阶段 在建立连接后所做的事情 

    PREACCESS阶段  limit_conn 限流 与HTTP类似

    ACCESS阶段 类似HTTP模块用于控制访问权限

    SSL阶段  ssl请求

    PREREAD阶段 ssl_preread 从头部取信息

    CONTENT阶段 return,stream_proxy

    LOG阶段 类似http log阶段

    Syntax: stream { ... } 提供指定流服务器指令的配置文件上下文
    Default:
    Context: main
    Syntax: server { ... }
    Default:
    Context: stream
    Syntax: listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
    Default:
    Context: server

    模块 ngx_stream_return_module

    Syntax: return value;
    Default:
    Context: server

    需要编译进Nginx

    http://nginx.org/en/docs/stream/ngx_stream_core_module.html

    可以用 telnet 192.168.1.1 10004 方式查看数据

    worker_processes auto;
    
    error_log /var/log/nginx/error.log info;
    
    events {
        worker_connections  1024;
    }
    
    stream {
        upstream backend {
            hash $remote_addr consistent;
    
            server backend1.example.com:12345 weight=5;
            server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
            server unix:/tmp/backend3;
        }
    
        upstream dns {
           server 192.168.0.1:53535;
           server dns.example.com:53;
        }
    
        server {
            listen 12345;
            proxy_connect_timeout 1s;
            proxy_timeout 3s;
            proxy_pass backend;
        }
    
        server {
            listen 127.0.0.1:53 udp reuseport;
            proxy_timeout 20s;
            proxy_pass dns;
        }
    
        server {
            listen [::1]:12345;
            proxy_pass unix:/tmp/stream.socket;
        }
    }

    代码示列

    stream {
        log_format basic '$remote_addr [$time_local] '
                     '$protocol $status $bytes_sent $bytes_received '
                     '$session_time';
    
        error_log logs/stream_error.log debug;
        access_log logs/stream_access.log basic;
        server {
            listen 10002 proxy_protocol;
            return '10002 server get ip: $remote_addr!
    ';
        }
    
        server {
            listen 10003 proxy_protocol;
            return '10003 server get ip: $remote_addr!
    ';
            }
    
        server {
            #listen 10004;
            listen 10004 proxy_protocol;
            set_real_ip_from 127.0.0.1;
            allow 202.112.144.236;
            deny all;
            return '10004 vars:
    bytes_received: $bytes_received
    bytes_sent: $bytes_sent
    proxy_protocol_addr: $proxy_protocol_addr
    proxy_protocol_port: $proxy_protocol_port
    remote_addr: $remote_addr
    remote_port: $remote_port
    realip_remote_addr: $realip_remote_addr
    realip_remote_port: $realip_remote_port
    server_addr: $server_addr
    server_port: $server_port
    session_time: $session_time
    status: $status
    protocol: $protocol
    ';
        }
    }
  • 相关阅读:
    13.字符串压缩算法
    12.字符串全部替换指定文本
    46.数字到字符串的转换
    45.切割字符串并精确分配内存
    44.字符串删除指定字符或者字符串
    11.表达式计算对一串加减乘除带括号进行计算
    43.可变参数实现printf
    关于float与double区别
    float,double与long long哪个更大?
    double 与0比较时有个精度问题,有时需精确到小数点后面几位,例如与>0.0001,而不能与>0比较
  • 原文地址:https://www.cnblogs.com/jackey2015/p/10566415.html
Copyright © 2020-2023  润新知