Search code examples
nginxtomcatamazon-elastic-beanstalk

NGINX Protocol header is null for $http_x_forwarded_proto ElasticBeanstalk Tomcat behind NLB


This is my .platform/nginx/conf.d/elasticbeanstalk/00_application.conf which is definitely working because I'm able to get the client's host just fine. However $http_x_forwarded_proto is null.

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header X-Request-URI $request;
    proxy_set_header X-Request-PATH $request_uri;
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version  1.1;

}
client_max_body_size 20M;

Solution

  • The solution to ensuring that the $http_x_forwarded_proto header is populated consisted of two steps:

    1. Edit your NLB target group's attributes and ensure the Proxy protocol v2 attribute is enabled

    2. Edit the nginx.conf file and insert proxy_protocol like so:

      server {
          listen        80 default_server proxy_protocol;
          access_log    /var/log/nginx/access.log main;
      
          client_header_timeout 60;
          client_body_timeout   60;
          keepalive_timeout     60;
          .
          .
          .
          # Include the Elastic Beanstalk generated locations
          include conf.d/elasticbeanstalk/*.conf;
      }
      

    The nginx.conf file can be placed in .platform/nginx/nginx.conf of your project's web root folder