重启 nginx:未知指令“listen”

Posted

技术标签:

【中文标题】重启 nginx:未知指令“listen”【英文标题】:Restart nginx: unknown directive "listen" 【发布时间】:2020-08-24 01:54:30 【问题描述】:

我看到已经有关于这个主题的帖子,但尽管尝试了我仍然做不到。

我想通过 AWS ec2 启动 nginx。下载 nginx 时,我没有 sites-enabled 和 sites-workable 文件夹,所以我按照此处的说明 (nginx missing sites-available directory) 创建文件并在 nginx.config 的 http 中添加 include 行。我还创建了符号链接。

但是没有成功...当我使用 sudo /etc/init.d/nginx restart 重新启动服务器时出现以下错误

"正在启动 nginx: nginx: [emerg] 未知指令 "listen:" in /etc/nginx/sites-enabled/react-flask-app.conf:2"

这是我的 /etc/nginx/sites-enabled/react-flask-app.con 文件:

server 
       listen: 80;
       root /home/ec2-user/cecs470-web-app-project-team2/test/testest/build;
       index index.html;

       location: / 
             try_files $uri $uri/ =404;
       

这是我的 /etc/nginx/nginx.conf 文件。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events 
    worker_connections 1024;


http 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/sites-enabled/*;
    include /etc/nginx/conf.d/*.conf;

    index   index.html index.htm;

    server 
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / 
        

        # redirect server error pages to the static page /40x.html
        #
        error_page 404 /404.html;
            location = /40x.html 
        

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
            location = /50x.html 
        

        # proxy the php scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ 
        #    proxy_pass   http://127.0.0.1;
        #

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ 
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht 
        #    deny  all;
        #
    

# Settings for a TLS enabled server.
#
#    server 
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        # It is *strongly* recommended to generate unique DH parameters
#        # Generate them with: openssl dhparam -out /etc/pki/nginx/dhparams.pem 2048
#        #ssl_dhparam "/etc/pki/nginx/dhparams.pem";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#        ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / 
#        
#
#        error_page 404 /404.html;
#            location = /40x.html 
#        
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html 
#        
#    



你有线索吗?谢谢

【问题讨论】:

指令应该是listenlocation不是listen:location: 看下面的答案 哦,谢谢!现在我有另一个问题,我的网站无法加载,我仍然有默认网站,你知道为什么吗?我停止并启动 nginx 您能否将新错误发布为更新,因为该错误已解决并选择一个答案 【参考方案1】:

来自以下文件的监听指令

my /etc/nginx/sites-enabled/react-flask-app.con

应该是

 listen 80;

你的有:

【讨论】:

以上是关于重启 nginx:未知指令“listen”的主要内容,如果未能解决你的问题,请参考以下文章

Nginx 未知指令“如果”

nginx 未知指令“上游”

NGINX未知指令流

Nginx-HTTP之listen指令解析

Nginx实用教程:配置文件入门

Nginx 与 Docker : nginx: [emerg] 未知指令“启用”在 /etc/nginx/nginx.conf