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

Host header is ignored when using proxyRequest() #825

Open
afonsosantos opened this issue Jul 12, 2024 · 1 comment
Open

Host header is ignored when using proxyRequest() #825

afonsosantos opened this issue Jul 12, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@afonsosantos
Copy link

afonsosantos commented Jul 12, 2024

Environment

  • Node version: v20.15.1
  • H3: v1.12.0

Reproduction

  • Initialize a new Nuxt 3 app
  • Add the following to nuxt.config.ts:
runtimeConfig: {
    public: {
      backend: {
        url: "http://localhost:8000",
        host_header: "dev.localhost:8000",
      },
    },
  },
  • Create an API route: server/api/[...].ts
  • Use the following snippet:
import { joinURL } from "ufo";

export default defineEventHandler(async (event) => {
  const {
    public: { backend },
  } = useRuntimeConfig(event);

  const path: string = event.path.replace(/^\/api\//, "");
  const target: string = joinURL(backend.url, path);

  console.info(
    `Proxying request: (frontend) ${event.method} ${event.path} -> (backend) ${target}`,
  );

  return await proxyRequest(event, target, {
    headers: { "Host": backend.host_header },
  });
});
  • Use the newly created API route, like GET localhost:3000/api/users/, and observe that in the receiving end (backend) the Host header is not replaced by the correct value.

Describe the bug

When using the proxyRequest function in the latest Nuxt JS version, the Host header is ignored. From looking into the Github issues, a similar problem was found, as seen here: #709 (in this case, the Accept header is ignored), introduced by this PR: #646.

In the case of my project, we use the Host header to change the tenant domain on the backend side (one subdomain per tenant).

A culript piece of code was found here:

h3/src/utils/proxy.ts

Lines 25 to 33 in 17fc905

const ignoredHeaders = new Set([
"transfer-encoding",
"connection",
"keep-alive",
"upgrade",
"expect",
"host",
"accept",
]);

where the Host header and others are explicitly being ignored.

If a fix is necessary, I can provide a PR with the code fix.

Additional context

No response

Logs

No response

@afonsosantos afonsosantos added the bug Something isn't working label Jul 12, 2024
@hecktarzuli
Copy link

hecktarzuli commented Oct 29, 2024

Same here. We run 1 nuxt instance serving 4 sites, each running their own APIs. Our problem is even using useRequestFetch the host isn't set because of this same line of code.

Being able to unIgnore headers would be great (and being able to pass that list to useRequestFetch).
Currently our APIS are https://[domain]/api/[domain]/foo/ which is pretty gross.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants