Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions bifrost-fastify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { FastifyRequest, RequestGenericInterface } from "fastify/types/request";
import proxy, { type FastifyHttpProxyOptions } from "@fastify/http-proxy";
import accepts from "@fastify/accepts";
import forwarded from "@fastify/forwarded";
import type { GetLayout, WrappedServerOnly } from "@alignable/bifrost/config";
import { renderPage } from "vike/server";
import { PageContextServer } from "vike/types";
Expand Down Expand Up @@ -192,12 +191,18 @@ export const viteProxyPlugin: FastifyPluginAsync<
},
replyOptions: {
rewriteRequestHeaders(request, headers) {
if (!(request.raw instanceof Http2ServerRequest)) {
const fwd = forwarded(request.raw).reverse();
headers["X-Forwarded-For"] = fwd.join(", ");
headers["X-Forwarded-Host"] = host.host;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting x-forwarded-host/protocol is required for rails to work properly

headers["X-Forwarded-Proto"] = host.protocol;
// Build X-Forwarded-For from existing header + socket address
const existingFor = request.headers["x-forwarded-for"];
const clientIp = request.raw.socket?.remoteAddress;
if (existingFor && clientIp) {
headers["X-Forwarded-For"] = `${clientIp}, ${existingFor}`;
} else if (existingFor) {
headers["X-Forwarded-For"] = existingFor;
} else if (clientIp) {
headers["X-Forwarded-For"] = clientIp;
}
headers["X-Forwarded-Host"] = host.host;
headers["X-Forwarded-Proto"] = host.protocol.replace(":", "");

if ((request.raw as RawRequestExtendedWithProxy)._bfproxy) {
// Proxying and wrapping
Expand Down
1 change: 0 additions & 1 deletion bifrost-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"types": "./dist/index.d.ts",
"dependencies": {
"@fastify/accepts": "^5.0.4",
"@fastify/forwarded": "^3.0.1",
"@fastify/http-proxy": "^11.4.1",
"@fastify/reply-from": "^12.5.0",
"fast-content-type-parse": "^3.0.0",
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.