
내 error_page에 대한 대체를 만들려고 합니다. 기본적으로 논리는 다음과 같아야 합니다.
- foobar.html 로드
- 원격 서버에 존재하지 않습니다. -> 원격 서버에서 404.html을 로드하여 404 페이지를 표시합니다.
- 원격 서버에 존재하지 않습니다. -> 로컬 파일 시스템에 404.html을 로드합니다.
둘 다 로드 localhost/404.html
하면 localhost/global404.html
작동하지만 중단하면 (http 서버에서 파일을 제거하여) 예상한 대로 페이지가 localhost/404.html
표시되지 않습니다 .global404.html
server {
listen 80;
server_name example.com www.example.com;
proxy_intercept_errors on;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
error_page 404 /404.html;
}
location /404.html {
proxy_pass http://localhost:3000/404.html;
error_page 404 /global404.html;
}
location /global404.html {
root /usr/share/nginx/html;
}
}
위의 내용은 내가 실행했을 때 잘 작동합니다 http://localhost/404.html
(404.html 파일이 원격 서버에 있을 때 파일을 삭제하면 global404.html 파일이 로드되는 것으로 표시됩니다).
그러나 존재하지 않는 페이지를 입력하면 기본 nginx 404 페이지만 표시됩니다.
답변1
질문에 남겨진 의견 덕분에 recursive_error_pages
계단식/재귀를 허용하는 옵션을 찾을 수 있었습니다 error_pages
. 문서에 누락된 것이 어리석은 것 같습니다.
하지만 단순히 해보면
server {
listen 80;
server_name example.com www.example.com;
proxy_intercept_errors on;
recursive_error_pages on;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
error_page 404 /404.html;
}
location /404.html {
proxy_pass http://localhost:3000/404.html;
error_page 404 /global404.html;
}
location /global404.html {
root /usr/share/nginx/html;
}
}
매력을 발휘했습니다. 나는 nginx를 좋아한다.