惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

张戈博客

FastTTS:支持私有化部署和源阅读无缝对接的语音合成服务张戈博客 | 张戈博客 gRPC开发过程中遇到的问题记录张戈博客 | 张戈博客 SQLAlchemy因密码含有@符号连接MySQL失败张戈博客 | 张戈博客 Flyer:基于FastAPI的轻量级API开发框架张戈博客 | 张戈博客 解决paramiko使用invoke_shell交互式命令超时问题张戈博客 | 张戈博客 分享一个APISIX网关返回502的典型案例张戈博客 | 张戈博客 解决百度搜索出现安全中心提醒张戈博客 | 张戈博客 APISIX运维优化之解决长尾请求(耗时抖动)问题张戈博客 | 张戈博客 APISIX运维优化之配置文件自动化生成方案张戈博客 | 张戈博客
APISIX高级路由之301/302跳转配置张戈博客 | 张戈博客
张戈博客 · 2022-03-18 · via 张戈博客

我们这有个内部网站同时支持 2 个域名访问,最近因升级需求希望统一到一个域名,即老域名做一个 301 跳转到新域名。这个在 Nginx 直接配置一个 if 逻辑,判断是老的 host 并跳转到新的域名即可。那这个 Case 在 APISIX 里面应该怎么配置呢?

看了下 APISIX 文档,发现有两种跳转插件:redirectresponse-rewrite,前者只能跳转 uri 不包含协议主机部分,后者可以通过修改返回头和状态码来实现跳转。

因此,这里选择response-rewrite来实现,经过一番验证,最终配置(信息已简化处理)如下:

{
  "uris": [
    "/*"
  ],
  "hosts": [
    "www.domain1.com",
    "www.domain2.com"
  ],
  "plugins": {
    "response-rewrite": {    # 跳转插件配置
      "disable": false,
      "headers": {
        "Location": "$schema://www.domain2.com$uri"  # 跳转到 www.domain2.com
      },
      "status_code": 301,
      "vars": [
        [
          "host",
          "==",
          "www.domain1.com"    # 当用户访问的是 www.domain1.com 的时候才跳转
        ]
      ]
    }
  },
  "service_id": "demo_service",
  "status": 1
}

上述路由配置的跳转逻辑为:当访问域名是 www.domain1.com 时,跳转到 www.domain2.com。可以看到,通过response-rewrite 插件,我们仅用一个路由就满足了上述跳转需求,但是这个方法有个弊端,就是这个插件其实是在请求完后端之后生效的,因此这个请求其实已经到了后端服务,然后才跳转的,不是特别优雅。