You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx服务器URL重定向配置咨询:含odb字符的URL跳转规则

Nginx URL Redirect Based on "odb" String Presence

Got it, let's tackle this redirect scenario step by step. You’re absolutely right that location and rewrite directives are the way to go here—here’s a clean, effective implementation that meets all your requirements:

The Configuration

Add these blocks inside your Nginx server block (the one for mynginxserver):

# Match any URL containing the string "odb" (case-insensitive)
location ~* odb {
    # Capture the full request path and redirect to the odb server
    rewrite ^(.*)$ http://www.myodbserver.com$1 permanent;
}

# Match all remaining URLs (those without "odb")
location / {
    # Capture the full request path and redirect to the non-odb server
    rewrite ^(.*)$ http://www.mynotodbserver.com$1 permanent;
}

Key Details Explained

  • Case Sensitivity: The ~* modifier makes the odb match case-insensitive. If you only want to match lowercase "odb", replace ~* with ~.
  • Path Preservation: The ^(.*)$ regex captures the entire request path (including any query parameters after ?) and appends it to the target domain using $1—so your original XXXXXXodbXXXXX or XXXXXXXXXXX segments stay intact, no matter where "odb" appears in the path.
  • Redirect Type: permanent sends a 301 (permanent) redirect, which is great for SEO and caching. If you need a temporary redirect (302), swap permanent with redirect.

Testing & Reloading

Before applying, always validate your Nginx configuration to avoid syntax errors:

nginx -t

If the test passes, reload Nginx to activate the changes:

nginx -s reload

This setup will handle all your redirect cases exactly as you described—any URL with "odb" goes to your odb server, everything else goes to the non-odb server, with full path preservation.

内容的提问来源于stack exchange,提问作者user4153231

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:56:30