logstash nginx代理配置

logstash nginx代理配置

  • http 协议 ; tcp 7层
http {
        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;
    tcp_nodelay         on;
     keepalive_timeout   65;
    types_hash_max_size 12048;

    client_max_body_size 20M;
    large_client_header_buffers 4 128k;
    client_header_buffer_size 128k;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;


####

upstream logstash {
      server 0.0.0.0:5001;
}

server{
    listen 8081;
    server_name _;
    location / {
        proxy_pass         http://127.0.0.1:5001;
        proxy_set_header Connection "";
        proxy_set_header Content-Type  "application/json;charset=utf-8";

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

###
}

tcp 4层

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


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



    upstream backend {
         hash $remote_addr consistent;

        server 0.0.0.0:5001;
    }
    server {
        listen 8081 ;
        proxy_pass 127.0.0.1:5001;
        proxy_bind 127.0.0.1 ;# $remote_addr;
        proxy_timeout 1s;
        proxy_responses 1;

        access_log /var/log/nginx/logtest_access.log proxy;
        error_log /var/log/nginx/logtest_error.log;

    }
}

  • tcp数据格式

  • 四层代理不能穿透客户端ip: 原因就是,这个指令会将代理出去发向被反向代理的数据包修改目标IP为被反向代理的服务器,保留源IP不变。但被反向代理的服务器回复数据包时目标IP也就是真实的客户端IP了,这是不行的,必须在被代理服务器上或者被代理服务器的网关上设置路由表将回包的目标IP改为Nginx服务器。https://www.oschina.net/question/3515598_2243795?sort=time

  • http数据请求格式

其他

  • 查看nginx安装模块
nginx -V 2>&1 | grep -- 'stream_realip_module'