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

9

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.

Coleencolella answered 3/10, 2013 at 18:0 Comment(3)
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?Enschede
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).Coleencolella
Please select the answer that worked best for you. Thank you.Daiseydaisi
A
46

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.

Advise answered 22/2, 2014 at 21:27 Comment(3)
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.Chrysanthemum
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.Coryphaeus
Also check for a BOM (byte order mark); NGINX wouldn't read my config because my IDE added the BOM by default.Danzig
D
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.

Daiseydaisi answered 23/7, 2014 at 18:25 Comment(0)
B
2

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.

Bespeak answered 24/11, 2013 at 2:31 Comment(1)
Good approach, but how to recompile with necessary options?Winny
R
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.

Raymond answered 15/3, 2019 at 18:47 Comment(1)
This one helped me. Man, I owe you one.Nils
F
1

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;
Fantasm answered 10/5, 2021 at 15:51 Comment(0)
F
1

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

Foolscap answered 9/10, 2022 at 17:58 Comment(0)
K
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.

Kyanite answered 12/11, 2019 at 12:42 Comment(0)
S
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.

Snaffle answered 5/7, 2021 at 18:47 Comment(0)
N
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
Nereid answered 29/12, 2022 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.