Giter VIP home page Giter VIP logo

ngx_http_websocket_stat_module's Introduction

Travis Build Status

NGINX module websocket connection and traffic statistics

Nginx module developed for logging and displaying statistic of websocket proxy connections traffic, limiting number of websocket connections and closing long lasting websocket connections.

Installation

  1. Configure nginx adding this module with:
       ./configure (...) --add-module=./ngx_http_websocket_stat_module
  1. Build nginx with make -j command where n is number of cpu cores on your build machine

Alternatively could be used build script shipped along with module: From module directory run

test/build_helper.py build

It would download and build nginx and all required libraries (openssl, pcre and zlib) and generate nginx configuration file.

Usage

To enable websocket logging specify log file in server section of nginx config file with ws_log directibe.

You can specify your own websocket log format using ws_log_format directive in server section. To customize connection open and close log messages use "open" and "close" parameter for ws_log_format directive.

Maximum number of concurrent websocket connections could be specified with ws_max_connections on server section. This value applies to whole connections that are on nginx. Argument should be integer representing maximum connections. When client tries to open more connections it recevies close framee with 1013 error code and connection is closed on nginx side. If zero number of connections is given there would be no limit on websocket connections.

To set maximum single connection lifetime use ws_conn_age parameter. Argument is time given in nginx time format (e.g. 1s, 1m 1h and so on). When connection's lifetime is exceeding specified value there is close websocket packet with 4001 error code generated and connection is closed.

Here is a list of variables you can use in log format string:

  • $ws_opcode - websocket packet opcode. Look into https://tools.ietf.org/html/rfc6455 Section 5.2, Base Framing Protocol.
  • $ws_payload_size - Websocket packet size without protocol specific data. Only data that been sent or received by the client
  • $ws_packet_source - Could be "client" if packet has been sent by the user or "upstream" if it has been received from the server
  • $ws_conn_age - Number of seconds connection is alive
  • $time_local - Nginx local time, date and timezone
  • $request - Http reqeust string. Usual looks like "GET /uri HTTP/1.1"
  • $uri - Http request uri.
  • $request_id - unique random generated request id.
  • $remote_user - username if basic authentification is used
  • $remote_addr - Client's remote ip address
  • $remote_port - Client's remote port
  • $server_addr - Server's remote ip address
  • $server_port - Server's port
  • $upstream_addr - websocket backend address

To read websocket statistic there is GET request should be set up at "location" location of nginx config file with ws_stat command in it. Look into example section for details.

Example of configuration


server
{
   ws_log <path/to/logfile>;
   ws_log_format "$time_local: packet of type $ws_opcode received from $ws_packet_source, packet size is $ws_payload_size";
   ws_log_format open "$time_local: Connection opened";
   ws_log_format close "$time_local: Connection closed";
   ws_max_connections 200;
   ws_conn_age 12h;
# set up location for statistic
   location /websocket_status {
      ws_stat;
   }
   ...
}

Copyright

This document is licensed under BSD-2-Clause license. See LICENSE for details.

The code has been opened by (c) Thomson Reuters. It is now maintained by (c) Refinitiv.

ngx_http_websocket_stat_module's People

Contributors

dmytro-rezchykov-tr avatar emmanuelcanaan avatar jonathanhuot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ngx_http_websocket_stat_module's Issues

cant compile with nginx 1.21.4

Hi, i am trying to compile with nginx 1.21.4 with this configure:
./configure (...) --add-module=./ngx_http_websocket_stat_module
configure runs normal, but 'make' return error:
make ....... -o objs/addon/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.o \ /opt/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.c In file included from /usr/include/string.h:494, from src/os/unix/ngx_linux_config.h:27, from src/core/ngx_config.h:26, from src/core/ngx_core.h:12, from /opt/ngx_http_websocket_stat_module/ngx_http_websocket_stat_format.h:18, from /opt/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.c:1: In function ‘memcpy’, inlined from ‘send_close_packet’ at /opt/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.c:674:5, inlined from ‘check_ws_age.isra.1.part.2’ at /opt/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.c:302:9, inlined from ‘check_ws_age.isra.1’ at /opt/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.c:296:1, inlined from ‘my_send’ at /opt/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.c:320:9: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34:10: error: ‘__builtin_memcpy’ forming offset 20 is out of the bounds [0, 19] [-Werror=array-bounds] return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors make[1]: *** [objs/Makefile:2079: objs/addon/ngx_http_websocket_stat_module/ngx_http_websocket_stat_module.o] Error 1 make[1]: Leaving directory '/opt/nginx-1.21.4' make: *** [Makefile:10: build] Error 2
How to fix this error?

Handling proxy_pass connections

I've installed and configured the plugin, however, my ws_log file stays empty even when I connect websocket clients to the server. My nginx server serves as a proxy to application on docker (proxy_pass directvie in location block). Does the plugin handle such configuration? If so, what is a possible cause for empty ws_log file? The file is created automatically, so I think my installation and configuration is correct. I am also able to reach ws_stat webpage on configured location - all stats are 0, even with connected clients.

$ nginx-v
nginx version: nginx/1.20.0

Usage with kubernetes/ingress-nginx

Hi!
I'm trying to compile custom dynamic module and use it with ingress-controller 0.44.0 (https://github.com/kubernetes/ingress-nginx/). I made a fork to slightly modify config file to compile it as a dynamic module. https://github.com/chainstack/ngx_http_websocket_stat_module

The resulting dynamic module works fine with pure nginx 1.19.6. But the problem is that when I'm trying to use with ingress-nginx image, it does nothing. No frames are logged and no errors appear:(

I know that it's not a problem of the module itself, but maybe you guys can advise me smth to look at.

This is the dockerfile I came up with to build an nginx with the dynamic module inside.

FROM k8s.gcr.io/ingress-nginx/controller:v0.44.0 as builder
USER root
WORKDIR /tmp
RUN apk add git openssl-dev pcre-dev zlib-dev libc-dev gcc make
RUN NGINX_VERSION=$(nginx -v 2>&1 | sed 's/nginx version: nginx\///') && \
    wget -qO- https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz | tar xvz && \
    mv nginx-${NGINX_VERSION} nginx
RUN git clone --depth 1 --branch v1.0.0 https://github.com/chainstack/ngx_http_websocket_stat_module.git && \
    cd ./nginx && \
    ./configure  --with-compat --add-dynamic-module=../ngx_http_websocket_stat_module && make modules
FROM k8s.gcr.io/ingress-nginx/controller:v0.44.0
COPY --from=builder /tmp/nginx/objs/ngx_http_websocket_stat_module.so /etc/nginx/modules

The way I was testing it is the following:

  1. I ran docker container from the image
  2. Added load_module /etc/nginx/modules/ngx_http_websocket_stat_module.so; to /etc/nginx/nginx.conf file
  3. Added an nginx service and location sections that accept websocket and proxies it to a dummy websocket backend.
  4. Finally added ws_log* directives to the added service.

P.S.
Maybe you guys know some other way that allows to log each time frame goes through websocket connection.

ws_log_format only for open and close connection operations

Hi All,
is it possible to configure the moduel to avoid the logging of all operations into ws_log_format like:

        ws_log /usr/local/nginx/logs/websocket.log;
        ws_log_format open "[open] $remote_addr $time_local $request";
        ws_log_format close "[close] $remote_addr $time_local $request $ws_conn_age";

?

Thanks,
Marcello

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.