Skip to content

Commit

Permalink
Merge pull request #1627 from estaleiro/brotli
Browse files Browse the repository at this point in the history
Add brotli support
  • Loading branch information
aledbf authored Nov 1, 2017
2 parents 3ea7464 + fddcfd0 commit dc3225e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/user-guide/configmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ Setting at least one code also enables [proxy_intercept_errors](http://nginx.org

Example usage: `custom-http-errors: 404,415`

### Other Directives

#### brotli-level
Sets the Brotli Compression Level that will be used.
*Defaults to* 4


#### brotli-types
Sets the MIME Types that will be compressed on-the-fly by brotli.
*Defaults to* `application/xml+rss application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component`


#### enable-modsecurity

Enables the modsecurity module for NGINX
Expand Down Expand Up @@ -314,6 +326,15 @@ Sets the number of unsuccessful attempts to communicate with the [server](http:/

Sets the time during which the specified number of unsuccessful attempts to communicate with the [server](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream) should happen to consider the server unavailable.

#### use-brotli

Enables or disables compression of HTTP responses using the ["brotli" module](https://github.com/google/ngx_brotli).

The default mime type list to compress is: `application/xml+rss application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component`.

This is *enabled* by default


#### use-gzip

Enables or disables compression of HTTP responses using the ["gzip" module](http://nginx.org/en/docs/http/ngx_http_gzip_module.html).
Expand Down
6 changes: 6 additions & 0 deletions images/nginx-slim/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ make install

cd "$BUILD_PATH"

# Get Brotli source and deps
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli && git submodule update --init


if [[ ${ARCH} == "x86_64" ]]; then
# build modsecurity library
git clone https://github.com/SpiderLabs/ModSecurity
Expand Down Expand Up @@ -204,6 +209,7 @@ WITH_MODULES="--add-module=$BUILD_PATH/ngx_devel_kit-$NDK_VERSION \
--add-module=$BUILD_PATH/nginx-goodies-nginx-sticky-module-ng-$STICKY_SESSIONS_VERSION \
--add-module=$BUILD_PATH/nginx-http-auth-digest-$NGINX_DIGEST_AUTH \
--add-module=$BUILD_PATH/ngx_http_substitutions_filter_module-$NGINX_SUBSTITUTIONS \
--add-module=$BUILD_PATH/ngx_brotli \
--add-dynamic-module=$BUILD_PATH/nginx-opentracing-$NGINX_OPENTRACING_VERSION/opentracing \
--add-dynamic-module=$BUILD_PATH/nginx-opentracing-$NGINX_OPENTRACING_VERSION/zipkin"

Expand Down
15 changes: 15 additions & 0 deletions pkg/nginx/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (

gzipTypes = "application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component"

brotliTypes = "application/xml+rss application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component"

logFormatUpstream = `%v - [$the_real_ip] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status`

logFormatStream = `[$time_local] $protocol $status $bytes_sent $bytes_received $session_time`
Expand Down Expand Up @@ -331,6 +333,16 @@ type Configuration struct {
// http://nginx.org/en/docs/http/ngx_http_gzip_module.html
UseGzip bool `json:"use-gzip,omitempty"`

// Enables or disables the use of the NGINX Brotli Module for compression
// https://github.com/google/ngx_brotli
UseBrotli bool `json:"use-brotli,omitempty"`

// Brotli Compression Level that will be used
BrotliLevel int `json:"brotli-level,omitempty"`

// MIME Types that will be compressed on-the-fly using Brotli module
BrotliTypes string `json:"brotli-types,omitempty"`

// Enables or disables the HTTP/2 support in secure connections
// http://nginx.org/en/docs/http/ngx_http_v2_module.html
// Default: true
Expand Down Expand Up @@ -424,6 +436,8 @@ func NewDefault() Configuration {
AllowBackendServerHeader: false,
AccessLogPath: "/var/log/nginx/access.log",
ErrorLogPath: "/var/log/nginx/error.log",
BrotliLevel: 4,
BrotliTypes: brotliTypes,
ClientHeaderBufferSize: "1k",
ClientHeaderTimeout: 60,
ClientBodyBufferSize: "8k",
Expand Down Expand Up @@ -462,6 +476,7 @@ func NewDefault() Configuration {
SSLSessionCacheSize: sslSessionCacheSize,
SSLSessionTickets: true,
SSLSessionTimeout: sslSessionTimeout,
UseBrotli: true,
UseGzip: true,
WorkerProcesses: strconv.Itoa(runtime.NumCPU()),
WorkerShutdownTimeout: "10s",
Expand Down
7 changes: 7 additions & 0 deletions rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ http {

include /etc/nginx/mime.types;
default_type text/html;

{{ if $cfg.UseBrotli }}
brotli on;
brotli_comp_level {{ $cfg.BrotliLevel }};
brotli_types {{ $cfg.BrotliTypes }};
{{ end }}

{{ if $cfg.UseGzip }}
gzip on;
gzip_comp_level 5;
Expand Down

0 comments on commit dc3225e

Please sign in to comment.