You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
22
26
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.
24
27
25
-
Below is an example for configuring the middleware:
28
+
### Generic Rack app example
26
29
27
30
```ruby
28
31
require'rack/reverse_proxy'
@@ -44,6 +47,37 @@ end
44
47
run app
45
48
```
46
49
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
+
47
81
`reverse_proxy_options` sets global options for all reverse proxies. Available options are:
48
82
49
83
*`:preserve_host` Set to false to omit Host headers
@@ -56,28 +90,6 @@ run app
56
90
*`:x_forwarded_headers` sets up proper `X-Forwarded-*` headers. Default: true.
57
91
*`:preserve_encoding` Set to true to pass Accept-Encoding header to proxy server. Default: false.
58
92
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
0 commit comments