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

3
  • What did you expect to happen? What caused you to believe that was a command that would result in what you expected?
    – womble
    Oct 3, 2019 at 4:12
  • As told in this very easy DO guide, I was expecting the testing result to be something like `Syntax is ok. Test is successful'. Oct 3, 2019 at 4:15
  • That guide is wrong.
    – womble
    Oct 3, 2019 at 4:22

1 Answer 1

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

3
  • Ok. Do you mean I should proceed further acc to DO guide? Unfortunately DO guide, (link given in my above comment) just asks to use this command to 'test the configuration'. But it fails to specify what to expect in the result. And users like me get afraid at the words like "test failed". Oct 3, 2019 at 4:33
  • Can you kindly tell me if I should proceed acc to this DO guide? It it correct, considering the fact that another user above has commented that the guide is not trustworthy (it indeed is a surprise if the same IS wrong). Oct 3, 2019 at 4:37
  • @BathindaHelper yes i wouldn't trust it as we've already established it has incorrect information in it. Try the tutorial from Digital Ocean, in general they are a great resource. Like this one for example: digitalocean.com/community/tutorials/…
    – esoroka
    Oct 3, 2019 at 4:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .