요청한 파일이 존재하는데도 Debian의 nginx가 404를 반환합니다.

요청한 파일이 존재하는데도 Debian의 nginx가 404를 반환합니다.

요청한 파일이 존재하는데도 Debian의 nginx가 404를 반환합니다.

나는 공식 문서를 따랐습니다 (https://nginx.org/en/linux_packages.html#Debian) 데비안에 nginx를 설치합니다.

  1. 설치 전제조건:
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
  1. apt가 패키지의 신뢰성을 확인할 수 있도록 공식 nginx 서명 키를 가져옵니다. 가져오기 > 키:
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 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. Photos도 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"

내 구성 파일에 필요한 줄이 누락되었나요? 서버가 예상대로 작동하려면 무엇을 추가, 편집 또는 삭제해야 합니까?

ベストアンサー1

おすすめ記事