From 904ce2292bfed86e838a102631854d4fa14b7a61 Mon Sep 17 00:00:00 2001 From: Aleksander Cynarski Date: Sun, 18 Feb 2018 12:56:04 +0100 Subject: [PATCH] Initial commit --- Dockerfile | 133 ++++++++++++++++++++++++++++++++++++++++++ README.md | 31 ++++++++++ nginx.conf | 40 +++++++++++++ nginx.vh.default.conf | 126 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 330 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 nginx.conf create mode 100644 nginx.vh.default.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e28a51a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,133 @@ +FROM alpine:3.7 + +LABEL maintainer="aleksander@cynarski.pl" + +ENV NGINX_VERSION 1.13.8 + +RUN CONFIG="\ + --prefix=/etc/nginx \ + --sbin-path=/usr/sbin/nginx \ + --modules-path=/usr/lib/nginx/modules \ + --conf-path=/etc/nginx/nginx.conf \ + --error-log-path=/var/log/nginx/error.log \ + --http-log-path=/var/log/nginx/access.log \ + --pid-path=/var/run/nginx.pid \ + --lock-path=/var/run/nginx.lock \ + --http-client-body-temp-path=/var/cache/nginx/client_temp \ + --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ + --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ + --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ + --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ + --user=nginx \ + --group=nginx \ + --with-http_ssl_module \ + --with-http_realip_module \ + --with-http_addition_module \ + --with-http_sub_module \ + --with-http_dav_module \ + --with-http_flv_module \ + --with-http_mp4_module \ + --with-http_gunzip_module \ + --with-http_gzip_static_module \ + --with-http_random_index_module \ + --with-http_secure_link_module \ + --with-http_stub_status_module \ + --with-http_auth_request_module \ + --with-http_xslt_module=dynamic \ + --with-http_image_filter_module=dynamic \ + --with-http_geoip_module=dynamic \ + --with-threads \ + --with-stream \ + --with-stream_ssl_module \ + --with-stream_ssl_preread_module \ + --with-stream_realip_module \ + --with-stream_geoip_module=dynamic \ + --with-http_slice_module \ + --with-mail \ + --with-mail_ssl_module \ + --with-compat \ + --with-file-aio \ + --with-http_v2_module \ + --add-module=/root/nginx-push-stream-module \ + " \ + && addgroup -S nginx \ + && apk add --no-cache git openssh \ + && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \ + && apk add --no-cache --virtual .build-deps \ + gcc \ + libc-dev \ + make \ + openssl-dev \ + pcre-dev \ + zlib-dev \ + linux-headers \ + curl \ + gnupg \ + libxslt-dev \ + gd-dev \ + geoip-dev \ + && curl -fSL https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \ + && git clone https://github.com/wandenberg/nginx-push-stream-module.git /root/nginx-push-stream-module \ + && mkdir -p /usr/src \ + && tar -zxC /usr/src -f nginx.tar.gz \ + && rm nginx.tar.gz \ + && cd /usr/src/nginx-$NGINX_VERSION \ + && ./configure $CONFIG --with-debug \ + && make -j$(getconf _NPROCESSORS_ONLN) \ + && mv objs/nginx objs/nginx-debug \ + && mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so \ + && mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so \ + && mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so \ + && mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so \ + && ./configure $CONFIG \ + && make -j$(getconf _NPROCESSORS_ONLN) \ + && make install \ + && rm -rf /etc/nginx/html/ \ + && mkdir /etc/nginx/conf.d/ \ + && mkdir -p /usr/share/nginx/html/ \ + && install -m644 html/index.html /usr/share/nginx/html/ \ + && install -m644 html/50x.html /usr/share/nginx/html/ \ + && install -m755 objs/nginx-debug /usr/sbin/nginx-debug \ + && install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so \ + && install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so \ + && install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so \ + && install -m755 objs/ngx_stream_geoip_module-debug.so /usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so \ + && ln -s ../../usr/lib/nginx/modules /etc/nginx/modules \ + && strip /usr/sbin/nginx* \ + && strip /usr/lib/nginx/modules/*.so \ + && rm -rf /usr/src/nginx-$NGINX_VERSION \ + \ + # Bring in gettext so we can get `envsubst`, then throw + # the rest away. To do this, we need to install `gettext` + # then move `envsubst` out of the way so `gettext` can + # be deleted completely, then move `envsubst` back. + && apk add --no-cache --virtual .gettext gettext \ + && mv /usr/bin/envsubst /tmp/ \ + \ + && runDeps="$( \ + scanelf --needed --nobanner --format '%n#p' /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \ + | tr ',' '\n' \ + | sort -u \ + | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \ + )" \ + && apk add --no-cache --virtual .nginx-rundeps $runDeps \ + && apk del .build-deps \ + && apk del .gettext \ + && mv /tmp/envsubst /usr/local/bin/ \ + \ + # Bring in tzdata so users could set the timezones through the environment + # variables + && apk add --no-cache tzdata \ + \ + # forward request and error logs to docker log collector + && ln -sf /dev/stdout /var/log/nginx/access.log \ + && ln -sf /dev/stderr /var/log/nginx/error.log + +COPY nginx.conf /etc/nginx/nginx.conf +COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 + +STOPSIGNAL SIGTERM + +CMD ["nginx", "-g", "daemon off;"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..7be5861 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Nginx Push Stream Module + +https://github.com/wandenberg/nginx-push-stream-module + + +## Budowanie obrazu + +``` +docker build -t nginx_push . +``` + +## Uruchomienie obrazu + +``` +docker run -p 8080:8080 nginx_push +``` + +## Przykłady użycia + +W przeglądarce wchodzimy na adres: +[http://localhost:8080/sub/channel_1](http://localhost:8080/sub/channel_1) + + +W konsoli wpisujemy: + +``` +curl -s -v -X POST 'http://localhost:8080/pub?id=channel_1' -d 'Hello World!' +``` + +Tadam! ;] + diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..b886d73 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,40 @@ +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + +worker_processes 2; + +events { + worker_connections 1024; +} + +http { + postpone_output 1; # only postpone a single byte, default 1460 bytes + access_log /var/log/nginx-http_access.log; + + push_stream_shared_memory_size 100m; + push_stream_max_channel_id_length 200; + # max messages to store in memory + push_stream_max_messages_stored_per_channel 20; + # message ttl + push_stream_message_ttl 5m; + # ping frequency + push_stream_ping_message_interval 30s; + # connection ttl to enable recycle + push_stream_subscriber_connection_ttl 15m; + # connection ttl for long polling + push_stream_longpolling_connection_ttl 30s; + push_stream_timeout_with_body off; + + # wildcard + push_stream_wildcard_channel_prefix "broad_"; + push_stream_wildcard_channel_max_qtd 3; + + push_stream_message_template "{\"id\":~id~,\"channel\":\"~channel~\",\"text\":\"~text~\", \"tag\":\"~tag~\", \"time\":\"~time~\", \"eventid\":\"~event-id~\"}"; + + # subscriber may create channels on demand or only authorized (publisher) may do it? + push_stream_authorized_channels_only off; + + push_stream_allowed_origins "*"; + + include /etc/nginx/conf.d/*.conf; +} diff --git a/nginx.vh.default.conf b/nginx.vh.default.conf new file mode 100644 index 0000000..c88f59f --- /dev/null +++ b/nginx.vh.default.conf @@ -0,0 +1,126 @@ + server { + listen 8080 default_server; + server_name localhost; + + + location /pub { + # activate publisher mode for this location, with admin support + push_stream_publisher admin; + + # query string based channel id + push_stream_channels_path $arg_id; + + # store messages in memory + push_stream_store_messages on; + + # Message size limit + # client_max_body_size MUST be equal to client_body_buffer_size or + # you will be sorry. + client_max_body_size 1m; + client_body_buffer_size 1m; + } + + location ~ /sub/(.*) { + # activate subscriber mode for this location + push_stream_subscriber; + + # positional channel path + push_stream_channels_path $1; + if ($arg_tests = "on") { + push_stream_channels_path "test_$1"; + } + + # header to be sent when receiving new subscriber connection + push_stream_header_template "\r\n\r\n\r\n\r\n\r\n\r\n\r\n"; + + # message template + push_stream_message_template "
p(~id~,'~channel~','~text~','~event-id~', '~time~', '~tag~');

"; + # footer to be sent when finishing subscriber connection + push_stream_footer_template ""; + # content-type + default_type "text/html; charset=utf-8"; + + if ($arg_qs = "on") { + push_stream_last_received_message_time "$arg_time"; + push_stream_last_received_message_tag "$arg_tag"; + push_stream_last_event_id "$arg_eventid"; + } + } + + location ~ /ev/(.*) { + # activate event source mode for this location + push_stream_subscriber eventsource; + + # positional channel path + push_stream_channels_path $1; + if ($arg_tests = "on") { + push_stream_channels_path "test_$1"; + } + + if ($arg_qs = "on") { + push_stream_last_received_message_time "$arg_time"; + push_stream_last_received_message_tag "$arg_tag"; + push_stream_last_event_id "$arg_eventid"; + } + } + + location ~ /lp/(.*) { + # activate long-polling mode for this location + push_stream_subscriber long-polling; + + # positional channel path + push_stream_channels_path $1; + if ($arg_tests = "on") { + push_stream_channels_path "test_$1"; + } + + if ($arg_qs = "on") { + push_stream_last_received_message_time "$arg_time"; + push_stream_last_received_message_tag "$arg_tag"; + push_stream_last_event_id "$arg_eventid"; + } + } + + location ~ /jsonp/(.*) { + # activate long-polling mode for this location + push_stream_subscriber long-polling; + + push_stream_last_received_message_time "$arg_time"; + push_stream_last_received_message_tag "$arg_tag"; + push_stream_last_event_id "$arg_eventid"; + + # positional channel path + push_stream_channels_path $1; + if ($arg_tests = "on") { + push_stream_channels_path "test_$1"; + } + } + + location ~ /ws/(.*) { + # activate websocket mode for this location + push_stream_subscriber websocket; + + # positional channel path + push_stream_channels_path $1; + if ($arg_tests = "on") { + push_stream_channels_path "test_$1"; + } + + # store messages in memory + push_stream_store_messages on; + + push_stream_websocket_allow_publish on; + + if ($arg_qs = "on") { + push_stream_last_received_message_time "$arg_time"; + push_stream_last_received_message_tag "$arg_tag"; + push_stream_last_event_id "$arg_eventid"; + } + } + + location / { + #if (!-f $request_filename) { + # proxy_pass "http://localhost:8888"; + #} + } + }