hrva07
By:
hrva07

What exactly is "server_name" in nginx configuration file ?

February 7, 2016 12.3k views
Nginx LEMP Ubuntu

What exactly is "server_name" in nginx configuration file ?

I'm trying to set up 2 websites with different domain names.
I bought 2 domain names and pointed them to my droplet IP. I use Ubuntu droplet and nginx.

It works when I set only 1 website - then both domain names results by opening that website.

But later I tried to create 2 websites and set nginx to serve each website to its domain name.
I get one of the two results: both domain names opens first website, or both domain names get ERRCONNECTIONREFUSED.

I played around with the nginx configuration, looked at some tutorials here, but it doesn't work for me.

So I want to go from start :-)

What exactly is "server_name" ? And what should it looks like ? Like domain name, folder name, configuration file name ?

8 comments
  • Each server block in your /etc/nginx/sites-enabled/default will have a server_name.

    Server name is what it will listen for, together with your port settings.
    So let's say you have a domain, you point your A name like: example.com to your droplets IP.
    This lets the DNS server know where the domain should go (like a street adress).

    Then you can set a server block inside the default file, with server name: example.com
    But you wish to also have this accessible via www.example.com, that mens you change it like so:
    server_name example.com www.example.com;

    If you then want to have a subdomain like: yolo.example.com and have it work in another directory, you make another server block and you there add server name:
    yolo.example.com

    You also have to add the a name in the DNS agian, point yolo.example.com to your ip.
    For maximum flexibility use:
    a name>floating ip>your server

  • What if I use only one file in sites-enabled ?

    And put two blocks in the same sites-availables file:
    server {...}
    server {...}

    because that way I could make it work :-s

    Is there some rule for configuration file name in sites-availables ?
    Because when I change the file name (and of course create link to sites-enabled, and delete others) then I get ERRCONNECTIONREFUSED

  • You can use one file, no problem..
    I have 4 server blocks in the default file my self:

    1. mydomain.tld, www.mydomain.tld (443/https)
    2. subdomain.mydomain.tld (443/https)
    3. http redirect (80) for 1.
    4. http redirect (80) for 2.

    If you feel it becomes bloated you can actually include in the file.. So you can copy out the duplicate content of server sections in external files and then include it back.

  • So I made it working with 1 configuration file.
    Then I copied the file and made the symlink in sites-enabled to new file (and delete the old one).

    And it doesn't work...

    Why is that so ?

    The same configuration, but different file name.

  • Hi, I havent tried with symlink.
    When I said include, I meant inside default file, like here: http://nginx.org/en/docs/ngx_core_module.html#include

    You can also do conditional includes, like: http://serverfault.com/questions/351710/how-do-you-conditionally-include-files-in-nginx-vhost

    etc..

  • You won't believe what my problem was !!!

    I created the symlink on wrong way:
    sudo ln -s test1 ../sites-enabled/

    Obviously, I have to put the whole path:
    sudo ln -s /etc/nginx/sites-available/test1 /etc/nginx/sites-enabled/

    Now that I create the symlink correctly, it doesn't matter if I put all in one file, or each site in different file - IT WORKS... very coool..

  • Hi, glad it worked out :-)

  • Yeah, it doesnt matter if it's one file or multiple files. You can even include one config in the other one (if your directive starts growing, or if you want to reuse commonly shared piece between other server blocks). Glad you figured it out. Here is the post that could help you in the future: What is nginx server_name and how it works?

1 Answer

This question was answered by @olavamjelde:

Each server block in your /etc/nginx/sites-enabled/default will have a server_name.

Server name is what it will listen for, together with your port settings.
So let's say you have a domain, you point your A name like: example.com to your droplets IP.
This lets the DNS server know where the domain should go (like a street adress).

Then you can set a server block inside the default file, with server name: example.com
But you wish to also have this accessible via www.example.com, that mens you change it like so:
server_name example.com www.example.com;

If you then want to have a subdomain like: yolo.example.com and have it work in another directory, you make another server block and you there add server name:
yolo.example.com

You also have to add the a name in the DNS agian, point yolo.example.com to your ip.
For maximum flexibility use:
a name>floating ip>your server

View the original comment

Have another answer? Share your knowledge.