我学会了如何让NGINX回归 503
客户错误页面,
但我无法找到如何做到以下几点:
示例配置文件:
location / {
root www;
index index.php;
try_files /503.html =503;
}
error_page 503 /503.html;
location = /503.html {
root www;
}
如您所见,根据上面的代码,如果一个页面调用 503.html
在我的根目录中找到,该站点将此页面返回给用户。
但 似乎虽然上面的代码有效,但有人只是访问我的网站打字
它不会捕获如下请求:
使用我的代码,用户仍然可以看到配置文件页面或其他任何页面 index.php
。
问题是:
如何将请求捕获到我站点中的所有页面并转发给它们 503.html
每当 503.html
存在于我的根文件夹中?
更新:将“if -f”更改为“try_files”。
尝试这个:
server {
listen 80;
server_name mysite.com;
root /var/www/mysite.com/;
location / {
try_files /maintenance.html $uri $uri/ @maintenance;
# When maintenance ends, just mv maintenance.html from $root
... # the rest of your config goes here
}
location @maintenance {
return 503;
}
}
更多信息:
https://serverfault.com/questions/18994/nginx-best-practices
http://wiki.nginx.org/HttpCoreModule#try_files
更新:将“if -f”更改为“try_files”。
尝试这个:
server {
listen 80;
server_name mysite.com;
root /var/www/mysite.com/;
location / {
try_files /maintenance.html $uri $uri/ @maintenance;
# When maintenance ends, just mv maintenance.html from $root
... # the rest of your config goes here
}
location @maintenance {
return 503;
}
}
更多信息:
https://serverfault.com/questions/18994/nginx-best-practices
http://wiki.nginx.org/HttpCoreModule#try_files
以下配置适用于接近最新的稳定nginx 1.2.4
。
我找不到一种方法来启用维护页面而不使用 if
但显然是根据 IfIsEvil 这是好的 if
。
- 启用维护
touch /srv/sites/blah/public/maintenance.enable
。您可以 rm
要禁用的文件。
- 错误
502
将被映射到 503
这是大多数人想要的。你不想给谷歌 502
。
- 习惯
502
和 503
页面。您的应用将生成其他错误页面。
网络上还有其他配置,但它们似乎不适用于最新的nginx。
server {
listen 80;
server_name blah.com;
access_log /srv/sites/blah/logs/access.log;
error_log /srv/sites/blah/logs/error.log;
root /srv/sites/blah/public/;
index index.html;
location / {
if (-f $document_root/maintenance.enable) {
return 503;
}
try_files /override.html @tomcat;
}
location = /502.html {
}
location @maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
error_page 503 @maintenance;
error_page 502 =503 /502.html;
location @tomcat {
client_max_body_size 50M;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Referer $http_referer;
proxy_set_header X-Forwarded-Proto http;
proxy_pass http://tomcat;
proxy_redirect off;
}
}
其他答案都是正确的,但只是添加,如果您使用内部代理,您还需要添加 proxy_intercept_errors on;
在您的一个代理服务器上。
所以例如......
proxy_intercept_errors on;
root /var/www/site.com/public;
error_page 503 @503;
location @503 {
rewrite ^(.*)$ /scripts/503.html break;
}