21

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.

Confucius
  • 419
  • 3
  • 7
  • 14
  • What version of nginx did it install? According to this website the version you get with apt-get has some issues. http://virtualitblog.blogspot.nl/2013/05/install-nginx-141-raspberry-pi.html 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 Oct 03 '13 at 19:17
  • 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](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/](http://127.0.0.1/) I can see the default site (**/usr/share/nginx/www/index.html**). – Confucius Oct 05 '13 at 20:23
  • Please select the answer that worked best for you. Thank you. – Philll_t Jul 01 '16 at 21:48

9 Answers9

42

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.

JoeG
  • 7,191
  • 10
  • 60
  • 105
Andres Bott
  • 690
  • 6
  • 7
  • 1
    I had the same error. Didn't think this was the error to start of with. I tried copy pasting my text into notepad. Tried to use online tool to cleanup my text. But windows kept screwing with it. I solved it by spinning up a Linux VM and creating the same document. Then I transferred the file to windows and used it for my script. It worked. I couldn't tell the difference between the linux file and windows file. But it fixed it for me. – Kiksen Oct 24 '19 at 05:59
  • 1
    So the line-endings in windows are CRLF or \r\n. With nginx the parser does not understand the windows CRLF, but only understands LF (\n). Remove them using an editor and it works. – Martijn van Wezel Apr 30 '21 at 13:33
  • Also check for a BOM (byte order mark); NGINX wouldn't read my config because my IDE added the BOM by default. – simshaun Apr 27 '23 at 21:30
11

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.

Philll_t
  • 4,267
  • 5
  • 45
  • 59
1

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.

Ahmed
  • 1,231
  • 1
  • 10
  • 8
1

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.

shanmuga raja
  • 685
  • 6
  • 19
0

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.

Malathy
  • 337
  • 5
  • 14
0

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;
Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Raja G
  • 5,973
  • 14
  • 49
  • 82
0

Really didn't got how that happened,
I've "docker run " my nginx 1.14 (default apt for debian:bullseye) to extract it's default nginx.conf, with intention to update it and than use it into my Dockerfile.

Anyhow, after having read comments in this thread, found this that the file is "UTF-16LE" ... I'm not really expert, but is not "UTF-8".

solved as:

Issue seen from inside the container:

me@docker-nginx $ head nginx.conf 
��
user www-data:qgis;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
error_log /dev/stdout warn;


events {
    worker_connections 768;

me@docker-nginx  $ dos2unix nginx.conf 
dos2unix: converting UTF-16LE file nginx.conf to UTF-8 Unix format...

Solved also on working dir:
IntelliJ IDEA: select "convert" after "UTF-8"
enter image description here

LittleEaster
  • 527
  • 7
  • 10
0

I had the same problem when I looked inside my config file there was a syntax error so this might be the problem check this path

nano /etc/nginx/sites-available/example.com
Salio
  • 1,058
  • 10
  • 21