即使请求的文件存在,Debian 上的 nginx 也会返回 404

即使请求的文件存在,Debian 上的 nginx 也会返回 404

即使请求的文件存在,Debian 上的 nginx 也会返回 404

我按照官方文档(https://nginx.org/en/linux_packages.html#Debian)在 Debian 上安装 nginx。

  1. 安装先决条件:
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
  1. 导入官方 nginx 签名密钥,以便 apt 可以验证包的真实性。获取 > 密钥:
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
  | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
  1. 验证下载的文件是否包含正确的密钥:
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings>/nginx-archive-keyring.gpg
  1. 如果您想使用主线 nginx 软件包,请运行以下命令:
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
  http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
  | sudo tee /etc/apt/sources.list.d/nginx.list
  1. 设置存储库固定以优先选择我们的包而不是发行版提供的包:
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
  | sudo tee /etc/apt/preferences.d/99nginx
  1. 要安装 nginx,请运行以下命令:
sudo apt update
sudo apt install nginx

完成安装后,我将继续阅读初学者指南(https://nginx.org/en/docs/beginners_guide.html)。我跑去sudo nginx打开它,然后继续执行第 7 步。

  1. 首先,创建/data/www目录并将index.html包含任何文本内容的文件放入其中,然后创建/data/images目录并在其中放置一些图像。
mkdir /etc/nginx/data/
mkdir /etc/nginx/data/www
set +H
echo -e "<!DOCTYPE html>\n<html>\n<body>\n<h1>My First Website</h1>\n<p>My first paragraph.</p>\n</body>\n</html>" \
    | sudo tee /etc/nginx/data/www/index.html
set -H
mkdir /etc/nginx/data/images
cp /home/alex/Pictures/IMG_0685.jpg /etc/nginx/data/images/sample.jpg
  1. 接下来,打开配置文件。默认配置文件已包含服务器块的几个示例,其中大部分已注释掉。现在注释掉所有此类块并启动一个新的服务器块:
  http {
      server {
      }
  }

所以我用sudo nano /etc/nginx/nginx.conf.

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

events {
    worker_connections  1024;
}

http {
    include     /etc/nginx/mime.types;
    include /etc/nginx/conf.d/*.conf;
    default_type    application/octet-stream;
    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;
    keepalive_timeout   65;
    #gzip   on;

    server {
        listen 80;
        location / {
            root /data/www;
        }

        location /images/ {
            root /data;
        }
    }
}
  1. 在将 reload >configuration 命令发送到 nginx 或重新启动 nginx 之前,配置文件中所做的更改不会应用。要重新加载配置,请执行:
sudo nginx -s reload

我正是这么做的。然后我运行curl来检查我的进度。

curl 127.0.0.1:80/index.html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
curl 127.0.0.1:80/images/sample.jpg

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.25.1</center>
</body>
</html>

所以服务器已安装,但我得到一个默认页面而不是我自己的页面index.html。照片还返回 404。我检查了日志:

sudo cat /var/log/nginx/access.log

127.0.0.1 - - [08/Jul/2023:16:40:55 +0000] "GET /images/sample.png HTTP/1.1" 404 153 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" "-"

sudo cat /var/log/nginx/error.log

2023/07/08 16:40:55 [error] 1212021#1212021: *11 open() "/usr/share/nginx/html/images/sample.png" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /images/sample.png HTTP/1.1", host: "127.0.0.1"

我的配置文件是否缺少必要的行?我应该添加、编辑或删除哪些内容才能使服务器按预期工作?

相关内容