简体   繁体   中英

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

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.

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.

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.

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.

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.

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.

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;

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.

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"
在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM