Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy does not work, both requests to backend and http://localhost:3000/ #3475

Closed
doupongzeng opened this issue May 20, 2021 · 11 comments
Closed
Labels
cannot reproduce The bug cannot be reproduced pending triage

Comments

@doupongzeng
Copy link

Describe the bug

#1014 (comment)
Same like this issue.
my vite.config.ts

import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [reactRefresh()],
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:8000',
        changeOrigin: true
      }
    }
  }
})

and the React code

import React, { useState } from 'react'
import logo from './logo.svg'
import './App.css'

function App() {
  const [count, setCount] = useState(0)

  function request(params:any) {
    fetch('/api').then(response => response.json());
  }

  return (
    <div className="App">
      <header className="App-header">
        <button onClick={request}>
          Request
        </button>
      </header>
    </div>
  )
}

export default App

Reproduction

Create a blank project with vite, yarn create @vitejs/app
Add vite.config.ts with proxies from the doc
Issue request to/apior anything using fetch
Observe that requests are still sent to the dev server http://localhost:8000/ and http://localhost:3000/ in my case.

System Info

vite: ^2.3.0 => 2.3.2
OS: Linux 4.19 Ubuntu 20.04.2 LTS (Focal Fossa)
node: v16.0.0
yarn -v: 1.22.10

Logs

  vite v2.3.3 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 223ms.

  vite:time 0ms   / +0ms
  vite:spa-fallback Rewriting GET / to /index.html +0ms
  vite:time 20ms  /index.html +38ms
  vite:resolve 1ms   /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/client -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/client.js +0ms
  vite:resolve 3ms   /@vite/client -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/client.js +1ms
  vite:resolve 1ms   /src/main.tsx -> /home/hz-wsl2/vite-pp/src/main.tsx +3ms
  vite:load 4ms   [fs] /@vite/client +0ms
  vite:resolve 1ms   ./env -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/env.js +17ms
  vite:resolve 0ms   /node_modules/vite/dist/client/env.js -> /home/hz-wsl2/vite-pp/node_modules/vite/dist/client/env.js +2ms
  vite:transform 16ms  /@vite/client +0ms
  vite:time 31ms  /@vite/client +70ms
  vite:load 26ms  [fs] /src/main.tsx +25ms
  vite:load 0ms   [plugin] /@react-refresh +229ms
  vite:rewrite 1ms   [no imports] /@react-refresh +0ms
  vite:transform 1ms   /@react-refresh +236ms
  vite:time 3ms   /@react-refresh +235ms
  vite:resolve 0ms   react -> /home/hz-wsl2/vite-pp/node_modules/.vite/react.js?v=18fa1454&es-interop +244ms
  vite:resolve 1ms   /node_modules/.vite/react.js?v=18fa1454&es-interop -> /home/hz-wsl2/vite-pp/node_modules/.vite/react.js?v=18fa1454&es-interop +2ms
  vite:resolve 0ms   react-dom -> /home/hz-wsl2/vite-pp/node_modules/.vite/react-dom.js?v=18fa1454&es-interop +5ms
  vite:resolve 0ms   /node_modules/.vite/react-dom.js?v=18fa1454&es-interop -> /home/hz-wsl2/vite-pp/node_modules/.vite/react-dom.js?v=18fa1454&es-interop +1ms
  vite:resolve 0ms   ./index.css -> /home/hz-wsl2/vite-pp/src/index.css +1ms
  vite:resolve 0ms   /src/index.css -> /home/hz-wsl2/vite-pp/src/index.css +1ms
  vite:resolve 1ms   ./App -> /home/hz-wsl2/vite-pp/src/App.tsx +2ms
  vite:resolve 0ms   /src/App.tsx -> /home/hz-wsl2/vite-pp/src/App.tsx +1ms
  vite:resolve 0ms   /node_modules/.vite/react.js?v=18fa1454 -> /home/hz-wsl2/vite-pp/node_modules/.vite/react.js?v=18fa1454 +1ms
  vite:resolve 0ms   /node_modules/.vite/react-dom.js?v=18fa1454 -> /home/hz-wsl2/vite-pp/node_modules/.vite/react-dom.js?v=18fa1454 +1ms
  vite:transform 243ms /src/main.tsx +21ms
  vite:time 282ms /src/main.tsx +22ms
  vite:load 3ms   [fs] /node_modules/vite/dist/client/env.js +33ms
  vite:rewrite 0ms   [no imports] node_modules/vite/dist/client/env.js +34ms
  vite:transform 2ms   /node_modules/vite/dist/client/env.js +14ms
  vite:time 10ms  /node_modules/vite/dist/client/env.js +13ms
  vite:load 3ms   [fs] /node_modules/.vite/react.js?v=18fa1454 +13ms
  vite:resolve 2ms   ./chunk-JF75KDUF.js -> /home/hz-wsl2/vite-pp/node_modules/.vite/chunk-JF75KDUF.js +28ms
  vite:resolve 0ms   /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 -> /home/hz-wsl2/vite-pp/node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +2ms
  vite:transform 7ms   /node_modules/.vite/react.js?v=18fa1454 +17ms
  vite:time 15ms  /node_modules/.vite/react.js?v=18fa1454 +17ms
  vite:load 18ms  [fs] /node_modules/.vite/react-dom.js?v=18fa1454 +17ms
  vite:transform 31ms  /node_modules/.vite/react-dom.js?v=18fa1454 +83ms
  vite:time 113ms /node_modules/.vite/react-dom.js?v=18fa1454 +102ms
  vite:load 4ms   [fs] /src/index.css +120ms
  vite:load 14ms  [fs] /src/App.tsx +13ms
  vite:resolve 0ms   ./App.css -> /home/hz-wsl2/vite-pp/src/App.css +204ms
  vite:resolve 0ms   /src/App.css -> /home/hz-wsl2/vite-pp/src/App.css +1ms
  vite:hmr [self-accepts] src/App.tsx +0ms
  vite:transform 67ms  /src/App.tsx +127ms
  vite:load 12ms  [fs] /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +70ms
  vite:rewrite 1ms   [no imports] node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +233ms
  vite:transform 2ms   /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +6ms
  vite:time 18ms  /node_modules/.vite/chunk-JF75KDUF.js?v=18fa1454 +115ms
  vite:time 94ms  /src/App.tsx +3ms
  vite:hmr [self-accepts] src/index.css +21ms
  vite:transform 91ms  /src/index.css +10ms
  vite:time 106ms /src/index.css +6ms
  vite:load 0ms   [fs] /src/App.css +124ms
  vite:hmr [self-accepts] src/App.css +112ms
  vite:transform 1ms   /src/App.css +112ms
  vite:time 3ms   /src/App.css +111ms
  vite:time 4ms   /src/favicon.svg +298ms
  vite:proxy /api -> http://localhost:8000 +0ms
  vite:time 17ms  /api +10s
@12dollar
Copy link

12dollar commented Jun 11, 2021

I'm facing the same issue. Too bad there hasn't been an appropriate answer to solve posted yet. In my case, I'm facing this issue on Windows and Linux (Ubuntu).

@ygj6
Copy link
Member

ygj6 commented Jun 21, 2021

Maybe there was a misunderstanding, vite's proxy means it will route requests from a browser to the target server.
The request process is as follows:

Browser ->        Vite dev server    -> Your backend server
Browser -> http://localhost:3000 -> http://localhost:8000

@12dollar
Copy link

The request process is as follows:

Browser ->        Vite dev server    -> Your backend server
Browser -> http://localhost:3000 -> http://localhost:8000

And my requests are never proxied from http://localhost:3000 to http://localhost:8000 as per this example.

@Codermar
Copy link

Codermar commented Aug 11, 2021

I am facing a similar issue [vite] http proxy error: Error: self signed certificate in certificate chain
when trying to use a proxy configuration (basically like in the docs) I know my cert is legit. It works fine in my vue-cli project. In fact I can run the UI and the backend ajax call in the browser in separate tabs but not in the single tab using the proxy. The ajax request just and stays "pending". (Note the error says "http proxy error" also, and I this is an https config)

System Info
vite: ^2.4.4 (also tried 2.5.0-beta.2)
OS: MacOS 11.5.1
node: v16.6.0
yarn -v: 1.22.10

This is actually blocking my whole migration :-(

@mseele
Copy link

mseele commented Aug 27, 2021

I am facing a similar issue [vite] http proxy error: Error: self signed certificate in certificate chain
when trying to use a proxy configuration (basically like in the docs) I know my cert is legit.

@Codermar You can specifiy secure: false to avoid certificate checks

@jannhama
Copy link

jannhama commented Feb 11, 2022

Half a year later and the still same issue occurs. Any idea when this will be fixed?

@Shinigami92
Copy link
Member

Half a year later and the still same issue occurs. Any idea when this will be fixed?

Contributions welcome

@ironytr
Copy link

ironytr commented Mar 11, 2022

vite: { server: { proxy: { "/api": { target: "http://localhost:9001", changeOrigin: true, secure: false, rewrite: (path) => path.replace(/^\/api/, ""), }, }, }, },
hello, proxy is working in dev mode, but in production doesn't work. there is no error or log output. (im newbie)

@sapphi-red sapphi-red added the cannot reproduce The bug cannot be reproduced label May 13, 2022
@SaazNeupane
Copy link

SaazNeupane commented May 13, 2022

server: {
    proxy: {
      "/api": {
        target: "http://127.0.0.1:4000",
        changeOrigin: true,
        secure: false,
        ws: true,
      },
    },
  },

This worked for me.

@orhan-swe
Copy link

server: {
    proxy: {     
        '/api/mobile/v1': {
          target: 'http://localhost:8083'
} } },

This is working for me, it will proxy:
localhost:3000/api/mobile/v1 to localhost:8083/api/mobile/v1
Vite version 2.9.9

@sapphi-red
Copy link
Member

It's difficult to solve without reproducing it and there was many updates so it might be already solved.
I'm closing this issue for now but please create a new discussion if it is still happening.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cannot reproduce The bug cannot be reproduced pending triage
Projects
None yet
Development

No branches or pull requests