Skip to content

Instantly share code, notes, and snippets.

@alexxxnf
Last active August 1, 2020 22:21
Show Gist options
  • Save alexxxnf/2511a7f6b9b82ea1d18e8f497dcc025b to your computer and use it in GitHub Desktop.
Save alexxxnf/2511a7f6b9b82ea1d18e8f497dcc025b to your computer and use it in GitHub Desktop.
Docker image for nginx with dynamic Brotli module
FROM nginx:1-alpine as builder
# ENV NGINX_VERSION 1.16.0 # defined in the base image
ENV NGX_BROTLI_VERSION 0.1.2
ENV BROTLI_VERSION 1.0.7
RUN set -x \
&& mkdir -p /usr/src \
# dowload and extract source files
&& wget -qO- https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz | tar xzf - -C /usr/src \
&& wget -qO- https://github.com/eustas/ngx_brotli/archive/v$NGX_BROTLI_VERSION.tar.gz | tar xzf - -C /usr/src \
&& wget -qO- https://github.com/google/brotli/archive/v$BROTLI_VERSION.tar.gz | tar xzf - -C /usr/src \
# ngx_brotli needs brotli files under its deps/brotli directory
&& rm -rf /usr/src/ngx_brotli-$NGX_BROTLI_VERSION/deps/brotli/ \
&& ln -s /usr/src/brotli-$BROTLI_VERSION /usr/src/ngx_brotli-$NGX_BROTLI_VERSION/deps/brotli \
# install packages needed to build nginx and ngx_brotli
&& apk add --no-cache \
gcc \
libc-dev \
make \
openssl-dev \
pcre-dev \
zlib-dev \
linux-headers \
curl \
gnupg1 \
libxslt-dev \
gd-dev \
geoip-dev \
perl-dev \
&& cd /usr/src/nginx-$NGINX_VERSION \
# extract original nginx build arguments and append ngx_brotli as dynamic module
&& CNF="$(2>&1 nginx -V | sed -n 's/^configure arguments: //p') --add-dynamic-module=../ngx_brotli-$NGX_BROTLI_VERSION" \
# build modules
&& ./configure $CNF \
&& make modules
# copy built modules to a new clean image
FROM nginx:1-alpine
COPY --from=builder /usr/src/nginx-$NGINX_VERSION/objs/ngx_http_brotli_*_module.so /usr/lib/nginx/modules/
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
load_module /usr/lib/nginx/modules/ngx_http_brotli_filter_module.so;
load_module /usr/lib/nginx/modules/ngx_http_brotli_static_module.so;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
gzip_static on;
gzip on;
gzip_comp_level 6;
gzip_proxied any;
gzip_vary on;
gzip_types
application/atom+xml
application/javascript
application/json
application/rdf+xml
application/rss+xml
application/x-javascript
application/xml
font/opentype
font/truetype
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
brotli_static on;
brotli on;
brotli_comp_level 6;
brotli_types
application/atom+xml
application/javascript
application/json
application/rdf+xml
application/rss+xml
application/x-javascript
application/xml
font/opentype
font/truetype
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
try_files $uri /index.html;
etag on;
expires max;
add_header Cache-Control public;
location = /index.html {
expires 0;
add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
}
}
}
}
@alexxxnf
Copy link
Author

alexxxnf commented Aug 1, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment