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

Adding support forward all headrs from downstream to upstream #6151

Closed
alzaicko opened this issue Aug 7, 2024 · 2 comments
Closed

Adding support forward all headrs from downstream to upstream #6151

alzaicko opened this issue Aug 7, 2024 · 2 comments

Comments

@alzaicko
Copy link

alzaicko commented Aug 7, 2024

Is your feature request related to a problem? Please describe.
I need to pass custom values ​​in the Host header to upstream, at the moment I can’t override the Host header in any way.

Describe the solution you'd like
Make settings in the configuration so that for the Host header, which will be sent to upstream, the value will be taken from the transmitted X-Forwarded-Host header

Describe alternatives you've considered
For example, how it is implemented here:

use-forwarded-headers - https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#use-forwarded-headers
If true, NGINX passes the incoming X-Forwarded-* headers to upstreams. Use this option when NGINX is behind another L7 proxy / load balancer that is setting these headers.

If false, NGINX ignores incoming X-Forwarded-* headers, filling them with the request information it sees. Use this option if NGINX is exposed directly to the internet, or it's behind a L3/packet-based load balancer that doesn't alter the source IP in the packets.

@alzaicko alzaicko added the proposal An issue that proposes a feature request label Aug 7, 2024
Copy link

github-actions bot commented Aug 7, 2024

Hi @alzaicko thanks for reporting!

Be sure to check out the docs and the Contributing Guidelines while you wait for a human to take a look at this 🙂

Cheers!

@j1m-ryan
Copy link
Member

j1m-ryan commented Aug 8, 2024

Hi @alzaicko. Yes this is possible.
This is easiest using the VirtualServer resource.

I deployed a simple VirtualServer, similar to the example in here.

The main difference is using action.proxy instead of action.pass. This allows defining a list of headers to set in the requestHeaders.set. Below I set X-Forwarded-Host to example2.com, with the VirtualServer itself listening on cafe.example.com.

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: cafe
spec:
  host: cafe.example.com
  tls:
    secret: cafe-secret
  upstreams:
  - name: coffee
    service: coffee-svc
    port: 80
  routes:
  - path: /coffee
    action:
      proxy:
        upstream: coffee
        requestHeaders:
          set:
            - name: X-Forwarded-Host
              value: example2.com

In the deployment that the coffee-svc references, I deployed the image mendhak/http-https-echo which returns json of all the headers sent to it.

When I curl cafe.example.com/coffee I get this response.

{
  "path": "/coffee",
  "headers": {
    "connection": "close",
    "x-real-ip": "10.244.0.1",
    "x-forwarded-for": "10.244.0.1",
    "x-forwarded-port": "80",
    "x-forwarded-proto": "http",
    "x-forwarded-host": "example2.com",
    "host": "cafe.example.com",
    "user-agent": "curl/8.6.0",
    "accept": "*/*"
  },
  "method": "GET",
  "body": "",
  "fresh": false,
  "hostname": "example2.com",
  "ip": "10.244.0.1",
  "ips": [
    "10.244.0.1"
  ],
  "protocol": "http",
  "query": {},
  "subdomains": [],
  "xhr": false,
  "os": {
    "hostname": "coffee-c96565fcd-rr8vp"
  },
  "connection": {}
}

Here you can see "x-forwarded-host": "example2.com",

Edit:
Just to add also, you see the docs on this here

@j1m-ryan j1m-ryan added waiting for response Waiting for author's response and removed proposal An issue that proposes a feature request labels Aug 8, 2024
@vepatel vepatel closed this as completed Oct 7, 2024
@github-project-automation github-project-automation bot moved this from Todo ☑ to Done 🚀 in NGINX Ingress Controller Oct 7, 2024
@vepatel vepatel removed the waiting for response Waiting for author's response label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

3 participants