我在 Amazon S3 上托管了一个网页,但我不希望 http 响应代码为200
. 该页面是一个维护页面,当我关闭我们的主网站进行维护时,我会将流量重定向到该页面。
我希望 Amazon S3 页面包含以下响应标头:
HTTP/1.1 503 Service unavailable
亚马逊提供了向 S3 对象添加一些元数据的能力,但 http 状态代码没有任何内容。
可能吗?
我在 Amazon S3 上托管了一个网页,但我不希望 http 响应代码为200
. 该页面是一个维护页面,当我关闭我们的主网站进行维护时,我会将流量重定向到该页面。
我希望 Amazon S3 页面包含以下响应标头:
HTTP/1.1 503 Service unavailable
亚马逊提供了向 S3 对象添加一些元数据的能力,但 http 状态代码没有任何内容。
可能吗?
不确定哪些浏览器或爬虫支持此功能。但是您可能会使用meta http-equiv status元标记来完成此操作。
<meta http-equiv="status" content="503 Service Unavailable" />
规范说要以与发送 503 作为状态码相同的方式对待它。
我相信你可以让 Cloudfront 做到这一点。我还没有测试过,但试试这个:
http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/custom-error-pages.html
您无法自定义 S3 响应的状态代码。
您可以使用 API Gateway 作为 S3 网站错误页面的代理,您可以在其中自定义返回的状态代码。
在 Amazon 允许来自 S3 的自定义状态代码之前,这里有一个使用 nginx 的解决方法。
我们观察一个特定文件的存在,它充当维护模式的“ON 开关”。如果找到,我们proxy_pass
向 S3 请求 - 诀窍是return 503
将 503 状态代码的处理重定向到 nginx“命名位置”。
示例 nginx conf 文件(仅显示相关位):
server {
...
# Redirect processing of 503 status codes to a nginx "named location".
error_page 503 @maintenance;
# "Maintenance Mode" is off by default - Use a nginx variable to track state.
set $maintenance off;
# Switch on "Maintenance Mode" if a certain file exists.
if (-f /var/www/app/maintenanceON) {
set $maintenance on;
}
if ($maintenance = on) {
# For Maintenance mode Google recommend using status code: "503 Service unavailable".
return 503;
}
...
location @maintenance {
# Redirect the request to a static maintenance page hosted in Amazon S3.
# Note: Use proxy_pass instead of rewrite so we keep the 503 code (otherwise nginx serves a 302 code)
rewrite ^(.*)$ /index.html break;
proxy_pass http://bucketname.s3-website-us-east-1.amazonaws.com;
}
}