Nginx 4层反向代理

L112

是基于TCP

Nginx 4层反向代理_第1张图片

Nginx 4层反向代理_第2张图片

Nginx 4层反向代理_第3张图片

Nginx 4层反向代理_第4张图片

Nginx 4层反向代理_第5张图片

Nginx 4层反向代理_第6张图片

Nginx 4层反向代理_第7张图片

 

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!\n';
    }

    server {
        listen 10003 proxy_protocol;
        return '10003 server get ip: $remote_addr!\n';
        }

    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
';
    }
}

 

转载于:https://www.cnblogs.com/jackey2015/p/10566415.html

你可能感兴趣的:(Nginx 4层反向代理)