wafflestudio / seminar-2021

2021 Rookies 세미나
47 stars 110 forks source link

장고 과제4 nginx stop 혹은 restart 시 오류 관련 #680

Open xxnpark opened 2 years ago

xxnpark commented 2 years ago

요약 & 상황

과제 4를 진행하면서 nginx.config 파일 수정 이후 nginx를 재시작하려고 할 때, 80번 포트가 사용중이라며 98: Address already in use 에러가 계속해서 발생하고 있습니다.

문제 내용

우선 sudo service nginx restart를 실행하면 다음과 같이 에러가 발생했습니다.

(venv) [ec2-user@ip-172-31-23-78 waffle_backend]$ sudo service nginx restart
Redirecting to /bin/systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

에러 내용을 확인해보았더니 포트가 이미 사용중일때 뜨는 98번 에러가 발생함을 확인할 수 있었습니다.

(venv) [ec2-user@ip-172-31-23-78 waffle_backend]$ systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 일 2021-11-14 05:20:55 UTC; 7s ago
  Process: 18626 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 18622 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 18621 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

11월 14 05:20:52 ip-172-31-23-78.us-east-2.compute.internal nginx[18626]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address al...use)
11월 14 05:20:53 ip-172-31-23-78.us-east-2.compute.internal nginx[18626]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address al...use)
11월 14 05:20:53 ip-172-31-23-78.us-east-2.compute.internal nginx[18626]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address al...use)
11월 14 05:20:54 ip-172-31-23-78.us-east-2.compute.internal nginx[18626]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address al...use)
11월 14 05:20:54 ip-172-31-23-78.us-east-2.compute.internal nginx[18626]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address al...use)
11월 14 05:20:55 ip-172-31-23-78.us-east-2.compute.internal nginx[18626]: nginx: [emerg] still could not bind()
11월 14 05:20:55 ip-172-31-23-78.us-east-2.compute.internal systemd[1]: nginx.service: control process exited, code=exited status=1
11월 14 05:20:55 ip-172-31-23-78.us-east-2.compute.internal systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
11월 14 05:20:55 ip-172-31-23-78.us-east-2.compute.internal systemd[1]: Unit nginx.service entered failed state.
11월 14 05:20:55 ip-172-31-23-78.us-east-2.compute.internal systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

그래서 80번 포트의 사용을 검색해보았더니 nginx worker process가 실행중인 것 같았는데, 그러면 어떻게 nginx를 재시작할 수 있는지 궁금합니다.

(venv) [ec2-user@ip-172-31-23-78 waffle_backend]$ netstat -anp | grep 80
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      16780/nginx: worker
udp6       0      0 fe80::49e:a7ff:fe06:546 :::*                                -
unix  2      [ ]         DGRAM                    80200    -
unix  3      [ ]         STREAM     CONNECTED     62257    16780/nginx: worker
unix  3      [ ]         STREAM     CONNECTED     12680    -                    /run/systemd/journal/stdout

또한 nginx를 종료하고 다시 시작하려고 sudo nginx -s stop을 실행해본 결과, nginx.pid 가 발견되지 않는 문제가 있었습니다.

(venv) [ec2-user@ip-172-31-23-78 waffle_backend]$ sudo nginx -s stop
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)

링크와 기타 사이트를 검색해본 결과 ps -ef | grep nginx을 통해 실행중인 nginx 프로세스를 직접 찾아 kill하거나 pkill nginx로 처리한 뒤 실행하라고 하였는데, 이 방법으로는 nginx를 멈추고 다시 시작하는 것이 가능했습니다. 위의 80번 포트 사용 문제나 pid 파일 미발견 문제는 어떻게 처리해야할까요?

esc5221 commented 2 years ago

sudo fuser -k 80/tcp (혹은 sudo fuser -k 8000/tcp) 사용해보실래요? 같은 에러 만났었는데, 이걸로 해결했던 것 같습니다. https://happist.com/573788/%EC%9B%B9%EC%84%9C%EB%B2%84-%EC%97%90%EB%9F%AC-emerg-bind-to-443-failed-98-address-already-in-use#fuser_-k_80tcp_sayong

Jhvictor4 commented 2 years ago

또한 nginx를 종료하고 다시 시작하려고 sudo nginx -s stop을 실행해본 결과, nginx.pid 가 발견되지 않는 문제가 있었습니다.

해당 문제는 nginx가 읽어오는 .pid 경로랑 /run.. 경로랑 달라서 발생하는 것 같습니다. 링크 참고 부탁드립니다.

그래서 80번 포트의 사용을 검색해보았더니 nginx worker process가 실행중인 것 같았는데, 그러면 어떻게 nginx를 재시작할 수 있는지 궁금합니다.

이거도 위랑 관련이 있는데, nginx가 본인 worker pid를 제대로 파악하지 못해서 nginx worker가 포트 80을 점유하고 있음에도 다른 프로세스가 쓰는 것으로 인식하는 것 같습니다.

pkill, fuser 등 프로세스를 죽였을때는 80포트가 비게 되지만, restart를 하려할 때는 nginx로 하여금 해당 동작을 실행하려는 것이기 때문에 에러가 나는 것 같습니다.

pid 설정을 재조정하시면 됩니다.

** nginx -s reload 설정을 통해 재시작 없이 변경사항 반영하실 수 있습니다. 근데 이 명령어도 pid 경로 설정을 잘 해주어야 작동하긴 합니다.

xxnpark commented 2 years ago

sudo fuser -k 80/tcp (혹은 sudo fuser -k 8000/tcp) 사용해보실래요? 같은 에러 만났었는데, 이걸로 해결했던 것 같습니다. https://happist.com/573788/%EC%9B%B9%EC%84%9C%EB%B2%84-%EC%97%90%EB%9F%AC-emerg-bind-to-443-failed-98-address-already-in-use#fuser_-k_80tcp_sayong

@esc5221 넵 가능하긴한데 pkill이랑 같은 방법인것같아서 다른 방법은 없는지 궁금했었습니다..ㅠ 그래도 답변 정말 감사합니다!

xxnpark commented 2 years ago

@Jhvictor4 답변 감사합니다! pid 관련해서 위 링크도 구글링하면서 봤던 것 같은데 ~nginx가 pid를 어디서 읽어오는지를 모르겠습니다..~

확인해보니 읽어오는 위치와 지정된 위치는 동일한 것 같습니다. 그런데 findnginx.pid 파일을 찾아봐도 전체 디렉토리 내에 존재하지 않는 것 같아 이게 문제일 것 같은데 맞을까요..?

minkyu97 commented 2 years ago

저도 비슷한 문제가 있었는데 그냥 ec2를 재시작했더니 해결 되었습니다.