├── LICENSE
├── README.md
├── README
├── client-wss.jpg
└── nginx-wss.jpg
├── config
├── nginx_test
├── README.md
├── test_websocket.html
└── websocket_test.conf
├── ngx_http_set_header.c
├── ngx_http_set_header.h
├── ngx_websocket.c
├── ngx_websocket.h
├── ngx_websocket_handler.c
├── ngx_websocket_module.c
└── t
├── config
└── ngx_websocket_echo_module.c
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2020, pingostack
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | 1. Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | 2. Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | 3. Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # NGINX-based Websocket Server
2 |
3 | ## nginx-websocket-module
4 |
5 | ### Project blog
6 |
7 | [http://pingos.io/](http://pingos.io/)
8 |
9 | * wss client
10 | 
11 |
12 | * nginx-websocket-module supports wss protocol
13 | 
14 |
15 | ## Code sample
16 |
17 | **If you want to know how to develop a websocket server, refer to the code in the ['t/ngx_websocket_echo_module.c'](t/ngx_websocket_echo_module.c) .**
18 |
19 | ## Build
20 |
21 | ```shell
22 | $
23 | $ git clone https://github.com/nginx/nginx.git
24 | $
25 | $ git clone https://github.com/im-pingo/nginx-websocket-module.git
26 | $
27 | $ cd nginx
28 | $
29 | $ ./auto/configure --add-module=../nginx-websocket-module --add-module=../nginx-websocket-module/t
30 | $
31 | $ sudo make && sudo make install
32 | $
33 | ```
34 |
35 | ## Config file
36 |
37 | ### websocket
38 |
39 | * *syntax* : websocket [any]
40 |
41 | * *context*: location
42 |
43 | **The switch of websocket service has no args**
44 |
45 | ```nginx
46 |
47 | websocket out_queue=[num] message_length=[num] frame_length=[num] ping_interval=[time] timeout=[time];
48 |
49 | ```
50 |
51 | #### options
52 |
53 | ##### out_queue
54 |
55 | * *syntax*: out_queue=[num] (default 512)
56 | * *context*: websocket's arg
57 |
58 | **Number of out queue**
59 |
60 | ##### message_length
61 |
62 | * *syntax*: message_length=[num] (default 4096000 bytes)
63 | * *context*: websocket's arg
64 |
65 | **Max length of websocket message**
66 |
67 | ##### ping_interval
68 |
69 | * *syntax*: ping_interval=[msec] (default 5000ms)
70 | * *context*: websocket's arg
71 |
72 | **Time interval between pings**
73 |
74 | ##### timeout
75 |
76 | * *syntax*: timeout=[msec] (default 15000ms)
77 | * *context*: websocket's arg
78 |
79 | **receive timeout**
80 |
81 | ### websocket_echo
82 |
83 | * *syntax*: websocket_echo [no args]
84 | * *context*: location
85 |
86 | **The server responses the data it received**
87 |
88 | ```nginx
89 |
90 | websocket_echo;
91 |
92 | ```
93 |
94 | ### Example nginx.conf
95 |
96 | ```nginx
97 |
98 | daemon on;
99 | master_process on;
100 | #user nobody;
101 | worker_processes 1;
102 |
103 | error_log logs/error.log info;
104 |
105 | pid logs/nginx.pid;
106 | events {
107 | worker_connections 1024;
108 | }
109 |
110 |
111 | http {
112 | include mime.types;
113 | default_type application/octet-stream;
114 | sendfile on;
115 | keepalive_timeout 65;
116 |
117 | server {
118 | listen 80;
119 |
120 | # use wss(ssl)
121 | listen 443 ssl;
122 | ssl_certificate /usr/local/nginx/key/im-pingo.crt;
123 | ssl_certificate_key /usr/local/nginx/key/im-pingo.key;
124 |
125 | server_name localhost;
126 |
127 | location /im-pingo {
128 | websocket out_queue=512 message_length=4096000 ping_interval=5000ms timeout=15s;
129 | websocket_echo;
130 | }
131 | }
132 | }
133 |
134 | ```
135 |
--------------------------------------------------------------------------------
/README/client-wss.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pingostack/nginx-websocket-module/207993fbe44f133ef832dc1b7f9a0d2751171341/README/client-wss.jpg
--------------------------------------------------------------------------------
/README/nginx-wss.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pingostack/nginx-websocket-module/207993fbe44f133ef832dc1b7f9a0d2751171341/README/nginx-wss.jpg
--------------------------------------------------------------------------------
/config:
--------------------------------------------------------------------------------
1 | ngx_addon_name="ngx_websocket_module"
2 | WEBSOCKET_MODULES=" \
3 | ngx_websocket_module \
4 | "
5 | WEBSOCKET_SRCS=" \
6 | $ngx_addon_dir/ngx_websocket_module.c \
7 | $ngx_addon_dir/ngx_http_set_header.c \
8 | $ngx_addon_dir/ngx_websocket_handler.c \
9 | $ngx_addon_dir/ngx_websocket.c \
10 | "
11 |
12 | WEBSOCKET_DEPS=" \
13 | #ngx_addon_dir/ngx_http_set_header.h \
14 | #ngx_addon_dir/ngx_websocket.h \
15 | "
16 | HTTP_MODULES="$HTTP_MODULES $WEBSOCKET_MODULES"
17 |
18 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $WEBSOCKET_SRCS"
19 |
20 | USE_OPENSSL=YES
21 |
22 | WEBSOCKET_LIBS=""
23 |
24 | NGX_ADDON_DEPS="$NGX_ADDON_DEPS $WEBSOCKET_DEPS"
25 |
26 | CORE_LIBS="$CORE_LIBS $WEBSOCKET_LIBS"
27 | CFLAGS="$CFLAGS -I $ngx_addon_dir"
28 |
--------------------------------------------------------------------------------
/nginx_test/README.md:
--------------------------------------------------------------------------------
1 | # Nginx Test
2 | ## Browser-based Integration Test for Nginx Websocket
3 |
4 | This test exercises the websocket logic using the websocket client implementation native to modern browsers.
5 | In the test, we start an instance of Nginx with the websocket echo module loaded, then navigate using any
6 | modern browser to a web page containing javascript code that connects to that websocket and validates
7 | correct operation
8 |
9 | ### Quick Start for Linux using Bash shell
10 | Since there are many ways that the websocket modules could be compiled, this is less quick that we'd like.
11 | * Create a working directory - replace ${MY_HOME_OR_LOCAL_SCRATCH} with an appropriate directory:
12 | > cd ${MY_HOME_OR_LOCAL_SCRATCH}
13 | > mkdir nginx_temp
14 | > cd nginx_temp
15 | * Copy test files into working directory
16 | > cp ${PATH_TO_NGINX_WEBSOCKET_GIT_REPO}/nginx-test/* .
17 | * Symlink websocket modules into working directory (you could also copy, but symlinks mean you pick up changes following fixes)
18 | > ln -s ${PATH_TO_MODULES}/ngx_websocket_echo_module.so .
19 | > ln -s ${PATH_TO_MODULES}/ngx_websocket_module.so .
20 | > ln -s ${PATH_TO_NGINX_CONF}/mime.types .
21 | * Run Nginx
22 | > ${PATH_TO_NGINX}/nginx -p $(pwd) -c websocket_test.conf
23 | * If the execution fails because of missing shared object dependencies (e.g. zlib, pcre, openssl), add paths to these dependencies to LD_LIBRARY_PATH and rerun.
24 |
25 | ### Run the test
26 | * New open a browser and enter the URL of the test HTML page. For a browser running on the same host as Nginx
27 | > http://localhost:8220/test_websocket.html
28 | * If accessing remotely then replace localhost with the name of the host where Nginx is running.
29 | * The output of the test is shown in the browser. Scroll down if necessary to confirm that all tests are green.
30 |
--------------------------------------------------------------------------------
/nginx_test/test_websocket.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |