Skip to content

Commit a45e146

Browse files
authored
Add ws_log_format message directive (#3)
* Add srv configuration structure
1 parent ffc8ee2 commit a45e146

File tree

5 files changed

+282
-248
lines changed

5 files changed

+282
-248
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ Nginx module developed for logging and displaying statistic of websocket proxy c
77

88
## Installation
99

10-
1. Configure nginx adding this module with:
10+
1. Configure nginx adding this module with or build this module as a dynamic module:
1111
```sh
1212
./configure (...) --add-module=./ngx_http_websocket_stat_module
13+
# or
14+
./configure (...) --add-dynamic-module=./ngx_http_websocket_stat_module && make modules
1315
```
1416
2. Build nginx with make -j<n> command where n is number of cpu cores on your build machine
1517

@@ -22,9 +24,10 @@ Nginx module developed for logging and displaying statistic of websocket proxy c
2224

2325
## Usage
2426

25-
To enable websocket logging specify log file in server section of nginx config file with ws_log directibe.
27+
To enable websocket frames logging specify `log_enabled on` and `ws_log_format` in server section of nginx config file. Additionally, specify `ws_log` to override the log file, which is used to log ws frames.
2628

27-
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.
29+
To customize connection open and close log messages use "open" and "close" parameter for ws_log_format directive.
30+
To log only when the full message is received/sent use "message" parameter for ws_log_format directive.
2831

2932
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.
3033

@@ -34,7 +37,8 @@ To set maximum single connection lifetime use ws_conn_age parameter. Argument is
3437
Here is a list of variables you can use in log format string:
3538

3639
* $ws_opcode - websocket packet opcode. Look into https://tools.ietf.org/html/rfc6455 Section 5.2, Base Framing Protocol.
37-
* $ws_payload_size - Websocket packet size without protocol specific data. Only data that been sent or received by the client
40+
* $ws_payload_size - Size of the WS frame without protocol specific data. Only data that been sent or received by the client
41+
* $ws_message_size - Size of the WS message without protocol specific data. Only data that been sent or received by the client
3842
* $ws_packet_source - Could be "client" if packet has been sent by the user or "upstream" if it has been received from the server
3943
* $ws_conn_age - Number of seconds connection is alive
4044
* $time_local - Nginx local time, date and timezone
@@ -48,8 +52,6 @@ Here is a list of variables you can use in log format string:
4852
* $server_port - Server's port
4953
* $upstream_addr - websocket backend address
5054

51-
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.
52-
5355
## Example of configuration
5456

5557
```
@@ -62,10 +64,6 @@ server
6264
ws_log_format close "$time_local: Connection closed";
6365
ws_max_connections 200;
6466
ws_conn_age 12h;
65-
# set up location for statistic
66-
location /websocket_status {
67-
ws_stat;
68-
}
6967
...
7068
}
7169

config

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
ngx_addon_name=ngx_http_websocket_stat_module
22

3-
ngx_module_type=HTTP
4-
ngx_module_name=ngx_http_websocket_stat_module
5-
ngx_module_srcs="$ngx_addon_dir/ngx_http_websocket_stat_module.c \
6-
$ngx_addon_dir/ngx_http_websocket_stat_format.c \
7-
$ngx_addon_dir/ngx_http_websocket_stat_frame_counter.c"
8-
. auto/module
3+
if test -n "$ngx_module_link"; then
4+
ngx_module_type=HTTP
5+
ngx_module_name=ngx_http_websocket_stat_module
6+
ngx_module_srcs="$ngx_addon_dir/ngx_http_websocket_stat_module.c \
7+
$ngx_addon_dir/ngx_http_websocket_stat_format.c \
8+
$ngx_addon_dir/ngx_http_websocket_stat_frame_counter.c"
9+
. auto/module
10+
else
11+
HTTP_MODULES="$HTTP_MODULES ngx_http_websocket_stat_module"
12+
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_websocket_stat_module"
13+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
14+
$ngx_addon_dir/ngx_http_websocket_stat_module.c \
15+
$ngx_addon_dir/ngx_http_websocket_stat_format.c \
16+
$ngx_addon_dir/ngx_http_websocket_stat_frame_counter.c"
17+
fi

ngx_http_websocket_stat_frame_counter.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ frame_counter_process_message(u_char **buffer, ssize_t *size,
3737
switch (frame_counter->stage) {
3838
case HEADER:
3939
frame_counter->current_frame_type = **buffer & 0x0f;
40+
frame_counter->fragment_final = **buffer >> 7;
41+
42+
if (frame_counter->current_frame_type != CONTINUATION) {
43+
frame_counter->current_message_size = 0;
44+
}
45+
4046
move_buffer(buffer, size, 1);
4147
frame_counter->stage = PAYLOAD_LEN;
4248
frame_counter->bytes_consumed =
@@ -102,6 +108,7 @@ frame_counter_process_message(u_char **buffer, ssize_t *size,
102108
}
103109
break;
104110
case PAYLOAD:
111+
frame_counter->current_message_size += *size;
105112
if (*size >= (u_int)(frame_counter->current_payload_size -
106113
frame_counter->bytes_consumed)) {
107114
move_buffer(buffer, size,

ngx_http_websocket_stat_frame_counter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ typedef enum { CONTINUATION, TEXT, BINARY, CLOSE = 8, PING, PONG } frame_type;
1515

1616
// Structure representing frame statistic and parsing stage
1717
typedef struct {
18-
ngx_int_t total_payload_size;
18+
ngx_int_t current_message_size;
1919

20-
// private fields representing current parcing stage
20+
// private fields representing current parsing stage
2121
ngx_int_t bytes_consumed;
2222
packet_reading_stage stage;
2323
char payload_masked : 1;
24+
char fragment_final : 1;
2425
frame_type current_frame_type;
2526
ngx_int_t current_payload_size;
2627
} ngx_frame_counter_t;

0 commit comments

Comments
 (0)