webpack-dev-server icon indicating copy to clipboard operation
webpack-dev-server copied to clipboard

devServer proxy not working

Open gjunkie opened this issue 9 years ago • 49 comments

Do you want to request a feature or report a bug? Bug

What is the current behavior? devServer proxy doesn't proxy.

If the current behavior is a bug, please provide the steps to reproduce. Client server running at localhost:8080 and API server running at localhost:8000. I've tried several variations of what to proxy (api/*, /api/**, api/**, etc). Use the following proxy settings:

  devServer: {
    hot: true,
    contentBase: resolve(__dirname, 'dist'),
    publicPath: '/',
    proxy: {
      '/api/**': {
        target: 'http://localhost:8000',
        secure: false,
        changeOrigin: true,
      }
    },
  },

What is the expected behavior? Hitting /api/path should be proxied to http://localhost:8000/api/path but is instead hitting http://localhost:8080/api/path.

Please mention your webpack and Operating System version.

  • webpack @ 2.2.1
  • webpack-dev-server @ 2.3.0
  • macOS 10.12.3

gjunkie avatar Feb 17 '17 06:02 gjunkie

Have you tried '/api'?

steve-taylor avatar Feb 17 '17 06:02 steve-taylor

@steve-taylor I have indeed. /api, api, /api/path, etc. Every variation I could think of.

gjunkie avatar Feb 17 '17 06:02 gjunkie

I tried to reproduce this, but it does work for me. What's interesting though is that the error message I get in the browser is wrong. It says "Error occured while trying to proxy to: localhost:8080/api/users" even though the proxy is set to http://localhost:8000. When I start a server on port 8000 however, it does work correctly.

SpaceK33z avatar Feb 17 '17 09:02 SpaceK33z

I wonder if there's a way to plug in a reverse proxy that actually works, rather than being handcuffed to http-proxy-middleware.

steve-taylor avatar Feb 17 '17 18:02 steve-taylor

Yeah not sure.. Here's my whole config in case there's something else afoot. Maybe I've got something funky in the entry?

const { resolve } = require('path');
const webpack = require('webpack');

module.exports = {
  entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './app.js',
  ],

  output: {
    filename: 'bundle.js',
    path: resolve(__dirname, 'dist'),
    publicPath: '/',
  },

  context: resolve(__dirname, 'client'),

  devtool: 'inline-source-map',

  devServer: {
    hot: true,
    contentBase: resolve(__dirname, 'dist'),
    publicPath: '/',
    proxy: {
      '/api/**': {
        target: 'http://localhost:8000',
        secure: false,
        changeOrigin: true,
      }
    },
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          'babel-loader',
        ],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader?modules',
          'postcss-loader',
        ],
      },
    ],
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin()
  ],
};

gjunkie avatar Feb 17 '17 18:02 gjunkie

@steve-taylor what specific issues are you having? I would suggest creating github issues on http-proxy-middleware for those. The author of http-proxy-middleware is very responsive and helpful.

SpaceK33z avatar Feb 18 '17 11:02 SpaceK33z

@gjunkie can you make a repository with a minimal version of the backend you have on port 8000? That's the only way to be able to debug this further down.

SpaceK33z avatar Feb 18 '17 11:02 SpaceK33z

@SpaceK33z I decided to shift over to use Hapijs' cors feature for my needs, however this is the repo I was attempting to proxy in. Beyond removing the proxy option, there haven't been other changes, in case this helps.

https://github.com/gjunkie/hapi-react-kit

gjunkie avatar Feb 21 '17 18:02 gjunkie

I have the same problem as @gjunkie. Starting from v1.15.0 the http-proxy-middleware is installed as dependency and a ECONNRESET error is thrown each time I try to make a request to the backend.

Apparently, it is only happening in OS X environment because my colleagues at work are using Ubuntu and they can't reproduce this problem.

nancruz avatar Mar 31 '17 09:03 nancruz

I could reproduce this bug on OS X. In Windows, it is working just fine.

rpdmiranda avatar Apr 19 '17 14:04 rpdmiranda

I couldn't get devServer proxy to work either (OS X 10). I'm running with a server.js which reads webpack.config.js and my workaround was a reverse proxy using http-proxy in server.js:

var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();

// ... was here before
var app = express();
var compiler = webpack(config);

app.use(require('webpack-dev-middleware')(compiler, {
  publicPath: config.output.publicPath
}));

app.use(require('webpack-hot-middleware')(compiler));
// ... end of was here before

app.all("/api/*", function(req, res) {
    apiProxy.web(req, res, {target: 'http://localhost:3102'});
});

Reference: https://codeforgeek.com/2015/12/reverse-proxy-using-expressjs/

Cottin avatar Apr 21 '17 16:04 Cottin

How to set proxy buffer, i want to upload some files to backend

guoxiangwen avatar May 02 '17 02:05 guoxiangwen

Having the same issue here, also OSX

r01010010 avatar May 02 '17 12:05 r01010010

same issue on OSX

yuduxyz avatar May 22 '17 07:05 yuduxyz

Same issue on OSX as well.

maxsitu avatar Jun 01 '17 08:06 maxsitu

This configuration is working for me on OSX (I sweated blood to make it work). I have the GNU Coreutils installed, by the way (don't know if has something to do).

, proxy: [
          {
            path: /^\/styleguide(?!\/core-css\/build)/
          , target: 'http://localhost:8000'
          , bypass: function(req, res, _options) {
              return req.url.replace('styleguide', styleguideRoothPath + '/build');
            }
          }
        , {
            path: "**",
            target: "http://localhost:3000",
            secure: false,
            bypass: function(req, res, options) {
              if(req.url === '' || req.url === '/') {
                res.statusCode = 302;
                res.setHeader('Location', '/a/');
                return '/a/';
              }

              var frontend = new RegExp("^\/$^|\/a\/index\.html|^\/a\/|^\/a$|^\/styleguide");
              if (frontend.test(req.url)) return req.url;
            }
          }
        ]

r01010010 avatar Jun 01 '17 09:06 r01010010

Same issue on OSX.

ghost avatar Jul 19 '17 08:07 ghost

replace localhost with [::1]. like so: 'http://[::1]:3000'

this works for me:

devServer: {
  proxy: {
    "*": "http://[::1]:8081"
    // "secure": false,
    // "changeOrigin": true
  }
},

one wildcard, or double, with 'secure' or 'changeOrigin' - doesnt matter. [::1] is the game changer. Good luck!

jellyfish-tom avatar Jul 20 '17 09:07 jellyfish-tom

Meet the same issue on mac, any progress here?

sun-slaven avatar Aug 07 '17 10:08 sun-slaven

We've got a few workarounds posted for folks running into the issue. It seems that this might be slightly edge casey, however. If anyone would like to put together a Pull Request, we'd happily review it and revisit this issue.

shellscape avatar Aug 09 '17 16:08 shellscape