Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom header to Amazon AWS ELB response

I would like to add a custom header to the responses generated by an Elastic Load Balancer. This custom header should contain the EC2 instance ID (something like X-EC2-Instance-ID=123456) corresponding to the backend who has handled the request.

Is it possible to do this from the ELB itself, without configuring this header on each backend server?

thanks!

like image 773
Andrés Fernández Avatar asked Feb 15 '17 16:02

Andrés Fernández


People also ask

Can AWS ALB add custom header?

As far as I know there is no way to set custom headers at the ALB level. You can however add CloudFront as a CDN in front of it, that allows you to set custom headers, which will then be passed on to the ALB.

How do I add a header to an HTTP response?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

Does ALB strip headers?

But the ALB seems to strip the header and replace it with its own (which becomes X-Forwarded-Proto: http ), and then the backend application on the ECS servers sees http and writes all it's links/resource paths as http, causing an insecure mixed content warning in Safari, Chrome, etc.

How do I set up a load balancer in AWS?

Create an Application Load Balancer, if you don't already have one. Open the Amazon Elastic Compute Cloud (Amazon EC2) console. On the navigation pane, under Load Balancing, choose Load Balancers. Select your load balancer. Choose the Listeners tab. Select your HTTP listener, and then choose View/edit rules.

How to modify the X-Forwarded-forheader using the AWS CLI?

To modify, preserve, or remove the X-Forwarded-Forheader using the AWS CLI Use the modify-load-balancer-attributescommand with the routing.http.xff_header_processing.modeattribute.

Can I add custom HTTP headers to a response?

You can add other custom headers (metadata), but they must start with the x-amz-meta- prefix. Hence, using only CloudFront and S3 it is not possible to add to responses arbitrary HTTP headers, for example, X-Robots-Tag, X-Content-Type-Options or security headers.

Is there a way to set custom headers in Alb?

As far as I know there is no way to set custom headers at the ALB level. You can however add CloudFront as a CDN in front of it, that allows you to set custom headers, which will then be passed on to the ALB.


1 Answers

It isn't possible to accomplish this at the ELB. You would have to add this at the EC2 level.

like image 57
Mark B Avatar answered Oct 03 '22 00:10

Mark B