apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
13.97k stars 2.45k forks source link

help request: How to redirect sub-links in the upstream? #10863

Open AmerDwight opened 5 months ago

AmerDwight commented 5 months ago

Description

I wonder if it's a foundamental knowledge that I shall have. If there are docs, tutorials, or guides related to those, please let me know.

If I set a route for an upstream, which provides a web( html ) in response. And there is a sub link in that page, e.g. mainpage with a link to a sub-page. Then when I click it on blowser, where shall it directs to ?

  1. If that link uses a absolute address to sub-page --> Clients get 404 on blowser
  2. If that link uses a referal address to sub page --> Clients get ? ( still 404 in my test)

I don't know if there is a way to solve situ.1 So I tried to build a sample to testify the situ.2

Here are routes that I set in apisix Upstream:nginx_web Routes:

{
  "uri": "/web/*",
  "name": "Web",
  "methods": [
    "GET",
    "POST"
  ],
  "plugins": {
    "proxy-rewrite": {
      "regex_uri": [
        "^/web/(.*)",
        "/$1"
      ]
    }
  },
  "upstream_id": "490103218550866940",
  "status": 1
}

So if the client calls apisix/web/mainpage, it directs to nginx_web/mainpage, which is fine. but when I click in some link to another sub-page, it goes to: apisix/subpage ( I use referal address ) --> and this cannot be identified in apisix routing.

How do we solve that? and evenmore, how do we keep clients info to next page?

belows are samples, modified by apisix public case: Upstream:nginx web server

worker_processes 1;
error_log stderr notice;
events {
    worker_connections 1024;
}

http {
    variables_hash_max_size 1024;
    access_log /var/log/nginx/access.log;
    real_ip_header X-Real-IP;
    charset utf-8;

    server {
        listen 80;

        # 定义服务器的根目录
        root /usr/share/nginx/html;

        # 默认页面
        location / {
            index sample2.html;
            # return 200 "This is sample 2 from nginx web server 2 \n";
        }

        # sample1 页面
        location /sample1 {
            try_files $uri $uri/ /sample1.html;
        }

        # sample2 页面
        location /sample2 {
            try_files $uri $uri/ /sample2.html;
        }

        # 健康检查
        location /health {
            return 200 "OK";
        }

        access_log /var/log/nginx/domain1.access.log;
    }
}

Page: 1.

<!-- sample1.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample 1</title>
</head>
<body>
<p>This is sample 1 from nginx web server.</p>
<a href="/sample2"><button>Go to Sample 2</button></a>
</body>
</html>

2.

<!-- sample2.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample 2</title>
</head>
<body>
<p>This is sample 2 from nginx web server.</p>
<a href="/sample1"><button>Go to Sample 1</button></a>
</body>
</html>

Environment

Vacant2333 commented 5 months ago

your route was not include nginx_web/mainpage, u must add a new route to include it

AmerDwight commented 5 months ago

Nice to have your help.

I was considering to set up a route in a general way. So I choose proxy-rewrite to embed the logic.

The mainpage works fine now. The problem is, when i click a link inside mainpage, it won't be able to get recognised by apisix.

I'm asking about is there a way to solve this?

Vacant2333 commented 5 months ago

i think its not APISIX's problem, if the link was in the route so it will be recongnised by apisix

AmerDwight commented 5 months ago

Ummm, I can catchup your point. But if the link is at the same namespace, ex: target.com/mainpage --> target.com/subpage Do you mean I shall set up two routes for both of them?

Is it a conventional way to achieve that?

Vacant2333 commented 5 months ago

Ummm, I can catchup your point. But if the link is at the same namespace, ex: target.com/mainpage --> target.com/subpage Do you mean I shall set up two routes for both of them?

Is it a conventional way to achieve that?

yes, mainpage and subpage was 2 route

AmerDwight commented 5 months ago

Thanks for your help.

I still confuse about that. If I'm a gateway manager, I won't be aware when upstream provider add some pages in their project. Further, if there are some page components that interact with server, how do I set up routings?

BTW, is there any guide or something, that I can refer to? since I didn't find docs related to this topic.