0

I've added the following global conf to the http block inside nginx.conf.

The purpose is to cover all php apps (WordPress apps and PHPmyadmin) in one block of conf, instead creating multiple conf files and their symlinks.

http {
    ..........................................
    server {
        listen 80 default_server;
        root /var/www/$host;
        location / {
            index index.php index.html index.htm;
        }
        location ~ {
            try_files $uri $uri/ /index.php;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    ..........................................
}

My problem

This configuration breaks the system - as long as it's inside nginx.conf, the system breaks.

The full (updated) nginx.conf can be seen here.

The only error in nginx -t is regarding this line listen 80 default_server; and it says:

a duplicate default server for 0.0.0.0:80 in /etc/nginx/nginx.conf:65

My question

Why does my global code breaks Nginx?

1
  • In your case you should use only one 'listen 80'. grep -rl 'listen 80' /etc/nginx . And you should adjust your nginx config for your wordpress. examples is here: codex.wordpress.org/Nginx Jan 30, 2018 at 12:41

3 Answers 3

0

Place a server stanza after the general configuration and it will be more likely to work - the configuration is read from top to bottom so any settings in your virtual host configurations would overwrite the settings as shown there.

Also, you can test your configuration with nginx -t which is helpful in showing where errors are.

0
  location {
            try_files $uri $uri/ /index.php;
        }

you don't have a location_match on that particular location block. Simply changing it to

            location ~ {
                    try_files $uri $uri/ /index.php;
            }

makes your configuration file valid.

Explanation:

Location blocks generally take the following form:

location optional_modifier location_match {

    . . .

}
The location_match in the above defines what Nginx should check the request URI against. The existence or nonexistence of the modifier in the above example affects the way that the Nginx attempts to match the location block. The modifiers below will cause the associated location block to be interpreted as follows:

(none): If no modifiers are present, the location is interpreted as a prefix match. This means that the location given will be matched against the beginning of the request URI to determine a match.

=: If an equal sign is used, this block will be considered a match if the request URI exactly matches the location given.

~: If a tilde modifier is present, this location will be interpreted as a case-sensitive regular expression match.

~*: If a tilde and asterisk modifier is used, the location block will be interpreted as a case-insensitive regular expression match.

^~: If a carat and tilde modifier is present, and if this block is selected as the best non-regular expression match, regular expression matching will not take place.

4
  • Hi, I changed as you suggest but still have the same problem. Please see my update in the question. There seem to be another problem. Jan 30, 2018 at 12:18
  • I'm using the nginx.conf you've provided in a link in your question. When updating the location block which errors out (line 15), the configuration file passes successfully syntax validation check: [root@server nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
    – 13dimitar
    Jan 30, 2018 at 12:22
  • Hmm strange. Will you allow to post your full nginx.conf so I'll compare the two in a diff tool? Jan 30, 2018 at 12:26
  • codeshare.io/aJ6Q6d
    – 13dimitar
    Jan 30, 2018 at 12:49
0

You can easily see for yourself:

# nginx -t -c /tmp/nginx_test.conf
nginx: [emerg] invalid number of arguments in "location" directive in /tmp/nginx_test.conf:15

He doesn't like location {, I guess.

You must log in to answer this question.

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