19
votes

I have followed this website http://raspberrypihelp.net/tutorials/24-raspberry-pi-webserver to setup the HTTP server nginx on my Raspberry Pi and try to setup a site call example.com. But when I run sudo service nginx restart, it said

Restarting nginx: nginx: [emerg] unknown directive " " in /etc/nginx/sites-enabled/example.com:3

Here is the code in example.com.

    server {

    server_name example.com 192.168.1.88;

    access_log /srv/www/example.com/logs/access.log;

    error_log /srv/www/example.com/logs/error.log;

    root /srv/www/example.com/public/;

    location / {

        index index.php index.html index.htm;

        try_files $uri $uri/ /index.php?$args;

    }

    location ~ \.php$ {

        include /etc/nginx/fastcgi_params;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /srv/www/example.com/public$fastcgi_script_name;

    }

    location /phpmyadmin {

        root /usr/share/;

        index index.php index.html index.htm;

        location ~ ^/phpmyadmin/(.+\.php)$ {

            try_files $uri =404;

            root /usr/share/;

            fastcgi_pass unix:/var/run/php5-fpm.sock;

            fastcgi_index index.php;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            include /etc/nginx/fastcgi_params;

        }

        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {

            root /usr/share/;

        }

    }

    location /phpMyAdmin {

        rewrite ^/* /phpmyadmin last;

    }

}

I am just following the steps but it can't run successfully.

7
What version of nginx did it install? According to this website the version you get with apt-get has some issues. virtualitblog.blogspot.nl/2013/05/… May I suggest adding the nginx.org apt repository and re-installing nginx? Can you also run: ldd /usr/sbin/nginx and post the results somewhere with gist.github.com?Matthew Brown
I've used /usr/sbin/nginx -t -v to checked that I 'm using nginx/1.2.1, I have followed http://nginx.org/en/linux_packages.html to add it into apt sources.list, when I run sudo apt-get install nginx it return nginx is alreasy the newest version.. One thing I forgot to say is, I can go to http://127.0.0.1/ I can see the default site (/usr/share/nginx/www/index.html).Confucius
Please select the answer that worked best for you. Thank you.Philll_t

7 Answers

29
votes

I had the same problem which was that I copy/pasted the config code from the web and some dirty EOL(end of line) characters where there.

The editor didn't show them, but nginx treated them like a directive.

Just deleted every EOL and added again.

9
votes

It sounds like you did some copy and paste work here. It's not uncommon to snag some extra characters that are invisible at the end of line (EOL). Try this:

Run your text through this tool: http://www.textfixer.com/tools/remove-line-breaks.php

then fix any breaks that may have been removed and will be affected by the comments.

This worked for me. Hope it works for you.

1
votes

It looks like the nginx binary was compiled with --without-http_fastcgi_module option.This is not default. Try donwloading or compiling a different binary.

Try running

nginx -V

(with uppercase V) to see what options were used to compile the nginx.

0
votes

I edited some text in the mid of the conf file and nginx started showing this error at the starting of the file itself. I copied the contents of the file, created a new file, pasted the contents there and nginx stopped showing this error.

0
votes

I faced similar issue with error message as "unknown directive 'index.html'" when running 'sudo nginx -t'. After correcting the HTML syntax errors in index.html, the issue was resolved.

0
votes

Even if you miss a semicolon you will encounter the same error.

// Missed semicolon
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock

// With semicolon
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
0
votes

In my case, I have store configuration file nginx.conf in the github. I have done wget to raw version of code, thus resulted this error.

Later, I have cloned my repository and used the nginx.conf file from clone and issue got resolved.