1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { location /test.js { return 403; } location /404 { return 404; } location /500 { return 500; } error_page 500 502 503 504 /status.html; error_page 404 /status.html; } <!-- more -->
|
如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| server { listen 80; server_name test.me; root /Users/xiaowu/work/test.me; if ($request_uri ~* ^/403) { return 403; } location ~* "^/500/?$" { return 500; } if ($request_uri ~* ^/501/) { return 501; } location ~* "^/502(/.*)?$" { return 502; } location = /503 { return 503; } }
|
error_page配置小提示
注意 error_page
配置时加 =
和不加 =
的区别,加了 =
表示响应为指定的 http status code
,默认为 200,不加 =
为原错误的状态码~
1 2 3 4 5 6 7 8 9
| error_page 404 /404.html error_page 404 500 /404.html;
error_page 404 500 = /404.html;
error_page 404 500 =404 /404.html;
error_page 404 =301 https://xuexb.com/404;
|
这样就可以根据自己需求配置错误页为指定的状态码,因为非 200 的状态码可能会被浏览器拦截。