nginx 不监听 80 端口两次? [英] nginx doesn't listen on port 80 twice?

查看:326
本文介绍了nginx 不监听 80 端口两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了接受的答案外,在没有 systemd 的情况下启动 nginx 时会发生这些错误.杀死 nginx:ps -ax |grep nginx → 找到nginx master pidkill ###;运行 nginx with systemd: systemctl start nginx.

Apart from the accepted answer, these errors occur when nginx is started without systemd. Kill nginx: ps -ax | grep nginx → find the nginx master pidkill ###; run nginx with systemd: systemctl start nginx.

如果没有使用 systemctl 来启动 nginx, systemctl stop nginx 似乎不起作用(至少在我的服务器上);因此,systemctl restart nginx 在尝试第二次启动 nginx 时会出现此错误.

If systemctl is not used to start nginx, systemctl stop nginx doesn't seem to work (at least on my server); so, systemctl restart nginx gives this error when it tries to start nginx a second time.

我使用的是 Debian 10,Buster,有一个实时服务器.我已经测试了两个域名,它们使用这些配置文件自行广播,但是当两个配置文件都处于活动状态时,它们不会广播.

I'm on Debian 10, Buster, with a live server. I have tested both domain names, and they broadcast by themselves using these config files, but they do not broadcast when both config files are active.

如何使用 nginx 在一个 IP 地址上设置两个网站?

How do I set up two websites on a single ip address using nginx?

nginx: [emerg]/etc/nginx/sites-enabled/example.com:22 第 2 行中 0.0.0.0:80 的重复默认服务器.

nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/example.com:22 line 2.

nginx: [emerg]/etc/nginx/sites-enabled/example.com:22 第 2 行中 0.0.0.0:443 的重复默认服务器.default_server 用于端口 443.

nginx: [emerg] a duplicate default server for 0.0.0.0:443 in /etc/nginx/sites-enabled/example.com:22 line 2. default_server is used on port 443.

nginx: [emerg] 在/etc/nginx/sites-enabled/example.com:23 第 3 行中为 [::]:443 重复监听选项.default_server 在一个文件中尝试使用 http2 并使用端口 443.

nginx: [emerg] duplicate listen options for [::]:443 in /etc/nginx/sites-enabled/example.com:23 line 3. default_server tried as http2 in one file and port 443 is used.

nginx: [emerg] 在/etc/nginx/sites-enabled/example.com:23 第 3 行中为 [::]:80 重复监听选项.default_server 在一个文件中尝试作为 http2.

nginx: [emerg] duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/example.com:23 line 3. default_server tried as http2 in one file.

nginx: [emerg]/etc/nginx/sites-enabled/example.com:23 中的无效参数example.com"第 3 行:default_server 尝试为example.com

nginx: [emerg] invalid parameter "example.com" in /etc/nginx/sites-enabled/example.com:23 line 3: default_server tried as example.com

有两个这样的文件(见下面的代码).我的配置文件与下面的代码块完全一样,只有一个区别:在两个文件中,example.com 是我拥有的真实、唯一的域名.

There are two of these files (code seen below). My config files are exactly as-seen in the code block below with only one difference: in both files, example.com is a real, unique domain name that I own.

我的配置文件位于 /etc/nginx/sites-available,它们被符号链接到 /etc/nginx/sites-enabled.

My config files live in /etc/nginx/sites-available, and they are symlinked to /etc/nginx/sites-enabled.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/example.com; #example.com is different in both files.
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com; #example.com is different in both files.

    location / {
        try_files $uri $uri/ =404;
    }
}

主要失调:

另一个堆栈问题说要重写这些行或将其中的一行注释掉.重写要么无法解决错误,要么导致文件未公开提供的情况(example.com 无处可去).

Primary dissonance:

Another stack question says to rewrite these lines or comment one of them out. The rewrites either do not resolve the error or result in a situation where files are not publicly served (example.com goes nowhere).

这个问题似乎是关于第 2 行、第 3 行或第 2 行和第 3 行.

This question seems to be about line 2, line 3, or line 2 and 3.

基本上,所有主要的操作方法要么明确告诉读者使用上面的代码-要么-他们没有提到这些行.

Essentially, all of the leading how-to's either expressly tell the reader to use the code above -or- they don't mention these lines.

推荐答案

当您在 listen 指令上指定 default_server 标志时,nginx 将使用该 server 块以服务任何 HTTP Host 标头与任何其他服务器块中的 server_name 不匹配的请求(或请求缺少 Host 标头一点).您可以在任何特定 IP:port 组合上使用 default_server 标志作为 listen 指令的参数仅一次.但是您可以在不同的 IP:port 组合上多次使用此标志.

When you specify default_server flag on a listen directive, nginx will use that server block to serve any request where HTTP Host header does not match the server_name in any other server blocks (or the request missing Host header at all). You can use default_server flag on any particular IP:port combination given as parameter of listen directive only once. But you can use this flag several times on different IP:port combinations.

一些示例,假设您的服务器有多个网络接口:

Some examples, assuming your server have several network interfaces:

server {
    listen 1.2.3.4:80 default_server; # this will be default server block for any request coming to 1.2.3.4 IP address on port 80
    ...
}
server {
    listen 5.6.7.8:80 default_server; # this will be default server block for any request coming to 5.6.7.8 IP address on port 80
    ...
}
server {
    listen 80 default_server; # this will be default server block for any request coming to any other IP address (except 1.2.3.4 and 5.6.7.8) on port 80
    ...
}

工作示例:

通常,当我需要在同一台服务器上为多个站点提供服务时,我使用以下配置(当然这是简化的,现在我们通常使用带有 http://example.com 的 HTTPS 来https://example.com 重定向):

Working example:

Typically, when I need to serve several sites on the same server, I use the following configuration (of course this is simplified, nowadays we typically use HTTPS with http://example.com to https://example.com redirection):

站点 1 配置文件

server {
    listen 80;
    server_name example1.com www.example1.com;
    ...
}

站点 2 配置文件

server {
    listen 80;
    server_name example2.com www.example2.com;
    ...
}

默认服务器配置文件

server {
    listen 80 default_server;
    server_name _;
    return 444;
}

由于 listen 指令上的 default_server 标志,当请求的 Host 标头不包含我的站点名称之一时,将使用第三个服务器块(或者请求根本没有 Host 标头).我不希望访问者不知道他们正在访问的站点(这些通常是端口扫描程序、漏洞搜索程序等),因此我为他们使用了特殊的 nginx 444 代码(关闭连接没有任何响应).

Because of default_server flag on listen directive third server block is used when the request's Host header doesn't contain one of my sites names (or the request does not have Host header at all). I don't expect visitors that doesn't know what site they are visiting (these are typically port scanners, vulnerability searchers etc.), so I use special nginx 444 code (close connection without any response) for them.

这篇关于nginx 不监听 80 端口两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆