nginx:没有服务器处理程序

nginx: no handler for server

我正在尝试为 tcp 服务配置 nginx 代理,http 代理工作正常。但是,我一直在 stream 上下文中为服务器收到此错误。

no handler for server in /etc/nginx/nginx.conf:6

我不明白它与 http 上下文中定义的服务器有何不同。

events {

}

stream {
      server {
        listen 5601;
        deny   all;
    }
}

http {

    server {
        listen 80;
        server_name example.com;
        location / {
            proxy_set_header   X-Forwarded-For $remote_addr;
            proxy_set_header   Host $http_host;
            return 301 https://$host$request_uri;
        }
        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
    }
    server {
        listen 443 ssl;
        server_name example.com;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

        location / {
            proxy_pass http://example.com:5601;
        }
    }
}

这个错误是什么意思,我该如何解决?

问题出在这里:

listen 5601;
deny   all;

listen不明白,听这话我要做什么? 它立即找到了 deny all;拒绝后我会做什么?。 所以你必须告诉 listen 它代表什么。例如:

listen 853;
proxy_pass 127.0.0.1:53;

这里,我命令listen做某事。所以它知道它代表什么。它在那里侦听端口 853,然后代理到本地主机。是的,有些想法是这样的。 不要忘记重启

sudo systemctl restart nginx.services

你说:

I don't understand how it's different from the servers defined in the http context.

当然可以。因为 server on http 知道听完之后该怎么做。 :)