ez-nginx-proxy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +2 -0
- data/bin/ez-nginx-proxy +141 -0
- data/example_config.rb +4 -0
- data/ez-nginx-proxy.gemspec +20 -0
- data/lib/ez-nginx-proxy/version.rb +3 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Caleb Spare
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# ez-nginx-proxy
|
2
|
+
|
3
|
+
ez-nginx-proxy is a small wrapper around nginx. It requires nginx to already be installed. It is useful for
|
4
|
+
development, when you want to very easily start a proxy that will send requests to various other
|
5
|
+
development/production servers based on matching the routes.
|
6
|
+
|
7
|
+
ez-nginx-proxy takes a very simple Ruby configuration file. Here is an example configuration:
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
[
|
11
|
+
["/foo/bar", "localhost:1234"],
|
12
|
+
["/", "amazon.com"]
|
13
|
+
]
|
14
|
+
```
|
15
|
+
|
16
|
+
If you run this configuration like this:
|
17
|
+
|
18
|
+
$ ez-nginx-proxy -p 9000 my_config.rb
|
19
|
+
|
20
|
+
ez-nginx-proxy will create a temporary nginx configuration file that redirects `localhost:9000/foo/bar` to
|
21
|
+
`localhost:1234/foo/bar` and `localhost:9000/blah` to `amazon.com/blah`.
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
$ ez-nginx-proxy your_config.rb
|
26
|
+
|
27
|
+
See `ez-nginx-proxy -h` for all the options.
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
Install nginx. You can do this using your favorite package manager:
|
32
|
+
|
33
|
+
$ sudo apt-get install nginx
|
34
|
+
$ brew install nginx
|
35
|
+
# or whatever
|
36
|
+
|
37
|
+
Now install ez-nginx-proxy
|
38
|
+
|
39
|
+
$ gem install ez-nginx-proxy
|
40
|
+
|
41
|
+
## NOTE:
|
42
|
+
|
43
|
+
You will probably see this error when ez-nginx-proxy starts:
|
44
|
+
|
45
|
+
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
|
46
|
+
|
47
|
+
This is because nginx initially tries to log to `/var/log/nginx/error.log` before it starts up. See [this
|
48
|
+
nginx config page](http://wiki.nginx.org/CoreModule#error_log) for more information.
|
49
|
+
|
50
|
+
|
51
|
+
## TODO
|
52
|
+
|
53
|
+
* nginx regex matching
|
54
|
+
* pattern replacement?
|
55
|
+
* Hide the annoying 'permission denied' error
|
data/Rakefile
ADDED
data/bin/ez-nginx-proxy
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# For developing the gem
|
4
|
+
#$:.unshift(File.join(File.dirname(__FILE__), "../lib"))
|
5
|
+
|
6
|
+
require "erb"
|
7
|
+
require "securerandom"
|
8
|
+
require "fileutils"
|
9
|
+
|
10
|
+
require "trollop"
|
11
|
+
require "dedent"
|
12
|
+
require "colorize"
|
13
|
+
|
14
|
+
require "ez-nginx-proxy/version"
|
15
|
+
|
16
|
+
options = Trollop.options do
|
17
|
+
version EzNginxProxy::VERSION
|
18
|
+
banner <<-EOS.dedent
|
19
|
+
ez-nginx-proxy is a very simple reverse http proxy that can route requests based on pattern-matching the
|
20
|
+
URI. The configuration file is Ruby, and it must have the following form:
|
21
|
+
|
22
|
+
[
|
23
|
+
["/foo/bar", "localhost:1234"], # Route requests to /foo/bar to a local server
|
24
|
+
["/", "amazon.com"] # / matches every request
|
25
|
+
]
|
26
|
+
|
27
|
+
Usage:
|
28
|
+
|
29
|
+
$ ez-nginx-proxy [options] CONFIG_FILE
|
30
|
+
|
31
|
+
where [options] are:
|
32
|
+
EOS
|
33
|
+
opt :address, "Bind address", :default => "0.0.0.0"
|
34
|
+
opt :port, "Port", :default => 9876
|
35
|
+
opt :nginx_location, "Location of nginx", :default => `which nginx`
|
36
|
+
opt :version, "Show ez-nginx-proxy version"
|
37
|
+
end
|
38
|
+
|
39
|
+
Trollop.die "You must provide a configuration file" unless ARGV[0]
|
40
|
+
Trollop.die "Invalid config file: #{ARGV[0]}" unless File.file? ARGV[0]
|
41
|
+
begin
|
42
|
+
routes = eval(File.read(ARGV[0]))
|
43
|
+
rescue
|
44
|
+
Trollop.die "Error reading or parsing the config file #{ARGV[0]}"
|
45
|
+
end
|
46
|
+
|
47
|
+
# Locate nginx
|
48
|
+
nginx = nil
|
49
|
+
([options[:nginx_location]] + %w(/usr/sbin/nginx /usr/local/sbin/nginx)).each do |guess_location|
|
50
|
+
if File.file? guess_location
|
51
|
+
nginx = guess_location
|
52
|
+
break
|
53
|
+
end
|
54
|
+
end
|
55
|
+
unless nginx
|
56
|
+
abort <<-EOS.dedent
|
57
|
+
Unable to locate nginx. Please install it using
|
58
|
+
|
59
|
+
$ sudo apt-get install nginx # Ubuntu
|
60
|
+
|
61
|
+
or
|
62
|
+
|
63
|
+
$ brew install nginx # Mac OS / homebrew
|
64
|
+
|
65
|
+
or similar. If nginx is installed to a nonstandard location, please pass that in with the --nginx-location
|
66
|
+
option.
|
67
|
+
EOS
|
68
|
+
end
|
69
|
+
|
70
|
+
# Locate the nginx configuration root
|
71
|
+
config_output = `#{nginx} -V 2>&1`
|
72
|
+
puts "\033[01;34m>>>> config_output: #{config_output.inspect}\e[m"
|
73
|
+
match = config_output.match(/--prefix=([\S]+)/)
|
74
|
+
unless match && match.size == 2
|
75
|
+
abort "Unable to determine the nginx prefix root from the output of `#{nginx} -V`."
|
76
|
+
end
|
77
|
+
prefix = match[1]
|
78
|
+
|
79
|
+
routes.each do |pattern, target|
|
80
|
+
puts "Proxying `#{pattern}` to #{target}".green
|
81
|
+
end
|
82
|
+
|
83
|
+
template = ERB.new <<-EOS.dedent
|
84
|
+
# Template for nginx config
|
85
|
+
# Run in the foreground with no supervisor
|
86
|
+
|
87
|
+
daemon off;
|
88
|
+
master_process off;
|
89
|
+
worker_processes 1;
|
90
|
+
error_log stderr warn;
|
91
|
+
pid <%= pid_file %>
|
92
|
+
;
|
93
|
+
|
94
|
+
events {
|
95
|
+
worker_connections 768;
|
96
|
+
}
|
97
|
+
|
98
|
+
http {
|
99
|
+
# Basic Settings copied from the default ubuntu nginx.conf
|
100
|
+
sendfile on;
|
101
|
+
tcp_nopush on;
|
102
|
+
tcp_nodelay on;
|
103
|
+
keepalive_timeout 65;
|
104
|
+
types_hash_max_size 2048;
|
105
|
+
|
106
|
+
include <%= prefix %>/mime.types;
|
107
|
+
|
108
|
+
default_type application/octet-stream;
|
109
|
+
|
110
|
+
# Write access logs. Obviously, this is slow.
|
111
|
+
access_log /dev/stdout;
|
112
|
+
|
113
|
+
gzip on;
|
114
|
+
|
115
|
+
proxy_temp_path <%= prefix_dir %>;
|
116
|
+
server {
|
117
|
+
listen <%= options[:port] %>;
|
118
|
+
server_name localhost;
|
119
|
+
|
120
|
+
<% routes.each do |pattern, target| %>
|
121
|
+
location <%= pattern %> {
|
122
|
+
proxy_pass <%= target %>;
|
123
|
+
}
|
124
|
+
<% end %>
|
125
|
+
}
|
126
|
+
}
|
127
|
+
EOS
|
128
|
+
|
129
|
+
prefix_dir = "/tmp/ez-nginx-#{SecureRandom.hex}/"
|
130
|
+
FileUtils.mkdir(prefix_dir)
|
131
|
+
pid_file = File.join(prefix_dir, "ez-nginx.pid")
|
132
|
+
config_file = File.join(prefix_dir, "ez-nginx.conf")
|
133
|
+
|
134
|
+
config_file_contents = template.result(binding)
|
135
|
+
File.open(config_file, "w") { |f| f.write(config_file_contents) }
|
136
|
+
|
137
|
+
pid = Process.spawn("#{nginx} -p #{prefix_dir} -c ez-nginx.conf")
|
138
|
+
trap("SIGINT") { FileUtils.rm_rf(prefix_dir) }
|
139
|
+
Process.wait(pid)
|
140
|
+
# Clean up
|
141
|
+
FileUtils.rm_rf(prefix_dir)
|
data/example_config.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ez-nginx-proxy/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Caleb Spare"]
|
6
|
+
gem.email = ["cespare@gmail.com"]
|
7
|
+
gem.description = %q{A very simple-to-use nginx wrapper for reverse proxying}
|
8
|
+
gem.summary = %q{A very simple-to-use nginx wrapper for reverse proxying}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.name = "ez-nginx-proxy"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = EzNginxProxy::VERSION
|
16
|
+
|
17
|
+
gem.add_dependency "trollop"
|
18
|
+
gem.add_dependency "colorize"
|
19
|
+
gem.add_dependency "dedent"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ez-nginx-proxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Caleb Spare
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: trollop
|
16
|
+
requirement: &24225920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *24225920
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: colorize
|
27
|
+
requirement: &24273800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *24273800
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: dedent
|
38
|
+
requirement: &24273380 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *24273380
|
47
|
+
description: A very simple-to-use nginx wrapper for reverse proxying
|
48
|
+
email:
|
49
|
+
- cespare@gmail.com
|
50
|
+
executables:
|
51
|
+
- ez-nginx-proxy
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- bin/ez-nginx-proxy
|
61
|
+
- example_config.rb
|
62
|
+
- ez-nginx-proxy.gemspec
|
63
|
+
- lib/ez-nginx-proxy/version.rb
|
64
|
+
homepage: ''
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.10
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: A very simple-to-use nginx wrapper for reverse proxying
|
88
|
+
test_files: []
|
89
|
+
has_rdoc:
|