11

I have a webpage hosted on Amazon S3 but I don't want the http response code to be 200. The page is a maintenance page that I'll redirect traffic to when I take our main website down for maintenance.

I want the Amazon S3 page to include a response header of:

HTTP/1.1 503 Service unavailable

Amazon give the ability to add some metadata to the S3 Object but there is nothing for the http status code.

Is it possible?

Tom
  • 14,041
  • 16
  • 64
  • 80

4 Answers4

4

Not sure which browsers, or crawlers, that supports this. But you could potentially use the meta http-equiv status meta tag to accomplish this.

<meta http-equiv="status" content="503 Service Unavailable" />

The specification says to treat it in the same way as if 503 had been sent as the status code.

Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
  • Um, I don't get the impression *anything* at all is using `http-equiv="status"`. Any evidence to the contrary most welcome. – mlissner Jul 18 '22 at 23:40
2

I believe you can get Cloudfront to do this. I haven't tested it yet, but try this:

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/custom-error-pages.html

pcormier
  • 568
  • 1
  • 5
  • 15
  • You can only customize status codes when implementing a custom error page. At this time, custom error pages can only be made for a handful of predefined status codes >400 – Steve Buzonas Feb 04 '18 at 09:06
1

You cannot customize the status code for S3 responses.

You can use API Gateway as a proxy to your S3 website error page where you can customize status codes returned.

Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55
-1

Until Amazon allow a custom status code from S3, here is a workaround using nginx.

We watch for the existence of a specific file, that acts as a "ON switch" for maintenance mode. If found, we proxy_pass requests to S3 - The trick is to return 503 but redirect processing of 503 status codes to a nginx "named location".

Example nginx conf file (just the relevant bits shown):

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;
    }
}
Tom
  • 14,041
  • 16
  • 64
  • 80
  • 1
    While this is technically a solution to missing functionality, is it really worth paying $5 or more for degraded availability to correct a status code for a website that may cost less than $0.05/month for the whole website? – Steve Buzonas Feb 04 '18 at 09:13
  • For us it is. Our website costs $15k per month to run, so taking the time to ensure we have a maintenance website hosted in S3 with the correct status code is worth the effort. – Tom Feb 05 '18 at 19:30
  • I don't disagree that having the proper status code is preferred. My point is, the question title is "How to change http response code on an object in Amazon S3"; setting up a host to fix the status code for a S3 website object is an astronomical price increase compared to the cost of hosting the object on S3. – Steve Buzonas Feb 06 '18 at 23:13
  • I'm glad you found an acceptable solution to your scenario but I have the exact same question and this isn't an answer for me. – Liz Av Jul 02 '20 at 15:31