-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New "Static preset" for websites & single-page apps (#114)
* New "Static" mode following Heroku conventions to support websites & single page apps * Add config use-cases from heroku-buildpack-static to the nginx-static.conf with comments * Improve usage doc flow * Split out static vs proxy docs, improve default doc root & logging * Clarifying documentation * Update changelog * Fix doc typo * Doc formatting * Update to Nginx 1.25.1 & Ruby 3.2.2 * Use buildpack registry name in docs
- Loading branch information
Showing
11 changed files
with
396 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
|
||
echo 'buildpack=nginx at=erb-interpolate-config' | ||
erb config/nginx.conf.erb > config/nginx.conf | ||
|
||
echo 'buildpack=nginx at=nginx-start' | ||
bin/nginx -p . -c config/nginx.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
daemon off; | ||
# stay attached to the dyno process, run in Procfile / web | ||
|
||
pid /app/nginx.pid; | ||
# /app is $HOME & working directory of Heroku dyno | ||
|
||
error_log stderr info; | ||
# As documented for Nginx, but we still see error during start-up in log: | ||
# > nginx: [alert] could not open error log file: open() "./logs/error.log" | ||
|
||
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>; | ||
# Heroku dynos have at least 4 cores. | ||
|
||
events { | ||
use epoll; | ||
accept_mutex on; | ||
worker_connections <%= ENV['NGINX_WORKER_CONNECTIONS'] || 1024 %>; | ||
} | ||
|
||
http { | ||
gzip on; | ||
gzip_comp_level 2; | ||
gzip_min_length 512; | ||
gzip_proxied any; # Heroku router sends Via header | ||
|
||
server_tokens off; | ||
|
||
log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id'; | ||
access_log /dev/stdout l2met; | ||
# Remote IP, request path, HTTP status, & timestamp are all logged by Heroku Router, so not useful to include here. | ||
|
||
include mime.types; | ||
default_type application/octet-stream; | ||
sendfile on; | ||
|
||
client_body_timeout <%= ENV['NGINX_CLIENT_BODY_TIMEOUT'] || 5 %>; | ||
# Must read the body in 5 seconds. | ||
|
||
server { | ||
listen <%= ENV["PORT"] %>; | ||
server_name _; | ||
keepalive_timeout 5; | ||
client_max_body_size <%= ENV['NGINX_CLIENT_MAX_BODY_SIZE'] || 1 %>M; | ||
|
||
## HTTPS Only | ||
if ($http_x_forwarded_proto != "https") { | ||
return 301 https://$host$request_uri; | ||
} | ||
|
||
## Document root | ||
root /app/dist; | ||
|
||
location / { | ||
## Clean URLs: match on extensionless requests. | ||
# try_files $uri $uri/ $uri.html =404; | ||
|
||
## Single-page app client-side routing: returns index.html if the requested path doesn't exist. | ||
## When enabled, the client-side app must handle its own 404 errors. | ||
# error_page 404 = /index.html; | ||
} | ||
|
||
## Define specific behaviors for sub directories and other locations. | ||
# location /assets { | ||
# expires 7d; | ||
# } | ||
|
||
## Custom error pages | ||
# error_page 404 /404.html; | ||
# error_page 500 /500.html; | ||
} | ||
|
||
## Canonical Host: redirect to a canonical hostname. | ||
## Multiple server blocks may be used, one for each hostname to redirect from. | ||
# server { | ||
# server_name some-other-name.example.com; | ||
# return 301 https://canonical-name.example.com$request_uri; | ||
# } | ||
# server { | ||
# server_name yet-another-name.example.com; | ||
# return 301 https://canonical-name.example.com$request_uri; | ||
# } | ||
# … | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# Heroku Buildpack: Nginx local proxy to backend app servers | ||
|
||
Operates as an HTTP proxy to an app server running in the same dyno, via UNIX domain sockets. | ||
|
||
Add this buildpack to an app, as the last buildpack: | ||
```bash | ||
heroku buildpacks:add --app APP_NAME heroku-community/nginx | ||
``` | ||
|
||
Create the `Procfile` in the root of your app, containing, | ||
``` | ||
web: bin/start-nginx <backend server command> | ||
``` | ||
Example with backend server command: `bin/start-nginx bundle exec unicorn -c config/unicorn.rb` | ||
|
||
Backend server must: | ||
* Listen to the socket at `/tmp/nginx.socket`. | ||
* Touch `/tmp/app-initialized` when the backend is ready for traffic. | ||
|
||
The [default config `config/nginx.conf.erb`](config/nginx.conf.erb) will be loaded automatically. To customize, copy the default config to your app source code at `config/nginx.conf.erb` | ||
|
||
## Logging | ||
|
||
**Proxy mode writes logs to files, which is not the Heroku way. They should go to stdout.** | ||
|
||
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 | ||
``` | ||
### Setting custom log paths | ||
|
||
You can configure custom log paths using the environment variables `NGINX_ACCESS_LOG_PATH` and `NGINX_ERROR_LOG_PATH`. | ||
|
||
For example, if you wanted to stop nginx from logging your access logs you could set `NGINX_ACCESS_LOG_PATH` to `/dev/null`: | ||
```bash | ||
$ heroku config:set NGINX_ACCESS_LOG_PATH="/dev/null" | ||
``` | ||
|
||
## 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 | ||
``` | ||
|
||
### Proxy mode nginx-debug | ||
```bash | ||
$ cat Procfile | ||
web: bin/start-nginx-debug bundle exec unicorn -c config/unicorn.rb | ||
``` | ||
|
||
## Application/Dyno coordination | ||
|
||
Proxy mode will not start Nginx until a file has been written to `/tmp/app-initialized`. 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](#example-proxy-setup) show how/when you should write the file when working with Unicorn. | ||
|
||
## Force SSL | ||
|
||
You can add a redirect/force SSL based on Heroku headers. Full, commented example in the [default config file](config/nginx.conf.erb) or in the [nextjs with forceSSL config file](config/nginx-nextjs-with-forcessl.conf.erb). | ||
|
||
``` | ||
if ($http_x_forwarded_proto != "https") { | ||
return 301 https://$host$request_uri; | ||
} | ||
``` | ||
|
||
|
||
## Example Proxy Setup | ||
|
||
Here are 2 setup examples. One example for a new app, another for an existing app. In both cases, we are working with ruby & unicorn. Keep in mind that this buildpack is not ruby specific. However if your app does happen to use Ruby, make sure to add the Nginx buildpack **after** the Ruby buildpack, so the Nginx buildpack doesn't have to install its own redundant copy of Ruby for the ERB templating feature. | ||
|
||
### Existing App | ||
|
||
Update Buildpacks to use the latest stable version of this buildpack: | ||
```bash | ||
$ heroku buildpacks:add heroku-community/nginx | ||
``` | ||
Alternatively, you can use the Github URL of this repo if you want to edge version. | ||
|
||
Update Procfile: | ||
``` | ||
web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb | ||
``` | ||
```bash | ||
$ git add Procfile | ||
$ git commit -m 'Update procfile for Nginx buildpack' | ||
``` | ||
Update Unicorn Config | ||
```ruby | ||
require 'fileutils' | ||
listen '/tmp/nginx.socket' | ||
before_fork do |server,worker| | ||
FileUtils.touch('/tmp/app-initialized') | ||
end | ||
``` | ||
```bash | ||
$ git add config/unicorn.rb | ||
$ git commit -m 'Update unicorn config to listen on Nginx socket.' | ||
``` | ||
Deploy Changes | ||
```bash | ||
$ git push heroku main | ||
``` | ||
|
||
### New App | ||
|
||
```bash | ||
$ mkdir myapp; cd myapp | ||
$ git init | ||
``` | ||
|
||
**Gemfile** | ||
```ruby | ||
source 'https://rubygems.org' | ||
gem 'unicorn' | ||
``` | ||
|
||
**config.ru** | ||
```ruby | ||
run Proc.new {[200,{'Content-Type' => 'text/plain'}, ["hello world"]]} | ||
``` | ||
|
||
**config/unicorn.rb** | ||
```ruby | ||
require 'fileutils' | ||
preload_app true | ||
timeout 5 | ||
worker_processes 4 | ||
listen '/tmp/nginx.socket', backlog: 1024 | ||
|
||
before_fork do |server,worker| | ||
FileUtils.touch('/tmp/app-initialized') | ||
end | ||
``` | ||
Install Gems | ||
```bash | ||
$ bundle install | ||
``` | ||
Create Procfile | ||
``` | ||
web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb | ||
``` | ||
Create & Push Heroku App: | ||
```bash | ||
$ heroku create | ||
$ heroku buildpacks:add heroku/ruby | ||
$ heroku buildpacks:add heroku-community/nginx | ||
$ git add . | ||
$ git commit -am "init" | ||
$ git push heroku main | ||
$ heroku logs -t | ||
``` | ||
Visit App | ||
``` | ||
$ heroku open | ||
``` | ||
|
||
## Original Motivation | ||
|
||
Some application servers (e.g. Ruby's Unicorn) halt progress when dealing with network I/O. Heroku's routing stack [buffers only the headers](https://devcenter.heroku.com/articles/http-routing#request-buffering) of inbound requests. (The router will buffer the headers and body of a response up to 1MB) Thus, the Heroku router engages the dyno during the entire body transfer –from the client to dyno. For applications servers with blocking I/O, the latency per request will be degraded by the content transfer. By using Nginx in front of the application server, we can eliminate a great deal of transfer time from the application server. In addition to making request body transfers more efficient, all other I/O should be improved since the application server need only communicate with a UNIX socket on localhost. Basically, for webservers that are not designed for efficient, non-blocking I/O, we will benefit from having Nginx to handle all I/O operations. | ||
|
||
## License | ||
Copyright (c) 2013 Ryan R. Smith | ||
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. |
Oops, something went wrong.