0

SOLVED (see my answer below)

When I added a new nginx config edu.conf in sites-available and sites-enabled, nginx just died.

I cant restart service and all sites wont work.

When I type sudo nginx -t -c /etc/nginx/sites-available/edu.conf or the same command on any other of my nginx configs it throws error:

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

I already had nginx config that was identical to the edu.conf, I just changed the root folders and server name.

If I check for syntax errors on any of previous files that were completely fine it throws a error on first line on all of them. Even the default one.

sw@asd:/etc/nginx/sites-available$ sudo nginx -t -c /etc/nginx/sites-available/magento2.conf
nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/sites-available/magento2.conf:1
nginx: configuration file /etc/nginx/sites-available/magento2.conf test failed
sw@asd:/etc/nginx/sites-available$ sudo nginx -t -c /etc/nginx/sites-available/default
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/default:16
nginx: configuration file /etc/nginx/sites-available/default test failed

Here is the edu.conf that I added:

upstream fastcgi_backend {
           server unix:/var/run/php/php7.0-fpm.sock;        
        #  Or TCP
        #  server  127.0.0.1:9000;
}
server {
        listen 80;
        server_name edu.dev;
        set $MAGE_ROOT /var/www/html/mg;
        set $MAGE_MODE developer;
        include /var/www/html/mg/nginx.conf.sample;
}
rits
  • 1,474
  • 7
  • 29
  • 49
  • They are not complete configuration files. They are intended to be included into `nginx.conf`, within an `http {}` block. – Richard Smith Jan 23 '17 at 12:24
  • I have one that is identical to it and it works completely fine with a magento 2 installation. – rits Jan 23 '17 at 12:39
  • The file is fine, but you cannot specify it on the command line - it needs to be included from within `nginx.conf` which is how `nginx` usually starts up. – Richard Smith Jan 23 '17 at 12:44
  • @RichardSmith I don't understand what I need to do. I had the default nginx conf, I added one for a magento 2 that is exactly like the one I am having problems now with and it worked completely fine, I didn't do anything else. I literally added the same file, but just renamed it and changed config so that it's for a different installation. – rits Jan 23 '17 at 13:06
  • Configurations are usually included from symbolic links in the `sites-enabled` directory. Add a symlink from `sites-available` for your active configurations and remove any symlinks for configurations you no longer require. – Richard Smith Jan 23 '17 at 14:27
  • @RichardSmith I already have that. The problem is that it says I have syntax error in every single nginx config file after I added edu.conf to sites-available and made a link to sites-enabled. – rits Jan 23 '17 at 14:38
  • So what happens when you invoke `sudo nginx -T`? – Richard Smith Jan 23 '17 at 14:40
  • @RichardSmith `nginx: [emerg] duplicate upstream "fastcgi_backend" in /etc/nginx/sites-enabled/magento2.conf:1` magento2.conf is the file I grabbed config from, but worked fine. – rits Jan 23 '17 at 14:49
  • Each upstream needs a unique name - you are loading two configs at the moment, both using the same symbol for the upstream identifier. – Richard Smith Jan 23 '17 at 14:51
  • Changed the upstream name and now all of the previous sites work and I can run `sudo service nginx restart`. But when I run `sudo nginx -t -c /etc/nginx/sites-available/edu.conf` or the same command for magento2.conf I still get `nginx: [emerg] "upstream" directive is not allowed here in FILE_PATH` – rits Jan 23 '17 at 14:56
  • As I said - you can't use `-c /etc/nginx/sites-available/edu.conf` because `edu.conf` is not a complete configuration file - it is only part of a configuration file - so on its own it is syntactically invalid. – Richard Smith Jan 23 '17 at 14:59
  • @RichardSmith what a complete one would look like then? Every config I have seen so far (for magento 2) is the same as my or just includes server {} block. It gets included in nginx.conf `include /etc/nginx/sites-enabled/*;` and then in the config includes this `include /var/www/html/magento2/nginx.conf.sample;` – rits Jan 23 '17 at 15:19
  • See my [recent answer here](http://stackoverflow.com/questions/41766195/nginx-emerg-server-directive-is-not-allowed-here/41766811#41766811) – Richard Smith Jan 23 '17 at 15:28

1 Answers1

0

The actual config was working, the problem was in the Magento 2 config. I had to change root $MAGE_ROOT/pub; to root $MAGE_ROOT; in nginx.conf.sample that is located in Magento 2 root directory. Or I had to type URL like this edu.dev/setup instead of edu.dev.

rits
  • 1,474
  • 7
  • 29
  • 49