<commit_before>module VagrantPlugins
  module ReverseProxy
    module Action
      class WriteNginxConfig
        def initialize(app, env)
          @app = app
          @global_env = env[:machine].env
          @provider = env[:machine].provider_name
          @config = @global_env.vagrantfile.config
        end

        def call(env)
          @app.call(env)

          return unless @config.reverse_proxy.enabled?

          # Determine temp file and target file
          tmp_file = @global_env.tmp_path.join('nginx.vagrant-proxies')
          nginx_site = '/etc/nginx/vagrant-proxy-config'

          env[:ui].info('Updating nginx configuration. Administrator privileges will be required...')

          File.open(tmp_file, 'w') do |f|
            get_machines().each do |m|
              f.write(server_block(m))
            end
          end

          Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_site)
          Kernel.system('sudo', 'systemctl', 'reload', 'nginx')
        end

        def server_block(machine)
          if @config.reverse_proxy.vhosts
            vhosts = @config.reverse_proxy.vhosts
          else
            vhosts = [machine.config.vm.hostname || machine.name]
          end
          ip = get_ip_address(machine)
          vhosts.collect do |vhost| <<EOF
location /#{vhost}/ {
    proxy_set_header Host #{vhost};
    proxy_pass http://#{ip}/;
    proxy_redirect http://#{vhost}/ /#{vhost}/;
}
EOF
          end.join("\n")
        end

        # Machine-finding code stolen from vagrant-hostmanager :)
        def get_machines()
          # Collect only machines that exist for the current provider
          @global_env.active_machines.collect do |name, provider|
            if provider == @provider
              begin
                m = @global_env.machine(name, @provider)<commit_msg>Add some more headers to help proxied applications.<commit_after>module VagrantPlugins
  module ReverseProxy
    module Action
      class WriteNginxConfig
        def initialize(app, env)
          @app = app
          @global_env = env[:machine].env
          @provider = env[:machine].provider_name
          @config = @global_env.vagrantfile.config
        end

        def call(env)
          @app.call(env)

          return unless @config.reverse_proxy.enabled?

          # Determine temp file and target file
          tmp_file = @global_env.tmp_path.join('nginx.vagrant-proxies')
          nginx_site = '/etc/nginx/vagrant-proxy-config'

          env[:ui].info('Updating nginx configuration. Administrator privileges will be required...')

          File.open(tmp_file, 'w') do |f|
            get_machines().each do |m|
              f.write(server_block(m))
            end
          end

          Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_site)
          Kernel.system('sudo', 'systemctl', 'reload', 'nginx')
        end

        def server_block(machine)
          if @config.reverse_proxy.vhosts
            vhosts = @config.reverse_proxy.vhosts
          else
            vhosts = [machine.config.vm.hostname || machine.name]
          end
          ip = get_ip_address(machine)
          vhosts.collect do |vhost| <<EOF
location /#{vhost}/ {
    proxy_set_header Host #{vhost};
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Base-Url http://$host:$server_port/nl.dr.dev/;

    proxy_pass http://#{ip}/;
    proxy_redirect http://#{vhost}/ /#{vhost}/;
}
EOF
          end.join("\n")
        end

        # Machine-finding code stolen from vagrant-hostmanager :)
        def get_machines()
          # Collect only machines that exist for the current provider
          @global_env.active_machines.collect do |name, provider|
            if provider == @provider
              begin
                m = @global_env.machine(name, @provider)
{'fraction_non_alphanumeric': 0.10722491702833802, 'fraction_numerical': 0.0, 'mean_word_length': 2.080188679245283, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4819425', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1129, 'n_words': 258}