2

I've read some other answers to the almost exactly same question, but they don't seem to be addressing my query (some of them due to diff environ etc).

I'm setting up a new ubuntu server on DO. On it I created a file /etc/nginx/sites-available/3.conf. In this file only this code is there (I've tried the remaining lines of code also, but then also same result).

server {
     listen 80
     listen [::]:80;
     server_name 3.hotelbobbygg.xyz;
}

And yet when I test it like this root@2:~# nginx -t -c /etc/nginx/sites-available/3.conf It gives the following error:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/3.conf:1
nginx: configuration file /etc/nginx/sites-available/3.conf test failed

And if I delete all the contents in this file, even then it gives this error:

syntax is ok
nginx: [emerg] no "events" section in configuration
nginx: configuration file /etc/nginx/sites-available/3.conf test failed

And if I give this: nginx -t then it tests ok

Its just a new droplet, and besides updating the os and creating a sub folder under /var/www/ I haven't touched any other file.

Pls answer in the words for a guy who is new to servers and is trying to learn the basic functionality (I've setup a discourse site successfully on ubuntu and now am wanting to setup another website on the same droplet/server).

1 Answers1

3

You are using a wrong flag. From the documentation

-c filename Specify which configuration file NGINX should use instead of the default

So when you run

nginx -t -c /etc/nginx/sites-available/3.conf

You tell the server to use 3.conf instead of the main configuration. Which of course will give error, as the server block needs to be enclosed. And the sites-available files are included in the main conf file. When you run

nginx -t

It is ok, because the command verifies all the config files, including your sites-available. There is no reason to run it with -c flag, unless you are testing completely new main configuration

esoroka
  • 307