We help IT Professionals succeed at work.

Duplicate server_name in nginx config

Balbir Singh
Balbir Singh asked
In the nginx.conf I have following line

   # all the virtual hosts
    include vhosts/*.conf;

Now I keep on adding new config file under vhosts directory and then I reload the nginx. Now my script add the custom server_name  web1.example.com; in config files

so while adding the config file I do not check if any of the existing config under vhosts directory already have a server_name web1.example.com. So I am wondering what will happen if multiple config file has same server_name. Isnt' there going to be conflict as each config has different proxy_pass address. I would like to know how to what happen in such case of duplicate server_name and what is the best practice.

Thanks in advance!
Watch Question

Software Engineer
Distinguished Expert 2019

Yes there is a conflict you CAN ask nginx to verify it's configuration so you can undo your change...

nginx -c configfile -t

Either nginx won t start, or It will pick one config. Afaik, current nginx versions use the last loaded config but i am unsure.
@balbir : see the algo described in your other thread : drop files, check the config, copy the files if different from the original, restart/reload nginx if the nginx process is older than any of the files.