Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A way to modify headers like host as in nginx #29

Unanswered
ankit-sapkota asked this question in Q&A
Discussion options

Is there a way to modify headers like host?

Let's say I have a Web server running at 3000 and I want to use this reverse proxy to pass requests received at localhost:8005. But I need the host header to be localhost:3000 as it is required by the web server. How can I achieve this.

There does not seem to be a function to modify any thing. Am I missing something here? Or is it not yet supported ?

You must be logged in to vote

Replies: 1 comment 3 replies

Comment options

Hi @ankit-sapkota555 .

Is there a way to modify headers like host?

Have you read https://wsh032.github.io/fastapi-proxy-lib/Usage/Advanced/#modify-request ? Does it meet your needs?

newest link: https://www.python-httpx.org/advanced/authentication/#custom-authentication-schemes


I'm not sure if I understand your meaning correctly. Do you mean that there is a server running on port 8005, and you want to forward all requests received on port 8005 to port 3000?

And you want the requests received on port 3000 to have a request header with host: "localhost:3000" ?

Actually, this library will automatically modify the corresponding host for you, which follows common HTTP standards.

new_headers["host"] = target_url.netloc.decode("ascii")

Have you encountered any specific errors while using it? Let me know, and perhaps I can help resolve it.

You must be logged in to vote
3 replies
Comment options

Yes this follows common HTTP standards, but what I need is to forward all requests in port 8005 to 3000, but the web server expects the host header to be localhost:3000 (For my specific use case), So, to test that the proxy gateway will work as expected upon deployment, I need to modify the host header. Just curious if this is achievable through this package.

I will look into the docs refs and let you konw if they cover my use case. Thanks for the reply.

Comment options

Just curious if this is achievable through this package.

Of course. Refer to the following code:

import httpx
from fastapi_proxy_lib.fastapi.app import reverse_http_app
class MyCustomAuth(httpx.Auth):
 def auth_flow(self, request: httpx.Request):
 # whatever you want to do with the headers of the request
 request.headers["host"] = "localhost:3000"
 yield request
app = reverse_http_app(
 client=httpx.AsyncClient(auth=MyCustomAuth()),
 base_url="http://localhost:3000/",
)

Please let me know if it works for you.

Comment options

In fact, regardless of the request headers received on port 8005, the requests forwarded to port 3000 will always have "host": "localhost:3000". This functionality is available out of the box, so it's quite likely that you don't need to do anything.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /