Added Server Block for Nginx and now it won't start.

February 8, 2018 143 views
Nginx

I followed this tutorial. How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 14.04 LTS

I have an Ubuntu 16.04 server with Nginx. I have had a production website running under Default for almost a year. It is located at /var/www/html. I used the tutorial to add another Server Block to point to /var/www/causeaction.com/html. I created the new server block in the file /etc/nginx/sites-available/causeaction.com. I linked that file to /etc/nginx/sites-enabled.

I edited the line in nginx.conf as stated in the tutorial.

When I tried to restart Nginx it failed. My production site is now down! So I deleted the link and the new server block file and was able to start Nginx again. Whew!

The new server block file, causeaction.com, contained ONLY what was mentioned in the Example as below:

server {
listen 80 defaultserver;
listen [::]:80 default
server ipv6only=on;

root /var/www/causeaction.com/html;
index.php index index.html index.htm;

server_name causeaction.com www.causeaction.com;

location / {
    try_files $uri $uri/ =404;
}

}

I then noticed that the original Default server block was Much larger and contained a bunch more lines.
Must I include in the new server block file ALL the lines that were in the original Default file but just change the entries at the top??? Note I kept the old Default file because it referred to the production website which is VERY important to keep running.

Thanks for any help.

3 Answers
jasonjpeters February 9, 2018
Accepted Answer

nginx -t is a helpful command to check your configs before a restart/reload

When nginx failed to start it likely included some details on the error it encountered. If they were not then you should be able to find these details in /var/log/nginx/error.log.

By default the config files in /etc/nginx/sites-enabled (and sites-available) should end in .conf - I do not know if this will cause a failure like the one you experienced but it is best practice for nginx configuration files.

  • Thanks for that info. I downloaded the log and it pointed to this line in the new server block.
    index.php index index.html index.htm;

    It should be:
    index index.php index.html index.htm;

    Now I am getting a "duplicate default server error".
    I will continue to track it down with the help of the logs.

    • @johnpkotuby

      Do you have multiple server blocks that have default_server in the listen directive? Only one block can have that directive, otherwise you'll notice that error.

      The default_server directive tells NGINX where to route requests that wouldn't otherwise match the request being made.

      For example, if a request comes through for domain.com and there's not a server block for domain.com, it'll direct that request to the block that has the default_server option set.

      • @jtittle
        Thanks for that explanation. I made the change such that there is only one reference to default_server in the server blocks. Now nginx starts just fine. Note that the server block for my new website/domain...in the file /etc/nginx/sites-available/causeaction.com... contains ONLY the code below.

        server {
            listen 80;
            listen [::]:80;
        
            root /var/www/causeaction.com/html;
            index index.php index.html index.htm;
        
            server_name causeaction.org www.causeaction.org;
        
            location / {
                try_files $uri $uri/ =404;
            }
        }
        

        I am still using the "default" file for my original site...and that is working fine.

        However when I surf to the new wordpress site, which was moved from mediatemple to our new Ubuntu server, the index.php is downloaded instead of run. I have been trying to fix that all day. I changed the Owner of the folder /var/www/causeaction.com/html (the website root) to www-data:www-data recursively as well as setting file and folder permissions to 755. The contents of index.php seem to point to the correct location. /** Loads the WordPress Environment and Template */
        require( dirname( FILE ) . '/wp-blog-header.php' ); I'm assumiing that /wp-blog-header.php refers to that file in the root of the website...which sits alongside index.php.

        The following PHP block of code is ONLY in the "default" file...not in my new causeaction.com file.

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
        #
        #   # With php7.0-cgi alone:
        #   fastcgi_pass 127.0.0.1:9000;
        #   # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                    fastcgi_read_timeout 300;
        }
        

        Should it be added to the "causeaction.com" file also? I'm not sure which version of PHP the wordpress site was using at Media Temple.

Well I guess that was the answer. I noticed while looking at the default server block that the php location block was indeed inside the server block of code. I copied the rest of the default server block code in to my new server block and php is now working. A very important point to add to the Digital Ocean tutorial I used to create my new server block.

I had thought that anything in the default server block would be applied to ALL server blocks. Not so!

Have another answer? Share your knowledge.