Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have around 50 identical domains names with different tld like:,,,, now I would like to forward all incoming requests to this domain with and without www on nginx to: how I can do this without need to list every domain?

Something like: forward all request to .domain. or domain.* to: permanent

share|improve this question
up vote 2 down vote accepted

Use nginx server_name directive with wildcards. Example:

server { 

  ... real server ...

server { 
  server_name domain.* www.domain.*; 
  rewrite ^ $scheme://$request_uri? permanent; 
share|improve this answer
The order doesn't matter for the reason you stated. Wildcard server name entries are only checked if no exact match is found. However, it does matter if no server_name matches, in which case you may want the order reversed, so the redirecting server would still redirect to the desired name. The answer is still a good one, but you should probably remove that comment. – kolbyjack Jun 9 '11 at 18:34
You are absolutely right, I've missed the fact that static names matched before wildcard domains. I've removed this misleading comment. – AlexD Jun 10 '11 at 3:08
works great, thank you. – Nenad Jun 10 '11 at 11:46

I never checked using a catch all tld but its worth a try. Don't have access to any nginx install right now, so if you can try the following do let me know too if it worked or not.

server {

            listen   80;
            server_name  domain.* *.domain.*;
            rewrite ^/(.*)$1 permanent;

share|improve this answer

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.