Skip to content

Commit 48745a3

Browse files
committed
suggest first middleware position in documentation
and other documentation improvements
1 parent 233ea26 commit 48745a3

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

README.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ gem "rack-reverse-proxy", require: "rack/reverse_proxy"
1818

1919
## Usage
2020

21-
Rules can be a regex or a string. If a regex is used, you can use the subcaptures in your forwarding url by denoting them with a `$`.
21+
`Rack::ReverseProxy` should ideally be the very first middleware in your
22+
stack. In a typical use case it is being used to proxy an entirely
23+
different website through your application, so it's unlikely that you will want
24+
any other middleware to modify the requests or responses. The examples below
25+
reflect this.
2226

23-
Right now if more than one rule matches any given route, it throws an exception for an ambiguous match. This will probably change later. If no match is found, the call is forwarded to your application.
2427

25-
Below is an example for configuring the middleware:
28+
### Generic Rack app example
2629

2730
```ruby
2831
require 'rack/reverse_proxy'
@@ -44,6 +47,37 @@ end
4447
run app
4548
```
4649

50+
### Ruby on Rails app example
51+
52+
This example use `config.middleware.insert(0` to ensure that
53+
`Rack::ReverseProxy` is first in the stack. It is possible that
54+
other code in your app (usually in application.rb, development.rb, or production.rb)
55+
will take over this position in the stack. To ensure
56+
that this is not the case, view the stack by running `rails middleware`. You should see
57+
`Rack::ReverseProxy` at the top. Note that
58+
the middleware stack will likely differ slightly in each environment. All that said, it's a pretty
59+
safe bet to put the below code into application.rb.
60+
61+
```ruby
62+
# config/application.rb
63+
config.middleware.insert(0, Rack::ReverseProxy) do
64+
reverse_proxy_options preserve_host: true
65+
reverse_proxy '/wiki', 'http://wiki.example.com/'
66+
end
67+
```
68+
69+
### Rules
70+
71+
As seen in the Rack example above, `reverse_proxy` can be invoked multiple times with
72+
different rules, which will be commulatively added.
73+
74+
Rules can be a regex or a string. If a regex is used, you can use the subcaptures in your forwarding url by denoting them with a `$`.
75+
76+
Right now if more than one rule matches any given route, it throws an exception for an ambiguous match. This will probably change later. If no match is found, the call is forwarded to your application.
77+
78+
79+
### Options
80+
4781
`reverse_proxy_options` sets global options for all reverse proxies. Available options are:
4882

4983
* `:preserve_host` Set to false to omit Host headers
@@ -56,28 +90,6 @@ run app
5690
* `:x_forwarded_headers` sets up proper `X-Forwarded-*` headers. Default: true.
5791
* `:preserve_encoding` Set to true to pass Accept-Encoding header to proxy server. Default: false.
5892

59-
### Sample usage in a Ruby on Rails app
60-
61-
Rails 3 or less:
62-
63-
```ruby
64-
# config/application.rb
65-
config.middleware.insert_before(Rack::Lock, Rack::ReverseProxy) do
66-
reverse_proxy_options preserve_host: true
67-
reverse_proxy '/wiki', 'http://wiki.example.com/'
68-
end
69-
```
70-
71-
Rails 4+ or if you use `config.threadsafe`, you'll need to `insert_before(Rack::Runtime, Rack::ReverseProxy)` as `Rack::Lock` does not exist when `config.allow_concurrency == true`:
72-
73-
```ruby
74-
# config/application.rb
75-
config.middleware.insert_before(Rack::Runtime, Rack::ReverseProxy) do
76-
reverse_proxy_options preserve_host: true
77-
reverse_proxy '/wiki', 'http://wiki.example.com/'
78-
end
79-
```
80-
8193
## Note on Patches/Pull Requests
8294
* Fork the project.
8395
* Make your feature addition or bug fix.

0 commit comments

Comments
 (0)