• Stars
    star
    194
  • Rank 200,219 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Reverse Proxy for Rack

A Reverse Proxy for Rack

TravisCI

This is a simple reverse proxy for Rack that pretty heavily rips off Rack Forwarder. It is not meant for production systems (although it may work), as the webserver fronting your app is generally much better at this sort of thing.

Installation

The gem is available on rubygems. Assuming you have a recent version of Rubygems you should just be able to install it via:

gem install rack-reverse-proxy

For your Gemfile use:

gem "rack-reverse-proxy", require: "rack/reverse_proxy"

Usage

Rack::ReverseProxy should ideally be the very first middleware in your stack. In a typical use case it is being used to proxy an entirely different website through your application, so it's unlikely that you will want any other middleware to modify the requests or responses. The examples below reflect this.

Generic Rack app example

require 'rack/reverse_proxy'

use Rack::ReverseProxy do
  # Set :preserve_host to true globally (default is true already)
  reverse_proxy_options preserve_host: true

  # Forward the path /test* to http://example.com/test*
  reverse_proxy '/test', 'http://example.com/'

  # Forward the path /foo/* to http://example.com/bar/*
  reverse_proxy /^\/foo(\/.*)$/, 'http://example.com/bar$1', username: 'name', password: 'basic_auth_secret'
end

app = proc do |env|
  [ 200, {'Content-Type' => 'text/plain'}, ["b"] ]
end
run app

Ruby on Rails app example

This example use config.middleware.insert(0 to ensure that Rack::ReverseProxy is first in the stack. It is possible that other code in your app (usually in application.rb, development.rb, or production.rb) will take over this position in the stack. To ensure that this is not the case, view the stack by running rails middleware. You should see Rack::ReverseProxy at the top. Note that the middleware stack will likely differ slightly in each environment. All that said, it's a pretty safe bet to put the below code into application.rb.

# config/application.rb
config.middleware.insert(0, Rack::ReverseProxy) do
  reverse_proxy_options preserve_host: true
  reverse_proxy '/wiki', 'http://wiki.example.com/'
end

Rules

As seen in the Rack example above, reverse_proxy can be invoked multiple times with different rules, which will be commulatively added.

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 $.

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.

Options

reverse_proxy_options sets global options for all reverse proxies. Available options are:

  • :preserve_host Set to false to omit Host headers
  • :username username for basic auth
  • :password password for basic auth
  • :matching is a global only option, if set to :first the first matched url will be requested (no ambigous error). Default: :all.
  • :timeout seconds to timout the requests
  • :force_ssl redirects to ssl version, if not already using it (requires :replace_response_host). Default: false.
  • :verify_mode the OpenSSL::SSL verify mode passed to Net::HTTP. Default: OpenSSL::SSL::VERIFY_PEER.
  • :x_forwarded_headers sets up proper X-Forwarded-* headers. Default: true.
  • :stripped_headers Array of headers that should be stripped before forwarding reqeust. Default: nil. e.g. stripped_headers: ["Accept-Encoding", "Foo-Bar"]

If reverse_proxy_options is invoked multiple times, the invocations will have a commulative effect, only overwritting the values which they specify. Example of how this could be useful:

config.middleware.insert(0, Rack::ReverseProxy) do
  reverse_proxy_options preserve_host: false
  if Rails.env.production? or Rails.env.staging?
    reverse_proxy_options force_ssl: true, replace_response_host: true
  end
  reverse_proxy /^\/blog(\/?.*)$/, 'http://blog.example.com/blog$1'
end

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Contributors

  • Jon Swope, creator
  • Oleksii Fedorov, maintainer

More Repositories

1

active_record.cr

Active Record pattern implementation for Crystal.
Crystal
191
star
2

Challenge-Build-Your-Own-Array-In-Js

This is a challenge that will allow you to practice your logical, analytical and problem-solving skills. Additionally, by the end of it you’ll have much better command of arrays in javascript.
JavaScript
177
star
3

rspec-json_expectations

Set of matchers and helpers to allow you test your APIs responses like a pro.
Gherkin
138
star
4

spec2.cr

Enhanced `spec` testing library for [Crystal](http://crystal-lang.org/).
Crystal
103
star
5

mocks.cr

General purpose mocking library for Crystal.
Crystal
51
star
6

crystal-mysql

Basic mysql bindings for crystal.
Crystal
32
star
7

timecop.cr

Mock with `Time.now` with the power of time travel, time freeze and time scale.
Crystal
19
star
8

refactoring-koans-js

Refactoring Koans to help you learn to refactor code smells in javascript
JavaScript
17
star
9

query.cr

Query abstraction for Crystal Language. Used by active_record.cr library.
Crystal
13
star
10

quick.cr

QuickCheck implementation for Crystal Language
Crystal
11
star
11

kotlin-spring-boot-mvc-starter

This is a starter repository for work with Kotlin on Back-end using Spring Boot 2 MVC, JdbcTemplate, Thymeleaf, Emails with Thymeleaf templates, Spring Security, Feature/UI tests using Fluentlenium, Clean Controller->Service->Repository pattern that is a sweet spot as your starting architecture. Includes a small demo in its source code.
Kotlin
11
star
12

postgres_adapter.cr

Postgres adapter for [active_record.cr](https://github.com/waterlink/active_record.cr). Uses [crystal-pg](https://github.com/will/crystal-pg) driver.
Crystal
9
star
13

spec2-mocks.cr

This library connects spec2.cr and mocks.cr, effectively enabling 'have_received' expectation for spec2.
Crystal
8
star
14

expand.cr

Crystal tool for macro debugging. Allows one to expand macro recursively.
Shell
6
star
15

mysql_adapter.cr

Mysql adapter for [active_record.cr](https://github.com/waterlink/active_record.cr). Uses [crystal-mysql library](https://github.com/waterlink/crystal-mysql)
Crystal
6
star
16

timestamp.cr

Timestamps in crystal-lang. Adds `.from_timestamp` and `#to_timestamp` methods to `Time`
Crystal
5
star
17

quizzykotlin

An example application for my free Getting Started With Kotlin Tutorial
Kotlin
4
star
18

rebecca

Simple database convenience wrapper for Go language.
Go
4
star
19

BuildYourOwnTestingFrameworkPart1

This is a source code for the first part of "Build Your Own Testing Framework" series
JavaScript
4
star
20

singleton.cr

Singleton library for Crystal Language.
Crystal
3
star
21

devpoll

devpoll - small web application for making polls written in http://crystal-lang.org/ (Crystal lang)
Crystal
3
star
22

aop

Very thin AOP gem for Ruby
Ruby
3
star
23

four-cycles-of-tdd-lightning-talk

Slides for my lightning talk: 4 Cycles of Test-Driven Development
HTML
3
star
24

money_tracking

CLI tool for tracking your expenses.
Ruby
2
star
25

restricted_struct

RestrictedStruct gem: create Struct-s with private or protected attributes
Ruby
2
star
26

contracts-rspec

Plugin for contracts.ruby that fixes issues with rspec-mocks.
Ruby
2
star
27

openproject-docker

Let OpenProject run in a docker container
Shell
2
star
28

race-conditions-testing-example

Kotlin
2
star
29

plugged

Library for writing extendable CLI applications for Golang.
Go
2
star
30

tspp_project

C++
2
star
31

messenger-ES6

JavaScript
1
star
32

tdd-talk-ru

TDD talk (version in Russian, for Top-Engineer webinar). | Доклад о TDD, для вебинара Top-Engineer.
HTML
1
star
33

goactor

Thin Actor implementation in Golang
Go
1
star
34

confident.ruby

Be confident and narrative when writing code in ruby
Ruby
1
star
35

secrets

Simple bash script to manage your secrets with symmetric key.
Shell
1
star
36

es-snapshot

This is a small script to make an elasticsearch snapshot. Tested with TDD in Bash in a very esoteric way.
Shell
1
star
37

crystal-blog

Simple blog in crystal using frank and active_record.cr
Crystal
1
star
38

cars-droid

Clojure
1
star
39

ShellTools

My bash shell tools. Unit-tested.
Shell
1
star
40

likes

Give it a list of people and their likings and it will tell what else could these people like. Ruby gem.
Ruby
1
star
41

property-based-testing-talk

HTML
1
star
42

docker-elasticsearch-kubernetes-aws

Docker image for elasticsearch with support for Kubernetes and AWS cloud
Shell
1
star
43

namegen.cr

This library provides facilities for generating random names/nicknames. Written in Crystal-Lang.
Crystal
1
star
44

stomp

Example game engine in Ruby. Uses Component Entity System + World paradigm.
Ruby
1
star
45

elm-todo

Simple ToDo application written in Elm lang. No backend (yet?).
Elm
1
star
46

explorative-tdd-talk

CSS
1
star
47

gas-template

Google Apps Script template for more comfortable development on local machine. Plus CI/CD included.
Shell
1
star
48

cars_api

Ruby
1
star
49

strong_ruby

Just playing around with strong typing in Ruby
Ruby
1
star
50

contracts-non_intrusive

Less intrusive version of Contracts DSL. Allows to use static/dynamic code analysis tools.
Ruby
1
star
51

LoginSignupE2E

Example project for the blog post "Learning TDD with JS: End-to-End Testing". Implements Login and Signup for a web-application using TDD and E2E feature tests.
JavaScript
1
star
52

recorder

Daemon that records all incoming HTTP request and allow to fetch them and put expectations on them. Useful for CLI tools testing.
Go
1
star