【问题标题】:is it possible to change the http request header in local nginx forward是否可以在本地 nginx 转发中更改 http 请求标头
【发布时间】:2022-01-21 01:31:34
【问题描述】:

我使用yarn启动app,在我的本地机器上,配置一个本地nginx服务监听8083端口,yarn服务监听3000端口,当url路径以/manage开头时,将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 请求,它通过 http host 标头转发。这是 traefik 配置:

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

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

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

【问题讨论】:

    标签: http nginx kubernetes


    【解决方案1】:

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

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

    匹配主机的服务器块将处理请求。

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 1970-01-01
      • 2011-05-31
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 2016-03-18
      相关资源
      最近更新 更多