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

RC12: vite.server.proxy ignored in nuxt.config.ts #15195

Closed
bcspragu opened this issue Oct 19, 2022 · 12 comments
Closed

RC12: vite.server.proxy ignored in nuxt.config.ts #15195

bcspragu opened this issue Oct 19, 2022 · 12 comments

Comments

@bcspragu
Copy link

bcspragu commented Oct 19, 2022

Environment

  • Operating System: Linux
  • Node Version: v16.13.1
  • Nuxt Version: 3.0.0-rc.11 (note: I downgraded after rc12 broke things)
  • Nitro Version: 0.5.4
  • Package Manager: npm@8.1.2
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

I can try to make a fresh reproduction if needed, but the issue is pretty straightforward: updating to RC12 makes Vite dev server proxying stop working. For example, we have a config like the following:

  vite: {
    server: {
      proxy: {
        '/api': {
          target: 'http://localhost:8080',
        },
      },
    },
  },

We route any traffic at http://localhost:3000/api/... to our backend, running on 8080. This has worked well in all Nuxt 3 versions up til now, and as far as I can tell, is still a valid and supported Vite option.

Describe the bug

As of RC12, our proxy config is no longer respected. Building on the example above, a request to http://localhost:3000/api/graphql used to return a response from our backend server on http://localhost:8080, but now hits our Nuxt catchall route and gives us a 404 page.

SSR requests succeed as usual (because they don't use the proxy for backend requests), but any subsequent requests from the clients fail, because they're getting an HTML response when they were expecting JSON.

Additional context

No response

Logs

No response

@KurumiRin
Copy link

I also had this problem, because I configured server: false in useFetch and used vite proxy, so all my requests returned a 404 page.

@slarrasoain
Copy link

I also had this problem. Just try with fresh install and the issue is still here.

@danielroe
Copy link
Member

danielroe commented Oct 19, 2022

Please do not use vite proxy. The vite dev server is not the same thing as the nitro dev server and they are not compatible. See #14917.

edit: the change in behaviour is likely a result of new h3 format. (However, I think it's much more likely we want to make sure we support proxy natively in nitro than support vite proxy.)

cc: @pi0

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale Oct 19, 2022
@danielroe danielroe reopened this Oct 19, 2022
@P4sca1
Copy link
Contributor

P4sca1 commented Oct 19, 2022

We are using a proxy configured with nginx in production and I used the vite dev server proxy in development to get the same result. So there are legitimate reasons to use the vite proxy.

@bcspragu
Copy link
Author

Similar deal on our end: In production we use rewrites in Firebase Hosting to send traffic to the backend, so the proxying is only needed locally. We could use something like NGINX to do the proxying, but that's another service to run when developing.

@ironytr

This comment was marked as duplicate.

@silencerspirit
Copy link
Contributor

@danielroe Can you advise some additional solution of proxying locally?

@bcspragu
Copy link
Author

bcspragu commented Oct 27, 2022

@danielroe Can you advise some additional solution of proxying locally?

Not danielroe, but it turns out that our deployed app was broken (broken SCSS styling, didn't root cause it) on RC11, so I bit the bullet and updated to RC12 (which fixed the issue).

My solution was an NGINX proxy in a Docker container, that I start up alongside my backend service (though it could be done independently or with the frontend).

Here's how I did it, using the same config from above as an example:

  vite: {
    server: {
      proxy: {
        '/api': {
          target: 'http://localhost:8080',
        },
      },
    },
  },

The NGINX config looks like:

# nginx.conf, mostly copied from the nginx:1.23 container image's default nginx.conf file, see https://hub.docker.com/_/nginx/
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    server {
        listen       3000 default_server;
        listen       [::]:3000 default_server;

        location / {
            proxy_pass http://127.0.0.1:3002;
        }

        location /api/ {
            proxy_pass http://127.0.0.1:8080;
        }
    }
}

This config says "Run NGINX on port 3000, route traffic to anything at /api/... to port 8080 and everything else to port 3002". We use 3002 instead of 3001 because we've already co-opted 3001 for some WebSocket shenanigans.

And then we run NGINX in Docker with:

docker run \
  --net=host \
  --volume path/to/nginx.conf:/etc/nginx/nginx.conf:ro \
  nginx:1.23

The --net=host is important if you want it to proxy things that aren't also in Docker containers on the same Docker network. You might also want the usual Docker flags (--rm to delete after use, --name to reference it easily, --detach to run in the background, etc).

We then run (using "scripts" in package.json) our Nuxt dev server with:

HOST=0.0.0.0 PORT=3002 nuxi dev

and run it in preview mode with:

nuxi build
HOST=0.0.0.0 PORT=3002 nuxi preview

I also had to turn off writeEarlyHints, introduced in RC12, because NGINX was deeply unhappy with it, regardless of proxy buffering/not buffering/size/etc:

// in nuxt.config.ts
  experimental: {
    // This was breaking our local preview mode (e.g. `npm run preview`), see https://github.com/nuxt/framework/issues/8450
    writeEarlyHints: false,
  },

As far as I can tell, this works flawlessly, including HMR in dev mode.

@silencerspirit
Copy link
Contributor

@bcspragu thx so much for detailed answer.

@Binaryify
Copy link

same

@manniL
Copy link
Member

manniL commented Nov 8, 2022

With RC13, you can use Nitro's devProxy.

@danielroe
Copy link
Member

Closing as we now have devProxy support, which is what you should use rather than the vite.server.proxy option. If you have any issues with it, please feel free to open a new issue with a reproduction and we can resolve there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants