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 theodbmatch 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 originalXXXXXXodbXXXXXorXXXXXXXXXXXsegments stay intact, no matter where "odb" appears in the path. - Redirect Type:
permanentsends a 301 (permanent) redirect, which is great for SEO and caching. If you need a temporary redirect (302), swappermanentwithredirect.
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
相关产品推荐
相关产品推荐

