drewzboto / grunt-connect-proxy

Grunt Connect support for proxying API calls during development
MIT License
423 stars 122 forks source link

Host header not properly set in proxied request #69

Closed jeffsawatzky closed 6 years ago

jeffsawatzky commented 10 years ago

here is my config:

connect: {
      server: {
        options: {
          port: 8000,
          base: 'src',
          keepalive: true,
          middleware: function (connect, options) {
            var proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
            return [
              proxy,
              connect.static(options.base[0]),
              connect.directory(options.base[0])
              ];
          }
        },
        proxies: [{
          context: '/otherapi',
          host: 'www.other.com',
          port: 80,
          https: false,
          headers: {
            'host': 'www.other.com'
          },
          rewrite: {
            '^/otherapi': '/api'
          }
        }]
      }
    }
  });

Which works, and successfully proxies all XHR requests to http://localhost:8000/otherapi to http://www.other.com/api. However, without the headers option, the proxy doesn't work. The request is sent to the ip for www.other.com but the host header is still localhost:8000 and the api request fails. I had to add the extra host header to get it to work, which I don't think should be required.

jeffsawatzky commented 10 years ago

Apparently setting:

changeOrigin: true

fixes the issue without having to specify the headers. Still, though, why isn't this the default behaviour?

jeffsawatzky commented 10 years ago

in version 0.1.11, changeOrigin seems to have broken...

omanizer commented 10 years ago

Having exact same issue, tried on 0.1.10 as well and still need to add headers.host to make it work.

outaTiME commented 9 years ago

Same issue here =(

k7n4n5t3w4rt commented 9 years ago

Ditto for 0.2.0. Maybe it's the new way. Adding this here in case it's driving someone crazy like it did me.

I had to include "headers":

  proxies: [{
    context: '/api', // the context of the data service
    host: 'tidespy.com', // wherever the data service is running
    port: 80, // the port that the data service is running on
    headers: {
        'host': 'tidespy.com'
      }
  }],
m11m commented 9 years ago

I don't even get a request sent unless I add the host header...

Packet capture with wireshark verifies, without host header, it opens connections but never sends the HTTP request...