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

Time:12-23

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 in the remote kubernetes cluster. This is my local nginx forward config:

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;
        }
}

In the kubernetes cluster, I am using traefik to forward the http request, it forward by the http host header. This is the traefik config:

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

now the http request give 404 not found error, I am sure the api path exists becuase I could invoke api in the test tools. I think the request send from local debugging app was localhost:8083 that make the traefik could not recognize the request correctly. what should I do to change the local machine header to admin.example.top that make traefik could recognize? or my config was mistake? what should I do to make it work as expect? This is the local request demo:

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

CodePudding user response:

Try add to curl request: -H 'HOST: admin.example.top'. Then update your nginx config:

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

Server block that match the HOST will process the request.

  • Related