Skip to content

Commit

Permalink
update latest nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed Jun 17, 2017
1 parent 1f8300e commit 99047ca
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 145 deletions.
26 changes: 10 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,34 @@ FROM hyperknot/baseimage16:1.0.1
MAINTAINER [email protected]

ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 \
TERM=xterm container=docker DEBIAN_FRONTEND=noninteractive \
NGINX_VERSION=1.13.0-1~xenial
TERM=xterm container=docker DEBIAN_FRONTEND=noninteractive

ADD ./build/nginx_1.13.1-1~xenial_amd64.deb /tmp

# start
RUN \
cd /tmp \

# add nginx repo
&& curl -s https://nginx.org/keys/nginx_signing.key | apt-key add - \
&& cp /etc/apt/sources.list /etc/apt/sources.list.bak \
&& echo "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list \
&& echo "deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list \

# update repo, install nginx and module to get dependencies
&& apt-get update -y && apt-get upgrade -y \
&& apt-get update -y && apt-get upgrade -y --no-install-recommends --no-install-suggests \
&& apt-get install -y --no-install-recommends --no-install-suggests \
nano nginx=1.13.0-1~xenial \
nginx-module-geoip=1.13.0-1~xenial \
nginx-module-image-filter=1.13.0-1~xenial \
gettext-base \
nano libgd3 gettext-base unzip \
&& dpkg --configure -a \

&& dpkg -i nginx_1.13.1-1~xenial_amd64.deb \

# re-enable all default services
&& rm -f /etc/service/syslog-forwarder/down \
&& rm -f /etc/service/cron/down \
&& rm -f /etc/service/syslog-ng/down \
&& rm -f /core \

# geoip stuff
&& cd /tmp \
&& curl http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | gzip -d - > /etc/nginx/GeoLiteCity.dat \

# 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 \
Expand All @@ -42,13 +39,10 @@ RUN \
# cleanup
&& apt-get clean -y && apt-get autoclean -y \
&& apt-get autoremove --purge -y \
&& rm -rf /var/lib/apt/lists/* /var/lib/log/* /tmp/* /var/tmp/* \

# remove existing image filter module so we can overwrite with ours
&& rm -rf /etc/nginx/modules/ngx_http_image_filter_m*.so
&& rm -rf /var/lib/apt/lists/* /var/lib/log/* /tmp/* /var/tmp/*

ADD ./files /

EXPOSE 80

CMD ["/sbin/my_init"]
47 changes: 10 additions & 37 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,22 @@ FROM hyperknot/baseimage16:1.0.1

MAINTAINER [email protected]

ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 TERM=xterm container=docker DEBIAN_FRONTEND=noninteractive NGINX_VERSION=1.13.0 NGINX_BUILD_DIR=/tmp/nginx
ENV IMAGE_FILTER_URL=https://raw.githubusercontent.com/niiknow/docker-nginx-image-proxy/master/build/src/ngx_http_image_filter_module.c
ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 \
TERM=xterm container=docker DEBIAN_FRONTEND=noninteractive

# start
RUN \
apt-get update && apt-get upgrade -y --force-yes --no-install-recommends \
&& apt-get install -y --force-yes --no-install-recommends wget curl unzip nano vim apt-transport-https \
apt-utils software-properties-common build-essential ca-certificates libssl-dev \
zlib1g-dev dpkg-dev libpcre3 libpcre3-dev libgd-dev \

&& dpkg --configure -a \

# re-enable all default services
&& rm -f /etc/service/syslog-forwarder/down \
&& rm -f /etc/service/cron/down \
&& rm -f /etc/service/syslog-ng/down \
&& rm -f /core \
&& wget -O - http://nginx.org/keys/nginx_signing.key | apt-key add - \
&& cp /etc/apt/sources.list /etc/apt/sources.list.bak \
&& echo "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list \
&& echo "deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list \
&& apt-get update \

# recompile nginx
&& mkdir -p ${NGINX_BUILD_DIR} \

# get the source
&& cd ${NGINX_BUILD_DIR}; apt-get source nginx -y \
&& mv ${NGINX_BUILD_DIR}/nginx-${NGINX_VERSION}/src/http/modules/ngx_http_image_filter_module.c ${NGINX_BUILD_DIR}/nginx-${NGINX_VERSION}/src/http/modules/ngx_http_image_filter_module.bak \
ADD ./ubuntu.sh /tmp/

# apply patch
&& curl -SL $IMAGE_FILTER_URL --output ${NGINX_BUILD_DIR}/nginx-${NGINX_VERSION}/src/http/modules/ngx_http_image_filter_module.c \
&& sed -i "s/--with-http_ssl_module/--with-http_ssl_module --with-http_image_filter_module=dynamic/g" ${NGINX_BUILD_DIR}/nginx-${NGINX_VERSION}/debian/rules \

# get build dependencies
&& cd ${NGINX_BUILD_DIR}; apt-get build-dep nginx -y \
&& cd ${NGINX_BUILD_DIR}/nginx-${NGINX_VERSION}; dpkg-buildpackage -b \

# install new nginx package
&& cd ${NGINX_BUILD_DIR}; dpkg -i nginx_${NGINX_VERSION}-1~xenial_amd64.deb
# start
RUN \
cd /tmp \
&& bash ubuntu.sh

ADD ./files /
# docker run -it imageid /bin/bash
# ftp passive

EXPOSE 80
VOLUME ["/backup"]

CMD ["/sbin/my_init"]
Binary file added build/nginx_1.13.1-1~xenial_amd64.deb
Binary file not shown.
88 changes: 72 additions & 16 deletions build/src/ngx_http_image_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define NGX_HTTP_IMAGE_GIF 2
#define NGX_HTTP_IMAGE_PNG 3
#define NGX_HTTP_IMAGE_WEBP 4
#define NGX_HTTP_IMAGE_BMP 5


#define NGX_HTTP_IMAGE_OFFSET_CENTER 0
Expand All @@ -58,15 +59,16 @@ typedef struct {

ngx_flag_t transparency;
ngx_flag_t interlace;
ngx_str_t output;

ngx_http_complex_value_t *wcv;
ngx_http_complex_value_t *hcv;
ngx_http_complex_value_t *oxcv;
ngx_http_complex_value_t *oycv;
ngx_http_complex_value_t *acv;
ngx_http_complex_value_t *jqcv;
ngx_http_complex_value_t *wqcv;
ngx_http_complex_value_t *shcv;
ngx_http_complex_value_t *wcv;
ngx_http_complex_value_t *hcv;
ngx_http_complex_value_t *oxcv;
ngx_http_complex_value_t *oycv;
ngx_http_complex_value_t *acv;
ngx_http_complex_value_t *jqcv;
ngx_http_complex_value_t *wqcv;
ngx_http_complex_value_t *shcv;

size_t buffer_size;
} ngx_http_image_filter_conf_t;
Expand Down Expand Up @@ -193,6 +195,13 @@ static ngx_command_t ngx_http_image_filter_commands[] = {
0,
NULL },

{ ngx_string("image_filter_output"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_image_filter_conf_t, output),
NULL },

ngx_null_command
};

Expand Down Expand Up @@ -236,7 +245,8 @@ static ngx_str_t ngx_http_image_types[] = {
ngx_string("image/jpeg"),
ngx_string("image/gif"),
ngx_string("image/png"),
ngx_string("image/webp")
ngx_string("image/webp"),
ngx_string("image/bmp")
};


Expand Down Expand Up @@ -366,7 +376,19 @@ ngx_http_image_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
} else {
ct = &ngx_http_image_types[ctx->type - 1];
}


if (ngx_strncmp(conf->output.data, "jpg", 3) == 0 || ngx_strncmp(conf->output.data, "jpeg", 4) == 0) {
ct = &ngx_http_image_types[NGX_HTTP_IMAGE_JPEG - 1];
} else if (ngx_strncmp(conf->output.data, "gif", 3) == 0){
ct = &ngx_http_image_types[NGX_HTTP_IMAGE_GIF - 1];
} else if (ngx_strncmp(conf->output.data, "png", 3) == 0){
ct = &ngx_http_image_types[NGX_HTTP_IMAGE_PNG - 1];
} else if (ngx_strncmp(conf->output.data, "webp", 4) == 0){
ct = &ngx_http_image_types[NGX_HTTP_IMAGE_WEBP - 1];
} else if (ngx_strncmp(conf->output.data, "bmp", 3) == 0){
ct = &ngx_http_image_types[NGX_HTTP_IMAGE_BMP - 1];
}

r->headers_out.content_type_len = ct->len;
r->headers_out.content_type = *ct;
r->headers_out.content_type_lowcase = NULL;
Expand Down Expand Up @@ -490,6 +512,10 @@ ngx_http_image_test(ngx_http_request_t *r, ngx_chain_t *in)
/* WebP */

return NGX_HTTP_IMAGE_WEBP;
} else if (p[0] == 'B' && p[1] == 'M') {
/* BMP */

return NGX_HTTP_IMAGE_BMP;
}

return NGX_HTTP_IMAGE_NONE;
Expand Down Expand Up @@ -829,6 +855,16 @@ ngx_http_image_size(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)

break;

case NGX_HTTP_IMAGE_BMP:
if (ctx->length < 24) {
return NGX_DECLINED;
}

width = p[18] * 256 + p[19];
height = p[22] * 256 + p[23];

break;

default:

return NGX_DECLINED;
Expand Down Expand Up @@ -1185,6 +1221,11 @@ ngx_http_image_source(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
#endif
break;

case NGX_HTTP_IMAGE_BMP:
img = gdImageCreateFromBmpPtr(ctx->length, ctx->image);
failed = "gdImageCreateFromBmpPtr() failed";
break;

default:
failed = "unknown image type";
break;
Expand Down Expand Up @@ -1237,11 +1278,23 @@ ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img,

out = NULL;

conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);

if (ngx_strncmp(conf->output.data, "jpg", 3) == 0 || ngx_strncmp(conf->output.data, "jpeg", 4) == 0){
type = NGX_HTTP_IMAGE_JPEG;
} else if (ngx_strncmp(conf->output.data, "gif", 3) == 0){
type = NGX_HTTP_IMAGE_GIF;
} else if (ngx_strncmp(conf->output.data, "png", 3) == 0){
type = NGX_HTTP_IMAGE_PNG;
} else if (ngx_strncmp(conf->output.data, "webp", 4) == 0){
type = NGX_HTTP_IMAGE_WEBP;
} else if (ngx_strncmp(conf->output.data, "bmp", 3) == 0){
type = NGX_HTTP_IMAGE_BMP;
}

switch (type) {

case NGX_HTTP_IMAGE_JPEG:
conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);

q = ngx_http_image_filter_get_value(r, conf->jqcv, conf->jpeg_quality);
if (q <= 0) {
return NULL;
Expand All @@ -1257,8 +1310,6 @@ ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img,
break;

case NGX_HTTP_IMAGE_PNG:
conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);

if (conf->jpeg_quality >= 96) {
out = gdImagePngPtr(img, size);
failed = "gdImagePngPtr() failed";
Expand All @@ -1275,8 +1326,6 @@ ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img,

case NGX_HTTP_IMAGE_WEBP:
#if (NGX_HAVE_GD_WEBP)
conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);

q = ngx_http_image_filter_get_value(r, conf->wqcv, conf->webp_quality);
if (q <= 0) {
return NULL;
Expand All @@ -1289,6 +1338,11 @@ ngx_http_image_out(ngx_http_request_t *r, ngx_uint_t type, gdImagePtr img,
#endif
break;

case NGX_HTTP_IMAGE_BMP:
out = gdImageBmpPtr(img, size, 0);
failed = "gdImageBmpPtr() failed";
break;

default:
failed = "unknown image type";
break;
Expand Down Expand Up @@ -1459,6 +1513,8 @@ ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)

ngx_conf_merge_value(conf->interlace, prev->interlace, 0);

ngx_conf_merge_str_value(conf->output, prev->output, "");

ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
1 * 1024 * 1024);

Expand Down
32 changes: 32 additions & 0 deletions build/ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

export NGINX_VERSION=1.13.1
export IMAGE_FILTER_URL=https://gist.githubusercontent.com/noogen/4a662ade2d9570f8996f3af9869c5216/raw/54ff0a221a069a3c2574b0404afb71552fd4066d/ngx_http_image_filter_module.c
export NGINX_BUILD_DIR=/usr/src/nginx/nginx-${NGINX_VERSION}

cd /tmp
curl -s https://nginx.org/keys/nginx_signing.key | apt-key add -
cp /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list
echo "deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list

apt-get update && apt-get upgrade -y --no-install-recommends --no-install-suggests
apt-get install -y --no-install-recommends --no-install-suggests curl unzip nano vim apt-transport-https \
apt-utils software-properties-common build-essential ca-certificates libssl-dev \
zlib1g-dev dpkg-dev libpcre3 libpcre3-dev libgd-dev ftp

dpkg --configure -a

mkdir -p /usr/src/nginx

cd /usr/src/nginx
apt-get source nginx=${NGINX_VERSION} -y
mv ${NGINX_BUILD_DIR}/src/http/modules/ngx_http_image_filter_module.c ${NGINX_BUILD_DIR}/src/http/modules/ngx_http_image_filter_module.bak

curl -SL $IMAGE_FILTER_URL --output ${NGINX_BUILD_DIR}/src/http/modules/ngx_http_image_filter_module.c
sed -i "s/--with-http_ssl_module/--with-http_ssl_module --with-http_image_filter_module/g" ${NGINX_BUILD_DIR}/debian/rules

cd /usr/src/nginx
apt-get build-dep nginx -y
cd ${NGINX_BUILD_DIR}
dpkg-buildpackage -uc -us -b
32 changes: 0 additions & 32 deletions files/etc/nginx/nginx.new
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
load_module "modules/ngx_http_image_filter_module.so";
load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";

# Server globals
user www-data;
worker_processes auto;
Expand Down Expand Up @@ -102,37 +98,9 @@ http {
open_file_cache_min_uses 2;
open_file_cache_errors off;


# Other settings
log_subrequest on;
rewrite_log on;
resolver 8.8.8.8 8.8.4.4;


image_filter_buffer 20M;
image_filter_interlace on;

# Needed to allow uri protocol slashes from being merged
merge_slashes off;


proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass_header Set-Cookie;
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
proxy_ignore_headers Expires Cache-Control;
proxy_temp_path /var/cache/nginx/temp;


# Prevent Client headers from going to nginx.
proxy_pass_request_headers off;

# GeoIP stuff
geoip_city /etc/nginx/GeoLiteCity.dat;

# Wildcard include
include /etc/nginx/conf.d/*.conf;
Expand Down
Loading

0 comments on commit 99047ca

Please sign in to comment.