nginx 同时服务端口 80 和 443,但始终在端口 80 上返回 HTTP 404

nginx 同时服务端口 80 和 443,但始终在端口 80 上返回 HTTP 404

我在 Raspberry Pi 4 上运行 nginx v1.22.1(基于 Debian Bookworm)。 nginxNot Found对通过 HTTP/port 的所有请求返回 HTTP 404 80

在此输入图像描述

然而,通过 HTTPS/port 一切都运行良好443

这是我的/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
}

http {
        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;
        access_log /var/log/nginx/access.log;
        gzip on;
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

这是我配置的网站etc/nginx/sites-enabled/myraspi.conf

server {
    listen 80;

    location /hello {
        add_header Content-Type text/html;
        return 200 'Here I am!';
    }
}

server {
    listen 443 ssl;
    ssl_certificate /etc/ssl/certs/myraspi.fritz.box.crt;
    ssl_certificate_key /etc/nginx/ssl/certificates/myraspi.fritz.box.pem;

    location /world {
        add_header Content-Type text/html;
        return 200 'Here I am!';
    }
}

我可以成功调用HTTPS GET myraspi/world,但是不行HTTP GET myraspi/hello。但是,由于错误页面包含 nginx 页脚,因此似乎是 nginx 配置问题。

我已经检查过 nginx 是否监听这两个端口 - 情况是这样:

myuser@myraspi:/etc/nginx $ sudo netstat -nlp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1972636/nginx: mast
tcp6       0      0 :::80                   :::*                    LISTEN      1972636/nginx: mast

myuser@myraspi:/etc/nginx $ sudo netstat -nlp | grep 443
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1972636/nginx: mast

配置检查sudo nginx -t也成功:

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

错误日志/var/log/nginx/error.log不包含任何错误,并且在访问日志中/var/log/nginx/access.log,我看到传入的请求:

<<my IPv6 IP address>> - - [30/Mar/2024:13:35:03 +0100] "GET /hello HTTP/1.1" 404 125 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0"

关于可能出现的问题有什么想法吗?

谢谢!

答案1

我必须添加以下行来etc/nginx/sites-enabled/myraspi.conf添加 IPv6 支持:

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

    location /hello {
        add_header Content-Type text/html;
        return 200 'Here I am!';
    }
}

相关内容