无法使用第二个站点可用配置重新启动 nginx 服务器

无法使用第二个站点可用配置重新启动 nginx 服务器

我已在 中设置了一个 nginx 配置,并且运行良好/etc/nginx/sites-available/default-staging。今天,我尝试为基于 的 sendy 电子邮件营销应用程序添加另一个配置此配置

运行命令失败后service nginx restart我在线检查并发现nginx -c /etc/nginx/sites-available/sendy-newsletter -t应该突出显示任何语法错误,它给出了以下结果。

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/sendy-newsletter:1
nginx: configuration file /etc/nginx/sites-available/sendy-newsletter test failed

有两件事让我感到困惑,首先我不明白为什么不允许服务器阻止,在网上查了一下似乎这可能是由于sites-enabled配置文件未包含在hhtp我的块中而导致的nginx.conf,但它们是:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 2048;
    multi_accept on;
        use epoll;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

sites-available其次,当我从和文件夹中删除 sendy-newsletter 配置时sites-enabled,正如预期的那样,我可以成功重启 nginx 服务器,但是奇怪的是当我运行时,nginx -c /etc/nginx/sites-available/default-staging -t我收到与之前相同的错误消息,但是服务器可以工作!?

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/default-staging:1
nginx: configuration file /etc/nginx/sites-available/default-staging test failed

这是我的完整配置/etc/nginx/sites-available/sendy-newsletter,它是符号链接的,并且与sites-enabled

server {
    listen 80;
    #listen [::]:80;

    server_name newsletter.example.com;

    autoindex off;
    index index.php index.html;

    root /var/www/sendy-newsletter/public;
#    access_log /var/www/sendy-newsletter/logs/access.log;
#    error_log /var/www/sendy-newsletter/error.log;

    # Don't allow search engines to index any 
    add_header X-Robots-Tag "noindex, noarchive";

    location / {
        try_files $uri $uri/ $uri.php?$args;
    }

    location /l/ {
        rewrite ^/l/([a-zA-Z0-9/]+)$ /l.php?i=$1 last;
    }

    location /t/ {
        rewrite ^/t/([a-zA-Z0-9/]+)$ /t.php?i=$1 last;
    }

    location /w/ {
        rewrite ^/w/([a-zA-Z0-9/]+)$ /w.php?i=$1 last;
    }

    location /unsubscribe/ {
        rewrite ^/unsubscribe/(.*)$ /unsubscribe.php?i=$1 last;
    }

    location /subscribe/ {
        rewrite ^/subscribe/(.*)$ /subscribe.php?i=$1 last;
    }

    location ~ \.php$ {
        # --PHP5-FPM CONFIG START (keep fastcgi_param HTTPS OFF)--
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        ##fastcgi_param HTTPS $fastcgi_https;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # --PHP5-FPM CONFIG START--

        # --HHVM CONFIG START--
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #include        fastcgi_params;
        try_files $uri $uri/ @handler; 
        # --HHVM CONFIG END--

        include fastcgi_params;
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        access_log off;
        log_not_found off;
        expires 30d;
    }
}

更新

我检查了我的 nginx 错误日志并在尝试重新启动 nginx 后收到此消息。

2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: still could not bind()

这也是运行的结果:

$ sudo nginx -c /etc/nginx/nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

我还尝试在删除未使用的配置文件后运行语法测试

$ sudo rm default-staging $ sudo rm ../sites-enabled/default-staging $ sudo nginx -c /etc/nginx/sites-available/sendy-newsletter -t nginx:[emerg] /etc/nginx/sites-available/sendy:1 中不允许使用“server”指令 nginx:配置文件 /etc/nginx/sites-available/sendy 测试失败

答案1

当然,你会得到一个错误 :) nginx -c 应该检查你的整个 web 服务器配置,而不仅仅是服务器块。只需在主配置文件上运行它,你就会明白。它会自动考虑任何链接服务器指令。

这就是为什么您应该在进行检查之前从 sites-enabled 目录中删除您不使用的(链接到)指令。您可以随时将其链接回来。

答案2

我真是笨蛋,忘了我在另一款应用程序上运行了 varnish,所以端口 80 被占用了。这就是问题所在,错误日志为我指明了正确的方向,而 nginx -c 却让我感到困惑。

相关内容