본문 바로가기

Web

[Error] 새로고침하면 nginx 404 에러

문제상황

nginx로 빌드를 마치고 크롬 브라우저에서 새로 고침만 하면 404 에러가 뜬다

(로컬에서는 정상적으로 동작했으나, NGINX 서버에 올렸을때 발생하는 경우)

 

해결 방법. nginx.conf 파일 location 수정

1. nginx.conf 파일의 위치는 '/etc/nginx/sites-available/default'이다.

해당 파일을 터미널에서 접근하고

sudo nano /etc/nginx/sites-available/default

 

2. nginx.conf 파일에 해당 내용을 추가해주면 된다.

location / {
 try_files $uri /index.html;
}

 

수정 전 nginx.conf 파일

 

해당 파일 접근 하는 방법은 다음 중에서 두 번째 명령어를 실행했을 때

# nginx 설치 (시간이 조금 걸립니다)
sudo apt install nginx

# 설정 파일 수정 
sudo nano /etc/nginx/sites-available/default 

root /var/www/html;     부분을 찾은 다음,
root 아까 기록한 build 폴더 위치로 바꿉니다.

# 수정 후 저장 
컨트롤 x -> 컨트롤 y -> 엔터

 

출처:
https://stackoverflow.com/questions/43555282/react-js-application-showing-404-not-found-in-nginx-server