Erik Axelsson
Erik Axelsson

Reputation: 401

How to change nginx config in amazon elastic beanstalk running a docker instance

After i login and the cookie is set I get error 502. When i read the log i get the error:

014/05/17 01:54:43 [error] 11013#0: *8 upstream sent too big header while reading response
header from upstream, client: 83.248.134.236, server: , request: "GET /administration
HTTP/1.1", upstream:

After some fast googling i found: http://developernote.com/2012/09/how-i-fixed-nginx-502-bad-gateway-error/

and I want to try to set fastcgi_buffers and fastcgi_buffer_size to a different value. But how do i set variable on nginx in amazon elasticbeanstalk?

The nginx server is before my docker instance.

Upvotes: 39

Views: 72134

Answers (6)

0urz4g
0urz4g

Reputation: 473

Update as of 2023 october

As Amazon Linux 2 (AL2) will reach its end of life in 2025, AWS now recommends to start every new ElasticBeanstalk environment with a platform based on Amazon Linux 2023.

All the configuration related to nginx remains the same as it were with AL2.

You can continue to place custom configurations in

.platform/nginx/nginx.conf (to override the Elastic Beanstalk default nginx configuration completely)

or

.platform/nginx/conf.d/myconf.conf (to extend the Elastic Beanstalk default nginx configuration)

Extending Elastic Beanstalk Linux platforms

Upvotes: 2

Sunil Shahi
Sunil Shahi

Reputation: 653

Update as of 2022 Feb 2nd

Seems like AWS has changed some stuff in newer versions of Elastic Beanstalk that uses Amazon Linux 2 so the approach that was mentioned by @jsebfranck no longer works if you are using EBS with Amazon Linux2.

The newer approach is to create a ".platform" folder in root of your zip bundle and add your nginx.conf file at "./platform/nginx/nginx.conf" the eb engine should swap the file if found in that location.

See here for details: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.migration-al.html

Migration considerations

Also see "Reverse proxy configuration" > "Configuring nginx" section here.

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html swapping nginx.conf

Upvotes: 12

AngryUbuntuNerd
AngryUbuntuNerd

Reputation: 810

A cleaner approach (if you're using the Java or Go platform on Elastic Beanstalk) is to have nginx .conf files with your wished changes in a subfolder in .ebextensions:

You can now place an nginx.conf file in the .ebextensions/nginx folder to override the Nginx configuration. You can also place configuration files in the .ebextensions/nginx/conf.d folder in order to have them included in the Nginx configuration provided by the platform.

Source

Upvotes: 8

jsebfranck
jsebfranck

Reputation: 724

Another way to extend Elastic Beanstalk nginx config is to create a file in the .ebextensions directory, named for example nginx.config with the following content :

    files:
      "/etc/nginx/conf.d/000_my_config.conf":
      content: |
        upstream nodejsserver {
          server 127.0.0.1:8081;
          keepalive 256;
        }

        server {
          listen 8080;

          location / {
            proxy_pass  http://nodejsserver;
            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;
          }

          location /myconfig {
            proxy_pass http://my_proxy_pass_host;
          }
        }

/etc/nginx/conf.d/000_my_config.conf is the filename which will be created on the Elastic Beanstalk EC2 instances. By default this configuration is in the file /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf. So if you prefix with 000, it guarantees you that your configuration will be taken into account first.

The content has been copied from the default nginx configuration (/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf again), then customized with my own configuration.

Upvotes: 26

Matt Holtzman
Matt Holtzman

Reputation: 633

Amazon actually recommends editing the staging version of the nginx deployment file. There are several located at /tmp/deployment/config/, one for editing the general 'http' context, and then a few for configuring different aspects of the server.

I wanted to attach caching functionality to the default proxy server, so I wrote an .ebextensions config file to replace #etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf, which is then copied over to /etc/nginx/conf.d during deployment. You can inline the file if its simple enough, but I put mine in S3 so that different applications and pull it down and use it. Here's the config file:

commands: 
  01-get-nginx-conf-file:
    command: aws s3 cp s3://<bucket-name>/custom-nginx.conf /home/ec2-user

container_commands:
  01-replace-default-nginx-config:
    command: mv -f /home/ec2-user/custom-nginx.conf /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf

Upvotes: 39

svenkreiss
svenkreiss

Reputation: 481

I also needed to modify the nginx configuration.

  1. Create a script that modifies the nginx configuration (probably you want /etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker.conf) and restarts the nginx service (service nginx restart).
  2. You need to execute that script after this nginx config file is written which is after normal ebextensions are executed. This is undocumented, but Evan shared how to do this here: essentially you use an ebextension to copy the script into a directory with hooks that gets executed at the proper time.

An example ebextension config is .ebextensions/01modify_nginx.config:

container_commands:
  copy:
    command: "cp .ebextensions/01rewrite_nginx_config.py /opt/elasticbeanstalk/hooks/appdeploy/enact/"
  make_exe:
    command: "chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/01rewrite_nginx_config.py"

This is working nicely now for my project (here is the source where you can see it in action).

Upvotes: 25

Related Questions