2

I have several domain names registrations like example.com example.co.uk example.net I think you get the idea.

At the moment I am creating multiple server block in the nginx config file like:

server {
    listen 80;
    server_name example.com www.example.com 
    root /var/www/example.com/html 
}

server {
    listen 80;
    server_name example.co.uk www.example.co.uk
    root /var/www/example.com/html

}

And a few more of these. Basically they are all pointing to the same website in one folder.

My question is - could it be possible to create just server block to serve the same purpose? like

server {
     listen 80
     server_name example.com example.co.uk example.net // etc
     root /var/www/example.com/html 
}

Thanks in advance.

4

Yes, it's possible.

You can use "*", even you can use regular expression, a good solution for you could be:

server {
     listen 80
     server_name ~^example.com|example.co.uk|example.net$;
     root /var/www/example.com/html 
}

I need do something like a peopleusername.wix.com and I just should do:

server {
     listen 80
     server_name  *.wix.com;
     root /var/www/example.com/html 
}

and from the code I just can access to the full url from the request and extract the username from the url and fetch the user ID from the database and put all user configuration and data from the user togeter to show the theme and colors that the users configure

There's no magic:

  • the server_name accept regular and wildcard expressions server_name *.example.org; which is exactly what I need, awesome! – gsalgadotoledo Nov 15 '17 at 4:06
  • I do have one question... What if we write the bash script to create new server with new domain name with reload config on it. or just accept whatever subdomain the request url has..? – Suman Astani Oct 4 '18 at 8:40
  • and do we need to configure any thing in /etc/hosts in our server..? – Suman Astani Oct 4 '18 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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