Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1bad3be
Rewrite start-nginx for better signal handling.
agriffis Mar 27, 2014
324973a
Build NGINX in compile script.
agriffis Mar 27, 2014
1cce8f0
Update README
agriffis Mar 30, 2014
5439c04
Nice announcements in bin/compile instead of at=foo
agriffis Mar 30, 2014
e6a6e30
Remove the tail hack.
agriffis Mar 30, 2014
dbf2ad5
Expand pids as list, not a single string.
agriffis Mar 30, 2014
a0d433a
Remove obsolete comment.
agriffis Mar 30, 2014
60a8c06
Add Python/Gunicorn instructions.
agriffis Mar 30, 2014
1911fbb
Repair startup with pid in /app.
agriffis Mar 31, 2014
d66e690
Enable gzip for a common and configurable set of mimetypes.
agriffis Jul 20, 2015
562ffa2
Enable gzip for proxied requests.
agriffis Jul 20, 2015
a7964a9
Disable gzip automatically for IE6 since nginx has this built-in.
agriffis Jul 20, 2015
7cfd361
Don't override the default gzip_comp_level=1 which already offers the…
agriffis Jul 21, 2015
53c982b
Render all *.conf.erb templates include local configs.
agriffis Dec 22, 2015
6f44d5e
Source local config from nginx.conf dir
agriffis Dec 22, 2015
358a05f
Allow version of utilities to be set on environment
vsemionov Aug 19, 2017
bc84d54
upgraded dependencies
vsemionov Aug 19, 2017
d3ca57e
updated dependency versions in readme
vsemionov Aug 20, 2017
97c7f6e
Merge pull request #1 from vsemionov/master
agriffis Aug 20, 2017
75b640d
Fix spell-o in README
agriffis Aug 20, 2017
6a08206
DRY syntax for version variables
agriffis Aug 21, 2017
d1e6a2a
Merge branch 'master' of https://github.com/ryandotsmith/nginx-buildpack
agriffis Aug 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

178 changes: 178 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
Heroku Buildpack: NGINX
=======================

Nginx-buildpack vendors NGINX inside a dyno and connects NGINX to an app server via UNIX domain sockets.

This buildpack forks [Ryan Smith's excellent buildpack](https://github.com/ryandotsmith/nginx-buildpack) and maintains his feature list:

* Unified NGINX/App Server logs.
* [L2met](https://github.com/ryandotsmith/l2met) friendly NGINX log format.
* [Heroku request ids](https://devcenter.heroku.com/articles/http-request-id) embedded in NGINX logs.
* Crashes dyno if NGINX or App server crashes. Safety first.
* Language/App Server agnostic.
* Customizable NGINX config.
* Application coordinated dyno starts.

However there are some significant differences:

* NGINX is built during initial deployment, rather than using a prebuilt binary. The binary is cached between deploys.
* NGINX logs are sent directly to stderr rather than to the filesystem. This results in faster log delivery to Heroku logplex, and doesn't gradually fill the filesystem.
* Signals are handled gracefully by the wrapper script, allowing both the app and NGINX to stop properly on dyno shutdown.
* `PORT` is overridden to refer to the UNIX domain socket, so the application can detect whether it's running under NGINX or directly. This is especially because NGINX can be enabled or disabled at run-time by toggling `NGINX_ENABLED`.

Versions
--------

* [NGINX](http://nginx.org/) 1.12.1
* [PCRE](http://sourceforge.net/projects/pcre/) 8.41
* [ngx_headers_more](https://github.com/agentzh/headers-more-nginx-module) 0.32

These versions are tunable by setting `NGINX_VERSION`, `NGINX_PCRE_VERSION` and `NGINX_HEADERS_MORE_VERSION` in the app config, so you can update even if the buildpack hasn't been updated yet.

Requirements
------------

* Your webserver listens to the socket at `/tmp/nginx.socket`.
* You can start your web server with a shell command.

Logging
-------

NGINX will output the following style of logs:

```
measure.nginx.service=0.007 request_id=e2c79e86b3260b9c703756ec93f8a66d
```

You can correlate this id with your Heroku router logs:

```
at=info method=GET path=/ host=salty-earth-7125.herokuapp.com request_id=e2c79e86b3260b9c703756ec93f8a66d fwd="67.180.77.184" dyno=web.1 connect=1ms service=8ms status=200 bytes=21
```

Language/App Server Agnostic
----------------------------

Nginx-buildpack provides a command named `bin/start-nginx` this command takes another command as an argument. You must pass your app server's startup command to `start-nginx`.

For example, to get NGINX and Unicorn up and running:

```bash
$ cat Procfile
web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb
```

### Application/Dyno coordination

The buildpack will not start NGINX until the application is listening on `/tmp/nginx-socket`. Since NGINX binds to the dyno's `PORT` and since the `PORT` determines if the app can receive traffic, you can delay NGINX accepting traffic until your application is ready to handle it. The examples below show how/when you should write the file when working with Unicorn.

### Setting the Worker Processes

You can configure NGINX's `worker_processes` directive via the `NGINX_WORKERS` environment variable.

For example, to set your `NGINX_WORKERS` to 8 on a PX dyno:

```bash
$ heroku config:set NGINX_WORKERS=8
```

Customizable NGINX Config
-------------------------

You can provide your own NGINX config by creating a file named `config/nginx.conf.erb` in your app. Start by copying the buildpack's [default config file](https://raw.githubusercontent.com/agriffis/nginx-buildpack/develop/config/nginx.conf.erb).

Alternatively, if you like the defaults, you can add a file `config/nginx-local.conf` and it will be included automatically.

### Patched Logging

This buildpack patches NGINX to send the access log directly to stderr, which is not a feature NGINX provides normally. If you override the default config and want to make use of this logging feature, use the following:

```
error_log stderr;

http {
access_log error;
}
```

This instructs NGINX to send the error log to stderr (this is provided by NGINX upstream) and then to send the access log to the error log output (this is provided by the patch).

Examples
--------

### Ruby/Unicorn

#### Update Buildpacks

Use [heroku-buildpack-multii](https://github.com/agriffis/heroku-buildpack-multii) to load nginx-buildpack in addition to the Ruby buildpack.

```bash
$ heroku config:set BUILDPACK_URL=https://github.com/agriffis/heroku-buildpack-multii.git
$ echo 'https://github.com/agriffis/nginx-buildpack.git' >> .buildpacks
$ echo 'https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/ruby.tgz' >> .buildpacks
$ git add .buildpacks
$ git commit -m 'Add multi-buildpack'
```

#### Update Unicorn Config

Listen on `ENV['PORT']` which will be the UNIX domain socket when running under NGINX.

```ruby
listen ENV['PORT']
```

#### Update Procfile

Prefix your server with `bin/start-nginx`.

```
web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb
```

### Python/Gunicorn

#### Update Buildpacks

Use [heroku-buildpack-multii](https://github.com/agriffis/heroku-buildpack-multii) to load nginx-buildpack in addition to the Python buildpack.

```bash
$ heroku config:set BUILDPACK_URL=https://github.com/agriffis/heroku-buildpack-multii.git
$ echo 'https://github.com/agriffis/nginx-buildpack.git' >> .buildpacks
$ echo 'https://github.com/heroku/heroku-buildpack-python.git' >> .buildpacks
$ git add .buildpacks
$ git commit -m 'Add multi-buildpack'
```

#### Update Gunicorn Config

Check for the UNIX domain socket and prefix with `unix:` for Gunicorn in `config/gunicorn.py`.

```python
import os

bind = os.environ['PORT']

if bind.startswith('/'):
bind = 'unix:' + bind
```

#### Update Procfile

Prefix your server with `bin/start-nginx`.

```
web: bin/start-nginx gunicorn -c config/gunicorn.py
```

License
-------

* Copyright (c) 2013 Ryan R. Smith
* Copyright (c) 2014 Aron Griffis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
133 changes: 112 additions & 21 deletions bin/compile
Original file line number Diff line number Diff line change
@@ -1,23 +1,114 @@
#!/usr/bin/env bash

set -e

mkdir -p "$1/bin/"
cp "bin/nginx-$STACK" "$1/bin/nginx"
nginx_version=$(./bin/nginx-$STACK -V 2>&1 | head -1 | awk '{ print $NF }')
echo "-----> nginx-buildpack: Installed ${nginx_version} to app/bin"
cp bin/start-nginx "$1/bin/"
echo '-----> nginx-buildpack: Added start-nginx to app/bin'

mkdir -p "$1/config"

cp config/mime.types "$1/config/"
echo '-----> nginx-buildpack: Default mime.types copied to app/config/'

if [[ ! -f $1/config/nginx.conf.erb ]]; then
cp config/nginx.conf.erb "$1/config/"
echo '-----> nginx-buildpack: Default config copied to app/config.'
else
echo '-----> nginx-buildpack: Custom config found in app/config.'
fi
exit 0
main() {
APP_DIR=/app
BUILD_DIR=$1 # /tmp/build_76471de2-7dff-46b7-b331-c8dd07653def
CACHE_DIR=$2 # /app/tmp/cache
ENV_DIR=$3
BUILDPACK_DIR=$(readlink -f $(dirname $(type -P "$0"))/..)

: ${NGINX_VERSION:=1.12.1}
: ${NGINX_PCRE_VERSION:=8.41}
: ${NGINX_HEADERS_MORE_VERSION:=0.32}

set -eo pipefail

load-env

declare cached_version
if ! is-enabled "$NGINX_FORCE_BUILD"; then
cached_version=$($CACHE_DIR/sbin/nginx -V 2>&1 | head -n1 | awk -F/ '{print $NF}') || true
fi

if [[ "$cached_version" != "$NGINX_VERSION" ]]; then
announce "Building NGINX $NGINX_VERSION"
build-nginx $CACHE_DIR
else
announce "Using cached NGINX $NGINX_VERSION"
fi

install $CACHE_DIR/sbin/nginx bin/nginx
install $BUILDPACK_DIR/bin/start-nginx bin/start-nginx
cond-install $BUILDPACK_DIR/config/mime.types config/mime.types
cond-install $BUILDPACK_DIR/config/nginx.conf.erb config/nginx.conf.erb
}

load-env() {
declare e
for e in $ENV_DIR/*; do
e=${e##*/}
if [[ $e == NGINX_* ]]; then
export "$e=$(<$ENV_DIR/$e)"
fi
done
}

build-nginx() {
declare nginx_install_dir=$1
declare nginx_build_dir=/tmp/nginx-build

declare nginx_url=http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
declare pcre_url=http://downloads.sourceforge.net/project/pcre/pcre/$NGINX_PCRE_VERSION/pcre-$NGINX_PCRE_VERSION.tar.bz2
declare headers_more_url=https://github.com/agentzh/headers-more-nginx-module/archive/v$NGINX_HEADERS_MORE_VERSION.tar.gz

(
set-indent

mkdir -p $nginx_build_dir
cd $nginx_build_dir

curl -#L $nginx_url | tar xz
cd nginx-$NGINX_VERSION
curl -#L $pcre_url | tar xj
curl -#L $headers_more_url | tar xz

patch -p1 < $BUILDPACK_DIR/nginx-1.5.12-heroku.patch

CFLAGS="-O3 -pipe" ./configure \
--prefix=$nginx_install_dir \
--with-pcre=pcre-$NGINX_PCRE_VERSION \
--add-module=$PWD/headers-more-nginx-module-$NGINX_HEADERS_MORE_VERSION \
--with-http_addition_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_xslt_module
make -j4

mkdir -p $nginx_install_dir
make install
)
}

cond-install() {
if [[ -e $BUILD_DIR/$2 ]]; then
announce "Found custom $2"
else
install "$1" "$2"
fi
}

install() {
mkdir -p "$BUILD_DIR/$(dirname $2)"
announce "Installing $2"
cp -a "$1" "$BUILD_DIR/$(dirname $2)/"
}

set-indent() {
exec &> >(sed -u 's/^/ /')
}

announce() {
echo "-----> nginx-buildpack: $*"
}

is-enabled() {
( shopt -s extglob nocasematch
[[ $1 == @(1|true|yes|on) ]]
)
}

[[ "$0" != "$BASH_SOURCE" ]] || main "$@"
Binary file removed bin/nginx-cedar
Binary file not shown.
Binary file removed bin/nginx-cedar-14
Binary file not shown.
Loading