繁体   English   中英

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

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

I start the app using yarn, in my local machine, configure a local nginx service listening on the port 8083, and the yarn service listening with port 3000, when the url path start with /manage , forward the http request to the backend service deployment在远程 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 Z0994939540DBZEF31 转发。 这是 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 无法正确识别请求。 我应该怎么做才能将本地机器 header 更改为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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM