Question

What exactly is "server_name" in nginx configuration file ?

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 ERR_CONNECTION_REFUSED.

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 ?

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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