大网数据广告活动
服务器运维问答

Redirect existing API requests to the new API format using Nginx rules

· 52CE 我爱测

Q1: I have an existing API that uses the format https://sub1.domain.com/whatever?test=yes on sub1 subdomain, but now I want to switch over to the sub2 subdomain and a format like https://sub2.domain.com/v2/page/embed?new=1 The ol…

问题原文

原帖链接 · Yasitha Bogamuwa · CC BY-SA 4.0

Q1: I have an existing API that uses the format https://sub1.domain.com/whatever?test=yes on sub1 subdomain, but now I want to switch over to the sub2 subdomain and a format like https://sub2.domain.com/v2/page/embed?new=1

The old requests should still work but be "forwarded" or "redirected" to the new request format and different subdomain. Ideally, this would happen seamlessly (transparently behind-the-scenes) and not require cURL to follow redirects, unless it is unavoidable.

Q2: Alternatively, I could compromise on replying to API requests for https://sub1.domain.com/whatever?test=yes using the same format but "forward" or "redirect" to the new subdomain like https://sub2.domain.com/whatever?test=yes

The point being that 25,000+ users with legacy format would not have to update their code to end up being served from the new server on sub2, and preferably using the new format (eliminating the need to maintain two sets of API parameters).

Appreciate your support on this. Thanks!

UPDATE:

I could find the answers myself. This will help someone in future.

For Q1:

location / {

   if ($arg_test ~* yes) {

       return 301 https://sub2.domain.com/v2/page/embed?new=1;

   }

}

For Q2:

location / {

   if ($arg_test ~* yes) {

       rewrite ^(.*)$ https://sub2.domain.com$1;

   }

}

已采纳解答

原帖链接 · Yasitha Bogamuwa · CC BY-SA 4.0

I could find the answers myself. This will help someone in future.

For Q1:

location / {

   if ($arg_test ~* yes) {

       return 301 https://sub2.domain.com/v2/page/embed?new=1;

   }

}

For Q2:

location / {

   if ($arg_test ~* yes) {

       rewrite ^(.*)$ https://sub2.domain.com$1;

   }

}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-10-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。