
환경:Nginx, Node.js
Nginx에서 오류 처리를 실험하고 있는데 파일을 반환할 행운이 없습니다.
ngxinx.conf
http가 request_method
아닌 GET
경우 아래의 단순화된 내용에서 HEAD
서버 POST
가 405.html 오류 페이지를 반환하도록 하고 싶습니다.
원하는 출력:405.html이 브라우저로 전송됩니다.
실제 출력:이 일반 텍스트는 브라우저로 전송됩니다. http://www.example.com/html/405.html
참고: Postman에서 이를 테스트 중이므로 Chrome에 다양한 HTTP 메서드를 서버에 보낼 수 있는 추가 확장 프로그램을 설치할 필요가 없습니다.
내 구성의 관련 부분:
server {
include conf.d/listen-80;
server_name example.com www.example.com;
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 405 $scheme://www.example.com/html/405.html;
}
return 301 https://www.example.com$request_uri;
}
server {
include conf.d/listen-443;
server_name example.com;
include conf.d/letsencrypt;
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 405 $scheme://www.example.com/html/405.html;
}
return 301 https://www.example.com$request_uri;
}
server {
include conf.d/listen-443;
server_name www.example.com;
include conf.d/letsencrypt;
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 405 $scheme://www.example.com/html/405.html;
}
root /srv/example/views/public;
location ~* \.(htm|html)$ {
include conf.d/content-security-policy-html-rendered;
include conf.d/cache-control-30-days;
include conf.d/security-headers-html-rendered;
}
}
답변1
이후의 URL은 return nnn
리디렉션 코드(301, 302, 303, 307 및 308 상태 코드)의 대상 URL로만 처리됩니다. 문서에는 코드가 다른 것일 때 수행되는 작업이 명확하게 나와 있지 않습니다.
귀하의 경우 오류 페이지를 얻으려면 다음을 사용하십시오.
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 405;
}
error_page 405 /html/405.html;
/html/405.html
이는 상태 코드 405가 반환될 때 nginx에게 전송하도록 지시합니다 .