DEV Community

Cover image for Extend nginx/Apache Proxy Configurations on AWS ElasticBeanstalk
David Ojeda
David Ojeda

Posted on • Edited on • Originally published at blog.davidojeda.dev

Extend nginx/Apache Proxy Configurations on AWS ElasticBeanstalk

AWS ElasticBeanstalk applications use either an nginx or Apache proxy to relay requests. Using the .ebextensions feature of ElasticBeanstalk we can extend the configuration of these proxies. If you don't know how .ebextensions work you can read more here.

I'm going to extend the default nginx proxy configurations using .ebextensions. The same procedure can be used to extend an Apache proxy.

Create a .conf file

First we need to create a .conf file with the desired directives. A list of nginx directives can be found here. My conf file- named proxy.conf -increases some timeouts of the proxy:

Create nginx conf.d directory

Now we need the directory where our configuration file will be. Under .ebextensions, create a directory named 'nginx', and inside it another named 'conf.d'. Then add the file you just created. Your dir structure should look like this:

  • .ebextensions
    • nginx
      • conf.d
        • proxy.conf

Now, when you deploy a new version of your application, ElasticBeanstalk will automatically copy your files on the .ebextensions/nginx/conf.d/ directory to the /etc/nginx/conf.d/ directory of your instances.

This all works because the default nginx.conf file- on line 21 -specifies to include all .conf files under the conf.d directory:

The directives from the .conf file will be added to the http block of the default configuration.

If you need to add directives to the server block you will need to add .conf files to the elasticbeanstalk folder (see line 39 of previous Gist). That dir structure would look:

  • .ebextensions
    • nginx
      • conf.d
        • proxy.conf
      • elasticbeanstalk
        • my_other_conf.conf

Same can be done for an Apache proxy. The difference is on the directory structure. For Apache your structure should be this:

  • .ebextensions
    • httpd
      • conf
        • proxy.conf

Wrap-up

Using .ebextensions is by far the simplest method to add custom configurations to your nginx or Apache proxy. Create as many configuration files as you need and add them to the corresponding directory under .ebextensions and you are done.

Top comments (15)

Collapse
 
harjotscs profile image
Harjot Singh

I thoroughly followed every line of your post but elastic beanstalk is not creating file please help it’s important

Collapse
 
david_ojeda profile image
David Ojeda

Hey there!! Are you using Apache or Nginx?

Collapse
 
harjotscs profile image
Harjot Singh

I am using nginx and my web app platform is node js

Thread Thread
 
david_ojeda profile image
David Ojeda

Are you using .ebextensions already or is this the first file you are creating like this?

Where are you creating the .ebextensions directory?

Make sure your dir structure looks like this:
dir structure

Thread Thread
 
harjotscs profile image
Harjot Singh

I am creating .ebextensions folder for the first time my directory structure is absolutely similar to this except i didn't added elasticbeanstalk directory except everything is same
A picture of my structure is attatched
dev-to-uploads.s3.amazonaws.com/i/...

Thread Thread
 
david_ojeda profile image
David Ojeda

I'm not sure what's going on, everything looks good.

You can try these steps instead: docs.aws.amazon.com/elasticbeansta...

Thread Thread
 
harjotscs profile image
Harjot Singh

Thanks I will try these steps

Thread Thread
 
rahul689 profile image
rahul689

Hello,
Did you able to solved the problem?
I am troubling with same.

Thread Thread
 
harjotscs profile image
Harjot Singh

No I tried different methods but nothing worked
but i can tell you about a improvisation/hack which will work in changing your default nginx config values connect to your instance via putty or ssh terminal like git bash or online terminal then navigate to /etc/nginx/conf.d using cd command inside conf.d directory create a new file in the form anyname.conf paste your new config value and restart nginx.

But there is an issue in this this will only work if you use single instance otherwise after sometime when your load balancer will change/replace the instance to reduce load on the servers your nginx values will be reset to default

In case you find out any other better method please let me know i am also searching for the same

Thread Thread
 
rahul689 profile image
rahul689

Thanks for sharing the hack. I am also running the system by hack. But this is not proper solution for autoscale.
@david
Is there anything settings on IAM role permission?
Although i am using the default role.
Thanks in advance.

Thread Thread
 
david_ojeda profile image
David Ojeda

Hey there!

Have you tried using the files command on .ebextension? Something like this:

files:
  /etc/nginx/conf.d/proxy.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server 127.0.0.1:5000;
        keepalive 256;
      }

      server {
        listen 8080;

        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
            set $year $1;
            set $month $2;
            set $day $3;
            set $hour $4;
        }
        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
        access_log  /var/log/nginx/access.log  main;

        location / {
            proxy_pass  http://nodejs;
            proxy_set_header   Connection "";
            proxy_http_version 1.1;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        gzip on;
        gzip_comp_level 4;
        gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location /static {
            alias /var/app/current/static;
        }

      }

  /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash -xe
      rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      service nginx stop 
      service nginx start

container_commands:
  removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

It's documented here. It's another way to add custom files to your instances when deploying new versions.

Thread Thread
 
rahul689 profile image
rahul689

Hello friend,
Thanks for your help. My problem was hierarchy.

  1. .ebextension should contain in root application directory. My application was in sub directory. And was put .ebextension to subdirectory. :(

Once take back .ebextension to main root directory the problem was resolved.

Thread Thread
 
david_ojeda profile image
David Ojeda

Yeeeei! I'm glad you worked it out.

I might as well add that reminder of the root directory to the post.

Thanks a lot for getting back here!

Collapse
 
martin49007715 profile image
Martin

forbiden upload file .ebextensions/01-symfony.config

files:
"/etc/nginx/conf.d/elasticbeanstalk/z-symfony.conf":
mode: "000644"
owner: root
group: root
content: |
location / {
try_files $uri /index.php$is_args$args;
}

Collapse
 
martin49007715 profile image
Martin

Hello, when I deploy in ssh I see that the file is generated, but when the deployment finishes it automatically deletes it. Do you know what it can be? Thank you all