Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In Apache, if 1) I have multiple virtual hosts listening to one port and 2) if no requests match any of the virtual host, then the first virtual host will be the one that will answer the request. For example, given I have this config:

<VirtualHost *:8080>
  ServerName www.fallbacksite.com:8080
  ServerAlias *.fallbacksite.com
  # ...
</VirtualHost>

<VirtualHost *:8080>
  ServerName www.specific-site.com:8080
  ServerAlias *.specific-site.com
  # ...
</VirtualHost>

Then:

  • When someone specific-site.com:8080, then the second virtual host will be answer the request (no surprise here)
  • When someone visits fallbacksite.com:8080, then the first virtual host will answer the request (no surprise here)
  • When someone visits hellohowareyou.com:8080 (and hellohowareyou.com points to the server), then the first virtual host will answer the request

It's the last scenario I'm trying to implement with nginx, but could not see anything in the nginx.org site talking about this. I tried over-eager wildcards but it complains that it is not valid.

How would you do the above confiuration in nginx? Thanks in advance!

share|improve this question
add comment

1 Answer

If you only want a specific virtual host to answer the request you can simply do as following. Add (or change) the listen line to include default_server at the end. This will serve that virtual host without changing the URL.

server {
    # Add default_server for any unresolved request
    listen 80 default_server;        
}

If you want to do a redirect when a user hits hellohowareyou.com you can add a new server block and redirect to the host you want to use:

server {
    # Add default_server only here for any unresolved request
    listen 80 default_server;    
    server_name _;
    rewrite ^ http://fallbacksite.com$request_uri permanent;
}
share|improve this answer
add comment

Your Answer

 
discard

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.