Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

In an nginx server block:

server {
  listen       80;


How do I redirect and only to - the other hosts should remain unaffected. So and should work as normal.

share|improve this question

1 Answer 1

up vote 0 down vote accepted
if ($host ~* ^$) {
    rewrite ^/site(.*)$ /;

or different server locations with include of shared zones

share|improve this answer
I like "different server locations with include of shared zones" better. Nginx docs say if is evil and recommend avoiding it when possible. (But rewrite is one of the directives they say is safe inside if blocks, so this answer is perfectly correct.) – pjmorse Oct 18 '12 at 13:24
So obvious! Thanks :) I've just added a permanent so its a redirect rather than a rewrite. – jaygooby Oct 18 '12 at 13:40
Yep, if its evil. Check all requests not the best practice for high-load servers. More faster - takes 3 servers like: server {server_name; include someconf.conf;} – unlo Oct 18 '12 at 13:43
Yep, well aware of the "if is evil" :) – jaygooby Oct 18 '12 at 13:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.