-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat: Update reverse proxy example to support both http and https #2696
Open
afifurrohman-id
wants to merge
4
commits into
tokio-rs:main
Choose a base branch
from
afifurrohman-id:reverse-proxy-example-patch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d29cfd7
feat: Update reverse proxy example to support both http and https
afifurrohman-id 37900c6
fix(ci): Format `Cargo.toml` and perform cargo fmt
afifurrohman-id e01086b
Merge branch 'tokio-rs:main' into reverse-proxy-example-patch
afifurrohman-id 40971a4
refactor: add some informative header (`X-Forwarded-For`, `X-Forwarde…
afifurrohman-id File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the question mark in front here? Personally I don't know if this is the right behavior here, @yanns can you comment on it since you approved (in addition to @afifurrohman-id)?
Even if we keep it I don't think there's any reason to
.unwrap()
there though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When proxing a HTTP request, the initial HTTP request contains as
HOST
the address of the proxy (locahost:4000
). If we use that value against another server, that server will refuse the HTTP request as it expectslocalhost:3000
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this was previously broken? Also, should the header value be added as some (de-facto) standard proxied-host header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess yes. I have not checked. I've built several proxy and this is one of the usual issues.
If we want to go in the direction of "good behaving" proxy, yes they are several headers that should be set:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried the original example and it works.
The
host
header would be an issue if the two servers were running externally visible on the same port as virtual servers with nginx or something else in front of them that would forward the requests based on thehost
header.axum
,hyper
, or anything else in the stack does not check the header, because it can't even know what to check it for. The request just arrives either onlocalhost:4000
orlocalhost:3000
and then it is processed.Personally, I think that handling of this and the addition of an external dependency on
example.com
is a bit sad and it would be better to just add an axum tls server (based on any of the other examples) and call that instead.But even with
example.com
, the header change is not really needed ascurl -v https://example.com -H 'host: localhost:3000'
returns a 404 -- it's not the same page you get if you let curl use the correcthost
(and it might be returned by a reverse proxy and not the actual server), but it is a response to anhttps
request so it does show that this works.Generally, I think the examples should be as simple as possible to showcase the thing they mean to (here that's relaying a request, potentially with a change to the protocol), but details such as extension headers can and should be omitted for brevity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just for clarify why we remove
HOST
header, as @yanns say.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think like so, we need add it as well, like some other reverse proxy, it will also make more great example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, first because it think to simple as possible use an
example.com
can add more precisely how we can interact with external service, but will add some external network request as well.However, using other examples like
tls-rustls
add another complexity because will required to handle self-signed certificate