是否可以更改本地 nginx 转发中的 http 请求 header

is it possible to change the http request header in local nginx forward

我使用 yarn 启动应用程序,在我的本地机器上,配置一个监听端口 8083 的本地 nginx 服务,以及监听端口 3000 的 yarn 服务,当 url 路径以 [=14 开头时=],将http请求转发到远程kubernetes集群中的后端服务部署。这是我本地的 nginx 转发配置:

server {
        listen 8083;
        server_name admin.reddwarf.com;
        location / {
            proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
        }
        location ^~ /manage/ {
            proxy_pass  https://admin.example.top;
            proxy_redirect off;
            proxy_set_header Host https://admin.example.top;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

在kubernetes集群中,我使用traefik转发http请求,它通过httphostheader转发。这是 traefik 配置:

  routes:
    - kind: Rule
      match: Host(`admin.example.top`) && PathPrefix(`/manage`)
      priority: 2
      services:
        - name: dolphin-gateway
          port: 8081

现在 http 请求给出 404 未找到错误,我确信 api 路径存在,因为我可以在测试工具中调用 api。我认为从本地调试应用程序发送的请求是 localhost:8083,这使得 traefik 无法正确识别请求。我应该怎么做才能将本地机器 header 更改为 make traefik 可以识别的 admin.example.top?或者我的配置有误?我应该怎么做才能让它按预期工作?这是本地请求演示:

curl 'http://localhost:8083/manage/admin/user/login' \
  -H 'Connection: keep-alive' \
  -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'DNT: 1' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'Origin: http://localhost:8083' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Referer: http://localhost:8083/' \
  -H 'Accept-Language: en,zh-CN;q=0.9,zh;q=0.8,zh-TW;q=0.7,fr;q=0.6' \
  --data-raw '{"phone":"+8615623741658","password":"123"}' \
  --compressed

尝试添加到 curl 请求:-H 'HOST: admin.example.top'。然后更新你的 nginx 配置:

server {
  listen 8083;
  server_name admin.example.top;
...

与 HOST 匹配的服务器块将处理请求。