Elastic Beanstalk Deployment: nginx

Most node implementations are frontended by a proxy server such as nginx. AWS Elastic beanstalk's node AMI puts nginx in front of node on every instance with an AWS load balancer in front of the instances.

Most of this works out of the box as long as you follow loopback conventions but with some additional configuration you can offload a lot of work from your app server.

Get client ip

It is often necessary for the application server to have access to the requesting client ip which by default would be in the x-forwarded-for header set by nginx in the request to the backend. Loopback is build on express so we can set a flag to expose the ip on the request object req.ip.


app.enable('trust proxy');  

req.ip will now be set to req.headers['x-forwarded-for'] if supplied by the proxy.

Static Files

All static assets (images js css etc) should be offloaded to nginx for delivery so the node app server and api only have to respond to requests that have dynamic results.

Elastic beanstalk's node implementation has a configuration list for "Static Files" to map the virtual paths to directories in your project where nginx can find the files.


You should enable compression in nginx to compress pages and other assets. In elastic beanstalk this is done in the "Container Options" section of "software configuration" for your environment.

More control

It is sometimes necessary to take more control of the nginx config. In Elastic Beanstalk this is done using the rather cryptic .ebextentions capability. First define a directory called .ebextensions in your project root then add nginx config files as follows which will be created on instance startup. In this example larger file upload is allowed and a few additional mime types are added to compression.


  "/etc/nginx/conf.d/proxy.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      client_max_body_size 500M;
  "/etc/nginx/conf.d/gzip.conf" :
    owner: root
    group: root
    content: |
      gzip_types text/plain text/xml text/css application/x-javascript application/json application/font-woff2;

Photo: Ghost Cactus, Barbados (2016)
Document version 1.1