vagrant-reverse-proxy 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58327c38d18a0e56fe6daaf3614ad13b748aee25
4
- data.tar.gz: ab5b2297ac9c441bfe7f25c8efa6a3efd8dff978
3
+ metadata.gz: fc6bfbd2be5d73661529df41828a6f2bdbfacdc3
4
+ data.tar.gz: e3aad94f72a27c83b4c9cf4838109680ebb2d1fc
5
5
  SHA512:
6
- metadata.gz: c2edcff7ab0cd85ee985c67aacbf3ce5f62079cb190c15761ca2ccfcf9491fff712a2a5a72a34ce89e3247e31413ed5189b241d566d12d34890a8d50619db9e1
7
- data.tar.gz: 7207d37dfa085633b7a528cd5b6eb9d0a7b6df2af8ba78f082f8333a452ec9488a46703246f98cef72024d57dddabbe2e79d55565a503c9053f943e23d70ee7e
6
+ metadata.gz: 444eb3425dcea8535e3ee9bdbb9e9b305438f209829c3c90c317e7315188612ee210b0b1d0519d0e62357d5925c52bed4a8fb7970e3dd7161aa6a98c5d916802
7
+ data.tar.gz: 047e6b869a09fb2a68a59227243943eb1850c48924a022027f93e34461c8b1e5ca7b446d0f8fea7dfff6a274abc450b1aaf2809a7f8a992f50b6e91ae2d1057d
data/README.md CHANGED
@@ -25,18 +25,19 @@ Install the plugin as usual:
25
25
 
26
26
  First, install NGINX and create a configuration as usual. Then, in
27
27
  the `server` configuration block for the host you want to use for
28
- proxying, simply put `include "vagrant-proxy-config";` in the file.
28
+ proxying, simply put `include "vagrant-proxy-config-locations";` in the file.
29
29
 
30
30
  If you don't need anything specific, just put the following in
31
31
  `/etc/nginx/sites-enabled/default`:
32
32
 
33
33
  server {
34
- listen [::]:80 default ipv6only=off;
34
+ listen 80 default;
35
+ listen [::]:80 default;
35
36
  # This is the fallback server
36
37
  server_name default;
37
38
  # Redirect http://localhost/hostname/lalala
38
39
  # to http://hostname/lalala
39
- include "vagrant-proxy-config";
40
+ include "vagrant-proxy-config-locations";
40
41
  }
41
42
 
42
43
  This will load the `/etc/nginx/vagrant-proxy-config` file which is
@@ -46,9 +47,21 @@ proxy to port 80 on the virtual machine with a `config.vm.hostname`
46
47
  value of `foo`. This is only done for virtual machines that have
47
48
  `config.reverse_proxy.enabled` set to `true` in their config.
48
49
 
49
- Whenever you bring up or halt a machine, the plugin updates the proxy
50
- config file and invokes `sudo systemctl reload nginx` to make the
51
- change immediately visible.
50
+ #### Server Blocks
51
+
52
+ The plugin also writes `server` block configuration for the enabled
53
+ VMs so that they can be accessed directly via their hostname (as long
54
+ as the hostname resolves to the host machine's IP address).
55
+
56
+ To include these, simply include the generated file inside your main NGINX `http` block:
57
+
58
+ http {
59
+ include "vagrant-proxy-config-servers";
60
+ }
61
+
62
+
63
+ Whenever you bring up, halt, or reload a machine, the plugin updates the proxy
64
+ config files and invokes `sudo nginx -s reload` to make the change immediately visible.
52
65
 
53
66
  ### Custom host names
54
67
 
@@ -72,15 +85,25 @@ an array:
72
85
  As you can see, this allows you to define which port to connect to
73
86
  instead of the default port (which is port 80).
74
87
 
75
- ### Specifying the NGINX configuration file path
88
+ ### Specifying the NGINX configuration file paths
89
+
90
+ If you want to change the location of the managed NGINX configuration
91
+ files, set the `config.reverse_proxy.nginx_locations_config_file` or
92
+ `config.reverse_proxy.nginx_servers_config_file` values to paths on
93
+ your host machine in the Vagrantfile configuration:
76
94
 
77
- If you want to change the location of the managed nginx configuration file, set the `config.reverse_proxy.nginx_config_file` value to a path on your host machine in the Vagrantfile configuration:
95
+ config.reverse_proxy.nginx_locations_config_file = '/usr/local/etc/nginx/vagrant-proxy-config-locations'
96
+ config.reverse_proxy.nginx_servers_config_file = '/usr/local/etc/nginx/vagrant-proxy-config-servers'
78
97
 
79
- config.reverse_proxy.nginx_config_file = '/usr/local/etc/nginx/vagrant-proxy-config'
98
+ If you don't want to generate one of the locations or server configuration files, set the appropriate config value to `nil`.
80
99
 
81
100
  ### Specifying the NGINX reload command
82
101
 
83
- After the NGINX configuration file is generated, a reload command is executed so that the changes take effect. By default the command executed is `sudo nginx -s reload`. If you need to change this, set the `config.reverse_proxy.nginx_reload_command` option to the command to be executed:
102
+ After the NGINX configuration file is generated, a reload command is
103
+ executed so that the changes take effect. By default the command
104
+ executed is `sudo nginx -s reload`. If you need to change this, set
105
+ the `config.reverse_proxy.nginx_reload_command` option to the command
106
+ to be executed:
84
107
 
85
108
  config.reverse_proxy.nginx_reload_command = 'sudo service nginx reload'
86
109
 
@@ -113,6 +136,11 @@ add a bit of custom code there if you need to override the base URL.
113
136
 
114
137
  ## Changelog
115
138
 
139
+ - 0.4.0 Add support for `vagrant reload`, add support for host-based instead
140
+ of location-based dispatching (both thanks to Sam Stevens). NOTE: This is a
141
+ backwards incompatible change: the default name of the config file has
142
+ changed, so you must update the `include` statement in your NGINX config.
143
+ - 0.3.1 Allow overriding the NGINX reload command (thanks to Sam Stevens).
116
144
  - 0.3 Allow overriding the location of the NGINX configuration file
117
145
  (thanks to Sam Stevens). Support multiple VMs in a single Vagrant
118
146
  config (suggested by Nicholas Alipaz).
@@ -10,26 +10,39 @@ module VagrantPlugins
10
10
  def call(env)
11
11
  @app.call(env)
12
12
 
13
+ # Get the plugin config for the machine
14
+ rp_config = env[:machine].config.reverse_proxy
15
+
13
16
  # Does this make much sense? What if we disable it later
14
17
  # for one specific machine? Then, the config should still
15
18
  # be removed.
16
- return unless env[:machine].config.reverse_proxy.enabled?
19
+ return unless rp_config.enabled?
20
+
21
+ env[:ui].info('Updating nginx configuration. Administrator privileges will be required...')
22
+
23
+ # Generate the config files
24
+ generate_config_file(env, rp_config.nginx_locations_config_file, location_block(env[:machine]))
25
+ generate_config_file(env, rp_config.nginx_servers_config_file, server_block(env[:machine]))
26
+
27
+ # And reload nginx
28
+ nginx_reload_command = rp_config.nginx_reload_command || 'sudo nginx -s reload'
29
+ Kernel.system(nginx_reload_command)
30
+ end
17
31
 
18
- # Determine temp file and target file
19
- nginx_config_file = env[:machine].config.reverse_proxy.nginx_config_file || '/etc/nginx/vagrant-proxy-config'
32
+ def generate_config_file(env, target_file, config_block)
20
33
 
21
- # Get the directory of the config file
22
- nginx_config_dir = File.dirname(nginx_config_file)
34
+ return unless target_file
23
35
 
24
- unless File.directory?(nginx_config_dir)
25
- env[:ui].error("Could not update nginx configuration: directory '#{nginx_config_dir}' does not exist. Continuing without proxy...")
36
+ # Get the directory of the file being written to
37
+ file_dir = File.dirname(target_file)
38
+
39
+ unless File.directory?(file_dir)
40
+ env[:ui].error("Could not update nginx configuration file '#{target_file}' : directory '#{file_dir}' does not exist. Continuing...")
26
41
  return
27
42
  end
28
43
 
29
44
  tmp_file = env[:machine].env.tmp_path.join('nginx.vagrant-proxies')
30
45
 
31
- env[:ui].info('Updating nginx configuration. Administrator privileges will be required...')
32
-
33
46
  sm = start_marker(env[:machine])
34
47
  em = end_marker(env[:machine])
35
48
 
@@ -39,7 +52,7 @@ module VagrantPlugins
39
52
  # changed, and might not have been present originally.
40
53
  File.open(tmp_file, 'w') do |new|
41
54
  begin
42
- File.open(nginx_config_file, 'r') do |old|
55
+ File.open(target_file, 'r') do |old|
43
56
  # First, remove old entries for this machine.
44
57
  while ln = old.gets() do
45
58
  if sm == ln.chomp
@@ -61,20 +74,17 @@ module VagrantPlugins
61
74
  if @action == :add # Removal is already (always) done above
62
75
  # Write the config for this machine
63
76
  if env[:machine].config.reverse_proxy.enabled?
64
- new.write(sm+"\n"+server_block(env[:machine])+em+"\n")
77
+ new.write(sm+"\n"+config_block+em+"\n")
65
78
  end
66
79
  end
67
80
  end
68
81
 
69
82
  # Finally, copy tmp config to actual config
70
- Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_config_file)
83
+ Kernel.system('sudo', 'cp', tmp_file.to_s, target_file)
71
84
 
72
- # And reload nginx
73
- nginx_reload_command = env[:machine].config.reverse_proxy.nginx_reload_command || 'sudo nginx -s reload'
74
- Kernel.system(nginx_reload_command)
75
85
  end
76
86
 
77
- def server_block(machine)
87
+ def location_block(machine)
78
88
  if machine.config.reverse_proxy.vhosts
79
89
  vhosts = machine.config.reverse_proxy.vhosts
80
90
  else
@@ -92,6 +102,8 @@ location /#{path}/ {
92
102
  proxy_set_header X-Forwarded-For $remote_addr;
93
103
  proxy_set_header X-Forwarded-Host $host;
94
104
  proxy_set_header X-Forwarded-Port $server_port;
105
+ proxy_set_header X-Real-IP $remote_addr;
106
+ proxy_set_header X-Forwarded-Proto $scheme;
95
107
  proxy_set_header X-Base-Url http://$host:$server_port/#{path}/;
96
108
 
97
109
  proxy_pass http://#{ip}#{port_suffix}/;
@@ -101,6 +113,40 @@ EOF
101
113
  end.join("\n")
102
114
  end
103
115
 
116
+ def server_block(machine)
117
+ if machine.config.reverse_proxy.vhosts
118
+ vhosts = machine.config.reverse_proxy.vhosts
119
+ else
120
+ host = machine.config.vm.hostname || machine.name
121
+ vhosts = {host => host}
122
+ end
123
+ ip = get_ip_address(machine)
124
+ vhosts.collect do |path, vhost|
125
+ # Rewrites are matches literally by nginx, which means
126
+ # http://host:80/... will NOT match http://host/...!
127
+ port_suffix = vhost[:port] == 80 ? '' : ":#{vhost[:port]}"
128
+ <<EOF
129
+ server {
130
+ listen 80;
131
+ listen [::]:80;
132
+ listen 443 ssl;
133
+ listen [::]:443 ssl;
134
+
135
+ server_name #{path};
136
+ location / {
137
+ proxy_set_header Host #{vhost[:host]};
138
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
139
+ proxy_set_header X-Forwarded-Host $host;
140
+ proxy_set_header X-Forwarded-Port $server_port;
141
+ proxy_set_header X-Real-IP $remote_addr;
142
+ proxy_set_header X-Forwarded-Proto $scheme;
143
+ proxy_pass http://#{ip}#{port_suffix}/;
144
+ }
145
+ }
146
+ EOF
147
+ end.join("\n")
148
+ end
149
+
104
150
  def start_marker(m)
105
151
  "# BEGIN #{m.id} #"
106
152
  end
@@ -2,13 +2,14 @@ module VagrantPlugins
2
2
  module ReverseProxy
3
3
  class Plugin
4
4
  class Config < Vagrant.plugin(2, :config)
5
- attr_accessor :enabled, :vhosts, :nginx_config_file, :nginx_reload_command
5
+ attr_accessor :enabled, :vhosts, :nginx_locations_config_file, :nginx_servers_config_file, :nginx_reload_command
6
6
  alias_method :enabled?, :enabled
7
7
 
8
8
  def initialize
9
9
  @enabled = UNSET_VALUE
10
10
  @vhosts = UNSET_VALUE
11
- @nginx_config_file = UNSET_VALUE
11
+ @nginx_locations_config_file = UNSET_VALUE
12
+ @nginx_servers_config_file = UNSET_VALUE
12
13
  @nginx_reload_command = UNSET_VALUE
13
14
  end
14
15
 
@@ -31,8 +32,11 @@ module VagrantPlugins
31
32
  end
32
33
  end
33
34
  end
34
- if @nginx_config_file == UNSET_VALUE
35
- @nginx_config_file = nil
35
+ if @nginx_locations_config_file == UNSET_VALUE
36
+ @nginx_locations_config_file = '/etc/nginx/vagrant-proxy-config-locations'
37
+ end
38
+ if @nginx_servers_config_file == UNSET_VALUE
39
+ @nginx_servers_config_file = '/etc/nginx/vagrant-proxy-config-servers'
36
40
  end
37
41
  if @nginx_reload_command == UNSET_VALUE
38
42
  @nginx_reload_command = nil
@@ -69,8 +73,16 @@ module VagrantPlugins
69
73
  errors << 'vhosts must be an array of hostnames, a string=>string hash or nil'
70
74
  end
71
75
 
72
- unless @nginx_config_file.instance_of?(String) || @nginx_config_file == nil || @nginx_config_file == UNSET_VALUE
73
- errors << 'nginx_config_file must be a string'
76
+ unless @nginx_locations_config_file.instance_of?(String) || @nginx_locations_config_file == nil || @nginx_locations_config_file == UNSET_VALUE
77
+ errors << 'nginx_locations_config_file must be a string'
78
+ end
79
+
80
+ unless @nginx_servers_config_file.instance_of?(String) || @nginx_servers_config_file == nil || @nginx_servers_config_file == UNSET_VALUE
81
+ errors << 'nginx_servers_config_file must be a string'
82
+ end
83
+
84
+ if @nginx_locations_config_file == nil && @nginx_servers_config_file == nil
85
+ errors << 'only one of nginx_locations_config_file and nginx_servers_config_file can be nil'
74
86
  end
75
87
 
76
88
  unless @nginx_reload_command.instance_of?(String) || @nginx_reload_command == nil || @nginx_reload_command == UNSET_VALUE
@@ -32,6 +32,10 @@ module VagrantPlugins
32
32
  action_hook(:reverse_proxy, :machine_action_halt) do |hook|
33
33
  hook.append(Action.remove_machine)
34
34
  end
35
+
36
+ action_hook(:reverse_proxy, :machine_action_reload) do |hook|
37
+ hook.append(Action.add_machine)
38
+ end
35
39
  end
36
40
  end
37
41
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module ReverseProxy
3
- VERSION = "0.3.1"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-reverse-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bex
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-22 00:00:00.000000000 Z
11
+ date: 2016-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler