« Back to Index

[nginx redirect request to separate server block with different server_name]

View original Gist on GitHub

Tags: #nginx #rewrite #server

nginx redirect request to separate server block with different server_name.conf

# let's imagine that we have two virtual servers
#
# 1. *.example.com
# 2. *.foo.com
#
# let's also imagine that nearly all our requests are from host www.example.com
# so the first server block would handle those requests
#
# but if we get a request for a location that doesn't match in the *.example.com
# how do we get it to failover to the other server block to see if that has the path?
#
# we can use a rewrite rule (http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)...

set $is_foo 0;

if ($host = 'foo.com') {
  set $is_foo 1;
}

if ($is_foo = 1) {
  rewrite ^/(.*)$ http://www.foo.com/$1 permanent; # see link above for the various flag options
}