php Nginx - 自定义 404 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1024199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nginx - Customizing 404 page
提问by thomas55
Nginx+PHP (on fastCGI) works great for me. When I enter a path to a PHP file which doesn't exist, instead of getting the default 404 error page (which comes for any invalid .html file), I simply get a "No input file specified.".
Nginx+PHP(在 fastCGI 上)非常适合我。当我输入一个不存在的 PHP 文件的路径时,我没有得到默认的 404 错误页面(任何无效的 .html 文件都会出现),我只是得到一个“没有指定输入文件。”。
How can I customize this 404 error page?
如何自定义此 404 错误页面?
采纳答案by WizKid
You use the error_page property in the nginx config.
您在 nginx配置中使用 error_page 属性。
For example, if you intend to set the 404 error page to /404.html, use
例如,如果您打算将 404 错误页面设置为/404.html,请使用
error_page 404 /404.html;
Setting the 500 error page to /500.htmlis just as easy as:
将 500 错误页面设置/500.html为同样简单:
error_page 500 /500.html;
回答by Great Turtle
You can setup a custom error page for every location block in your nginx.conf, or a global error page for the site as a whole.
您可以为 nginx.conf 中的每个位置块设置一个自定义错误页面,或者为整个站点设置一个全局错误页面。
To redirect to a simple 404 not found page for a specific location:
要重定向到特定位置的简单 404 未找到页面:
location /my_blog {
error_page 404 /blog_article_not_found.html;
}
A site wide 404 page:
一个站点范围的 404 页面:
server {
listen 80;
error_page 404 /website_page_not_found.html;
...
You can append standard error codes together to have a single page for several types of errors:
您可以将标准错误代码附加到一起,以便为多种类型的错误创建一个页面:
location /my_blog {
error_page 500 502 503 504 /server_error.html
}
To redirect to a totally different server, assuming you had an upstream server named server2 defined in your http section:
要重定向到完全不同的服务器,假设您在 http 部分定义了一个名为 server2 的上游服务器:
upstream server2 {
server 10.0.0.1:80;
}
server {
location /my_blog {
error_page 404 @try_server2;
}
location @try_server2 {
proxy_pass http://server2;
}
The manualcan give you more details, or you can search google for the terms nginx.conf and error_page for real life examples on the web.
该手册可以为您提供更多详细信息,或者您可以在 google 中搜索术语 nginx.conf 和 error_page 以获取网络上的真实示例。
回答by dhr_p
Be careful with the syntax! Great Turtle used them interchangeably, but:
注意语法!Great Turtle 交替使用它们,但是:
error_page 404 = /404.html;
error_page 404 = /404.html;
Will return the 404.html page with a status code of 200 (because = has relayed that to this page)
将返回状态代码为 200 的 404.html 页面(因为 = 已将其转发到此页面)
error_page 404 /404.html;
error_page 404 /404.html;
Will return the 404.html page with a (the original) 404 error code.
将返回带有(原始)404 错误代码的 404.html 页面。
https://serverfault.com/questions/295789/nginx-return-correct-headers-with-custom-error-documents
https://serverfault.com/questions/295789/nginx-return-correct-headers-with-custom-error-documents
回答by Grigori Kochanov
The "error_page" parameter makes a redirect, converting the request method to "GET", it is not a custom response page.
“error_page”参数进行重定向,将请求方法转换为“GET”,它不是自定义响应页面。
The easiest solution is
最简单的解决方案是
server{
root /var/www/html;
location ~ \.php {
if (!-f $document_root/$fastcgi_script_name){
return 404;
}
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params.default;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
By the way, if you want Nginx to process 404 status returned by PHP scripts, you need to add
顺便说一句,如果你想让Nginx处理PHP脚本返回的404状态,你需要添加
[fastcgi_intercept_errors][1] on;
[fastcgi_intercept_errors][1] on;
E.g.
例如
location ~ \.php {
#...
error_page 404 404.html;
fastcgi_intercept_errors on;
}
回答by Isius
These answers are no longer recommended since try_filesworks faster than ifin this context. Simply add try_filesin your php location block to test if the file exists, otherwise return a 404.
不再推荐这些答案,因为try_files比if在这种情况下工作得更快。只需try_files在您的 php 位置块中添加以测试文件是否存在,否则返回 404。
location ~ \.php {
try_files $uri =404;
...
}

