16

I'm running a rails application on Ruby 2.0/Puma instances and am trying to customize the nginx configuration. I need to increase the permitted request size to allow file uploads. I've found some other posts that have lead me to add this to my .ebextensions:

files:
  "/etc/nginx/conf.d/proxy.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      client_max_body_size 70M;

That does create the file as expected, but it doesn't seem to work until I manually restart nginx. Because of that, I've tried to figure out a way to restart nginx with .ebextensions commands, but haven't had any success. Does anyone know of a way to restart nginx with .ebextensions or know of a better approach to solving this problem?

Graham
  • 463
  • 1
  • 5
  • 12

8 Answers8

16

I found a way to restart nginx after deployment using an undocumented technique for running post-deployment scripts. I added this to my .ebextensions:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      service nginx restart
Graham
  • 463
  • 1
  • 5
  • 12
5

To reload the nginx config, you can use container_commands

From http://www.infoq.com/news/2012/11/elastic-beanstalk-config-files:

The container_commands key allows you to execute commands for your container. They are run after the application and web server have been set up and the application has been extracted, but before the application is deployed. container_commands are processed in lexicographical order by name.

container_commands:
  01_reload_nginx:
    command: "service nginx reload"
Ari
  • 990
  • 12
  • 12
4

I might be a little late with the response here, but I've discovered another, less intrusive way to configure nginx on Elastic Beanstalk. You can specify configuration files for nginx directly by creating an .ebextensions/nginx/conf.d directory. Any config files found inside are automatically copied to your /etc/nginx/conf.d/ directory during the EB deployment. This seems to be a more robust solution.

Documentation available here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-nginx.html

EDIT: As pointed out in comments, Elastic Beanstalk has inconsistent implementations between platforms. The documentation here is for Java SE, and it appears this documentation is not relevant for all platforms.

ajxs
  • 3,347
  • 2
  • 18
  • 33
  • 1
    Did you get this to work for a ruby environment too? The docs appear to only show this in the Java documentation. I wasn't able to get this to work in my Ruby/Puma environment, but I'm not sure if I did something wrong or not. – Ryan McGeary Apr 20 '18 at 04:05
  • 4
    I opened a support ticket with Amazon last night. They confirmed that their different beanstalk platforms don’t work the same between each other and the Ruby platform doesn’t yet support an exploded nginx dir. They are escalating my ticket to get me an explanation of why the inconsistency. – Ryan McGeary Apr 20 '18 at 12:15
  • 1
    Oh wow, that's ridiculous! I can't say I'm actually all that surprised though considering my experiences working with EBS. – ajxs Apr 21 '18 at 01:30
1

this is my configuration and worked for me. You have to include it inside of the http block.

files:
  "/etc/nginx/conf.d/proxy.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
        http {
          client_max_body_size 20M;
        }
a14m
  • 7,808
  • 8
  • 50
  • 67
Israel Barba
  • 1,434
  • 20
  • 28
  • 2
    When I do that, I get "nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/proxy.conf:1" when manually trying to restart nginx. Maybe we're on different versions of nginx. Anyway, my configuration above works fine for me, but only after I ssh into the server and manually restart nginx. Fundamentally, the problem is that nginx is not picking up on the new configuration after a deployment. – Graham Jan 29 '15 at 20:42
1

The following worked for me (I increased my HTTP payload to 100M - please adjust if you'd like to increase to another size):

files:
  "/etc/nginx/conf.d/proxy.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      client_max_body_size 100M;
  "/opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      service nginx restart
shaam
  • 106
  • 14
1

I was able to get it working by adding the configuration files under the .platform directory as noted in https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html. I created a file .platform/nginx/conf.d/increase_upload_size.conf with the following code and just did a standard deploy

client_max_body_size 10M;
0

I got it working like this. No need to restart or reload nginx since the commands (and not container_commands) runes BEFORE the application deploy.

commands: 
  01-get-nginx-conf-file:
    command: "aws s3 cp s3://somepath/nginx.conf /home/ec2-user"
  02-replace-default-nginx-config:
    command: "cp /home/ec2-user/nginx.conf /etc/nginx/nginx.conf"
Christian Dechery
  • 876
  • 10
  • 31
0

I had a similar situation with a Docker deployment into Elastic Beanstalk. I was able to solve the change as well as the nginx reload with a single config file here: <app>/.ebextensions/increase_upload_size.config including the following code:

container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

files:
  "/etc/nginx/conf.d/proxy.conf" :
    mode: "000644"
    owner: root
    group: root
    content: |
      client_max_body_size 20M;

The change was implemented when I did an "Upload and Deploy" within EB.

Ben
  • 2,348
  • 1
  • 20
  • 23