Fix Host header forwarding to support reverse proxy usage#11
Merged
Conversation
Remove the Host header from forwarded requests so that aiohttp automatically sets the correct Host header based on the target URL. Previously, when corsproxy was used behind a reverse proxy (like Traefik), it would forward the client's Host header (e.g., proxy.example.com) to the target server. Many target servers using virtual host configurations would respond with 404 errors because they didn't recognize the proxy's hostname. Now, by filtering out the Host header before forwarding, aiohttp sets the correct Host header based on the target URL (e.g., www.researchcatalogue.net), ensuring the target server receives the expected hostname. This change maintains backward compatibility while fixing the issue for users behind reverse proxies without requiring manual Host header configuration in the reverse proxy. Fixes #9 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adjusts request header forwarding in corsproxy so that when deployed behind a reverse proxy, the inbound Host header is not forwarded to the target server—allowing the HTTP client to set Host based on the target URL and avoiding vhost-based 404s.
Changes:
- Filters out the
Hostheader (case-insensitively) from forwarded request headers inget(). - Adds inline comments explaining why
Hostshould not be forwarded in this setup.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Create mutable copy of headers and remove Host header so aiohttp | ||
| # sets it correctly based on the target URL instead of forwarding | ||
| # the client's Host header | ||
| hdr = {k: v for k, v in request.headers.items() if k.lower() != 'host'} |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When corsproxy is deployed behind a reverse proxy (like Traefik), the incoming request's Host header points to the proxy's hostname rather than the target server's hostname. Previously, corsproxy would forward this Host header unchanged, causing many target servers to respond with 404 errors because they didn't recognize the proxy's hostname in their virtual host configuration.
This PR modifies the request forwarding logic to filter out the Host header before sending requests to the target server. This allows aiohttp to automatically set the correct Host header based on the target URL, ensuring that target servers receive the hostname they expect.
Changes
get()function incorsproxyto filter out the Host header (case-insensitively) from forwarded requestsBenefits
Fixes: #9